Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<data_type_conversions>
!pip install pyod
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 500 )
test_df['FireplaceQu'].fillna('None', inplace=True )<data_type_conversions>
df_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 df_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 df_output = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 df_train['SalePrice'] = df_train['SalePrice'].astype(int )
train_df['LotFrontage'].fillna(data_df['LotFrontage'].median() , inplace=True )<feature_engineering>
tmp2 = pd.get_dummies(df_train)
 scaler2 = StandardScaler()
dummie_df2 = pd.DataFrame(scaler2.fit_transform(tmp2), columns = tmp2.columns)
 X = dummie_df2.loc[:, dummie_df2.columns != 'SalePrice']
Y = dummie_df2.loc[:, dummie_df2.columns == 'SalePrice']
X['LotFrontage'].fillna(60.0, inplace = True)
 X['GarageYrBlt'].fillna(1890.0, inplace = True)
 X['MasVnrArea'].fillna(0, inplace = True)
 tsne = TSNE(n_components = 2, perplexity = 30 ).fit_transform(X)
 result_f = pd.DataFrame(tsne)
 color = pd.qcut(Y['SalePrice'],5,labels=False ).map({0 : 'blue' , 1 : 'violet' , 2: 'deeppink', 3 : 'crimson', 4: 'darkred'})
 plt.figure(figsize=(10,10))
plt.title('TSNE 2D Resizing')
 plt.scatter(result_f[0],result_f[1], s=4, c=color)
 plt.show()
print('Number of missing values for LotFrontage in test set = ', test_df['LotFrontage'].isnull().sum())
 print('Mean = ', test_df['LotFrontage'].mean())
 print('Median = ', test_df['LotFrontage'].median() )<compute_test_metric>
df_test['SalePrice'] = 'test'
df = pd.concat([df_train, df_test])
 df.drop(['Id'], axis=1, inplace = True )
zscore = np.abs(stats.zscore(data_df['LotFrontage']))
thresh = 4
print(np.where(zscore > thresh), zscore[np.where(zscore > thresh)] )<filter>
df_train = df[df['SalePrice'] != 'test']
df_test = df[df['SalePrice'] == 'test']
df_train['SalePrice'] = df_train['SalePrice'].astype(float)
 df_train['SalePrice'] = np.log1p(df_train['SalePrice'])
 df_train['GrLivArea'] = np.log1p(df_train['GrLivArea'])
 df_train.loc[df_train['HasBsmt']==1,'TotalBsmtSF'] = np.log1p(df_train['TotalBsmtSF'])
 df_test['GrLivArea'] = np.log1p(df_test['GrLivArea'])
 df_test.loc[df_test['HasBsmt']==1,'TotalBsmtSF'] = np.log1p(df_test['TotalBsmtSF'] )
data_df[data_df['LotFrontage'] > 300]<define_variables>
scaler1 = StandardScaler()
df_test.drop(['SalePrice'], axis = 1, inplace = True)
 dum = pd.get_dummies(df_train, drop_first = True)
 dum_test = pd.get_dummies(df_test, drop_first = True)
 dum1 = dum.copy()
dum_test1 = dum_test.copy()
col = []
for i in dum.columns:
if i not in dum_test.columns :
col.append(i)
 for i in dum_test.columns:
if i not in dum.columns :
col.append(i)
 for i in dum1.columns:
if i in col :
dum1.drop(i, axis = 1, inplace = True)
 for i in dum_test1.columns:
if i in col :
dum_test1.drop(i, axis = 1, inplace = True )
outliers.update([outlier for outlier in list(np.where(data_df['LotFrontage'] > 300)[0])if outlier not in outliers])
 print('Running list of outliers: ', outliers )<count_missing_values>
x = scaler1.fit_transform(dum1)
 knn = KNN()
outliers = knn.fit_predict(x )
len(data_df[(data_df['GarageArea']!=0)&(( data_df['GarageCond'].isnull())| 
(data_df['GarageType'].isnull())| 
(data_df['GarageYrBlt'].isnull())| 
(data_df['GarageFinish'].isnull())| 
(data_df['GarageQual'].isnull())) ] )<data_type_conversions>
dum1['outliers_flag'] = outliers
dum1['SalePrice'] = df_train['SalePrice']
final_outlier = dum1[dum1.outliers_flag != 1]
X = final_outlier.loc[:, final_outlier.columns != 'outliers_flag']
X = X.loc[:, X.columns != 'SalePrice']
y = final_outlier['SalePrice']
pca = PCA(n_components=2)
 X['pca1'] = pd.DataFrame(pca.fit_transform(pd.DataFrame(X)))[0].tolist()
