Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<load_from_csv>
%matplotlib inline
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train.describe()<drop_column>
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv' )
train_ID = train['Id']
test_ID = test['Id']
train.drop("Id", axis = 1, inplace = True)
 test.drop("Id", axis = 1, inplace = True )<drop_column>
y = train_df['target']
X_lr = train_df.drop(['id', 'target'], axis=1)
 scaler = StandardScaler()
scaler.fit(X_lr)
 X_lr = scaler.transform(X_lr)
 X_lr_test = scaler.transform(test_df.drop('id', axis=1 ).values)
 X_train_lr, X_val_lr, y_train_lr, y_val_lr = train_test_split(X_lr, y, test_size=0.3, random_state=17, shuffle=False )
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )<feature_engineering>
y = train_df['target']
X = train_df.drop(['target'], axis=1)
 X_train_df, X_val_df, y_train_df, y_val_df = train_test_split(X, y, test_size =0.3, shuffle=False)
 del train_df
train["SalePrice"] = np.log1p(train["SalePrice"])
 check_skewness('SalePrice' )<prepare_x_and_y>
m1 = Lasso(alpha=0.001, random_state=123 )
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<create_dataframe>
m1_fit = m1.fit(X_train_lr, y_train_lr)
 print('Score reached: {} '.format(m1.score(X_train_lr, y_train_lr)))
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na} )<filter>
X_test_lr = test_df.drop(['id'], axis=1)
 y_test_lasso = m1.predict(X_test_lr )
all_data.PoolQC.loc[all_data.PoolQC.notnull() ]<data_type_conversions>
m2 = Ridge(alpha=0.1 )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )<data_type_conversions>
m2_fit = m2.fit(X_train_lr, y_train_lr)
 print('Score reached: {} '.format(m2.score(X_train_lr, y_train_lr)))
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )<data_type_conversions>
X_test_lr = test_df.drop(['id'], axis=1)
 y_test_ridge = m2.predict(X_test_lr )
all_data["Alley"] = all_data["Alley"].fillna("None" )<data_type_conversions>
lgb_train = lgb.Dataset(X_train_df, y_train_df, free_raw_data=False)
 lgb_eval = lgb.Dataset(X_val_df, y_val_df, free_raw_data=False )
all_data["Fence"] = all_data["Fence"].fillna("None" )<data_type_conversions>
all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )<groupby>
gsLGBM.best_params_
grouped_df = all_data.groupby('Neighborhood')['LotFrontage']
for key, item in grouped_df:
print(key,"
")
 print(grouped_df.get_group(key))
break<categorify>
m3 = lgb.LGBMRegressor(valid_sets = [lgb_train, lgb_eval], verbose_eval = 30, num_boost_round = 10000, early_stopping_rounds = 10, n_jobs=4, **opt_parameters_LGBM)
 m3.fit(X_train_df, y_train_df, eval_set =(X_val_df, y_val_df), eval_metric = 'rmse' )
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
X_test = test_df
y_test_lgbm = m3.predict(X_test )
for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
all_data[col] = all_data[col].fillna('None' )<groupby>
opt_parameters_ADA = {'learning_rate': 0.028555288989857153, 'n_estimators': 36}
abc = ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','GarageYrBlt', 'GarageArea', 'GarageCars']
all_data.groupby('GarageType')[abc].count()<data_type_conversions>
m4 = AdaBoostRegressor(base_estimator=DecisionTreeRegressor(max_depth=3, min_samples_leaf=1, min_impurity_decrease=10, random_state=47), random_state=47, **opt_parameters_ADA)
 m4.fit(X_train_df, y_train_df )
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
X_test = test_df
y_test_ada = m4.predict(X_test )
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
m5 = CatBoostRegressor(random_seed=47 )
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
gsCB.fit(X_train_df, y_train_df)
 print('Best score reached: {} with params: {} '.format(gsCB.best_score_, gsCB.best_params_))
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )<count_values>
X_test = test_df
y_test_cb = m5.predict(X_test )
all_data['MSZoning'].value_counts()<data_type_conversions>
gbm = xgb.XGBRegressor(
 learning_rate = 0.01,
n_estimators= 100,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'reg:squaredlogerror',
nthread= -1,
verbosity=3,
random_state=20)
 lgbm_params = m1.get_params()
lgbm_params["early_stopping_rounds"] = None
m1.set_params(**lgbm_params)
 estimators = [('lgbm', m1),('ada', m2),('lasso', m3),('ridge', m4)]
gbm = StackingRegressor(estimators=estimators, final_estimator=gbm, cv=5, verbose=1 )
all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )<count_values>
all_data['Utilities'].value_counts()<drop_column>
gbm.fit(X_train_df, y_train_df )
all_data = all_data.drop(['Utilities'], axis=1 )<data_type_conversions>
X_test = test_df
y_test_gbm = gbm.predict(X_test )
all_data["Functional"] = all_data["Functional"].fillna("Typ" )<categorify>
lasso_submission = pd.DataFrame({'id': test_df['id'], 'target': y_test_lasso})
 lasso_submission.to_csv('lasso_submission.csv', index=False )
mode_col = ['Electrical','KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType']
for col in mode_col:
all_data[col] = all_data[col].fillna(all_data[col].mode() [0] )<data_type_conversions>
ridge_submission = pd.DataFrame({'id': test_df['id'], 'target': y_test_ridge})
 ridge_submission.to_csv('ridge_submission.csv', index=False )
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )<sort_values>
lgbm_submission = pd.DataFrame({'id': test_df['id'], 'target': y_test_lgbm})
 lgbm_submission.to_csv('lgbm_submission.csv', index=False )
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()<count_values>
ada_submission = pd.DataFrame({'id': test_df['id'], 'target': y_test_ada})
 ada_submission.to_csv('ada_submission.csv', index=False )
<data_type_conversions><EOS> 
gbm_submission = pd.DataFrame({'id': test_df['id'], 'target': y_test_gbm})
 gbm_submission.to_csv('gbm_submission.csv', index=False )