X['pca2'] = pd.DataFrame(pca.fit_transform(pd.DataFrame(X)))[1].tolist()
train_df['GarageCond'].fillna('None', inplace=True)
 train_df['GarageType'].fillna('None', inplace=True)
 train_df['GarageYrBlt'].fillna('None', inplace=True)
 train_df['GarageFinish'].fillna('None', inplace=True)
 train_df['GarageQual'].fillna('None', inplace=True )<count_missing_values>
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(X)
 rmse= np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv = kf))
return(rmse)
 model_xgb = make_pipeline(RobustScaler() ,xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1))
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
KRR = make_pipeline(RobustScaler() ,KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5))
GBoost = make_pipeline(RobustScaler() ,GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5))
RFR = make_pipeline(RobustScaler() ,RandomForestRegressor(random_state = 1))
Ridge = make_pipeline(RobustScaler() ,Ridge(alpha = 17))
lr = make_pipeline(RobustScaler() ,LinearRegression())
 ex_reg=make_pipeline(RobustScaler() ,ExtraTreesRegressor(n_estimators=2000, max_depth=20))
svr = make_pipeline(RobustScaler() ,SVR(kernel='rbf',C=20000,gamma=0.045))
print('Number of missing values in garage area = ', test_df['GarageArea'].isnull().sum() )<filter>
scores = {}
score = rmsle_cv(model_xgb)
 scores['Xgboost'] = score.mean()
score = rmsle_cv(lasso)
 scores['Lasso'] = score.mean()
score = rmsle_cv(ENet)
 scores['ElasticNet'] = score.mean()
score = rmsle_cv(KRR)
 scores['Kernel Ridge'] = score.mean()
score = rmsle_cv(GBoost)
 scores['Gradient Boosting'] = score.mean()
score = rmsle_cv(RFR)
 scores['Random Forest'] = score.mean()
score = rmsle_cv(Ridge)
 scores['Ridge'] = score.mean()
score = rmsle_cv(lr)
 scores['lr'] = score.mean()
score = rmsle_cv(ex_reg)
 scores['svr'] = score.mean()
score = rmsle_cv(svr)
 scores['svr'] = score.mean()
test_df[(test_df['GarageArea']!=0)&(( test_df['GarageType'].isnull())| 
(test_df['GarageYrBlt'].isnull())| 
(test_df['GarageFinish'].isnull())| 
(test_df['GarageQual'].isnull())
)].loc[:, ['GarageArea', 'GarageType', 'GarageYrBlt', 'GarageFinish', 'GarageQual']]<data_type_conversions>
sorted(scores.items() , key=operator.itemgetter(1))
test_df['GarageArea'].fillna(test_df['GarageArea'].median() , inplace=True )<count_values>
vote_mod = make_pipeline(RobustScaler() ,VotingRegressor([('Ridge', Ridge),('Lasso', lasso),('Elastic', ENet),('Xgboost', model_xgb)]))
score = rmsle_cv(vote_mod)
 print("vote_mod score: {:.4f}
".format(score.mean()))
test_df['GarageFinish'].value_counts()<count_values>
stregr = make_pipeline(RobustScaler() ,StackingRegressor(regressors=[ENet,Ridge, lasso, vote_mod],
meta_regressor=model_xgb, use_features_in_secondary=True))
score = rmsle_cv(stregr)
 print("stregr score: {:.4f}
".format(score.mean()))
test_df['GarageQual'].value_counts()<feature_engineering>
stregr.fit(X, y)
 vote_mod.fit(X, y)
 pca = PCA(n_components=2)
 dum_test1['pca1'] = pd.DataFrame(pca.fit_transform(pd.DataFrame(dum_test1)))[0].tolist()
dum_test1['pca2'] = pd.DataFrame(pca.fit_transform(pd.DataFrame(dum_test1)))[1].tolist()
stack_predict = stregr.predict(dum_test1)
 vote_pred = vote_mod.predict(dum_test1 )
<data_type_conversions><EOS> 
df_output['SalePrice'] =(2/3)* np.expm1(vote_pred)+(1/3)* np.expm1(stack_predict)
 q1 = df_output['SalePrice'].quantile(0.0045)
 q2 = df_output['SalePrice'].quantile(0.99)
 df_output['SalePrice'] = df_output['SalePrice'].apply(lambda x: x if x > q1 else x*0.77)
 df_output['SalePrice'] = df_output['SalePrice'].apply(lambda x: x if x < q2 else x*1.1)
 filename = 'submission.csv'
df_output.to_csv(filename,index=False)
 print('Saved file: ' + filename )