Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<predict_on_test>
data=pd.read_csv('.. /input/train.csv')
 anwsering_this=pd.read_csv('.. /input/test.csv')
 submission_id=anwsering_this.Id.values
probabilities = model.predict(testing_padded2 )<data_type_conversions>
if len(set(data.Id)) ==len(data.Id):
print("No duplicates for Id column")
 data.drop("Id", axis = 1, inplace = True)
predictions =(probabilities > 0.5 ).astype(int)
 predictions = np.ndarray.flatten(predictions)
 pd.value_counts(predictions )<save_to_csv>
sep=data.shape[0]
data_fts=data.drop(['SalePrice'],axis=1)
 test_fts=anwsering_this.drop(['Id'],axis=1)
 all_instances=pd.concat([data_fts,test_fts] ).reset_index(drop=True)
 null_counts=all_instances.isnull().sum().sort_values(ascending=False)
 print(null_counts.head(38))
original_test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 df = pd.DataFrame({'text' : original_test_df['text'],'cleaned_text' : test_df['text'], 'prediction' : predictions,'probabilities' : np.ndarray.flatten(probabilities)})
 df.to_csv("test_df.csv", index=False )<save_to_csv>
all_instances.loc[:,'PoolQC']=all_instances.loc[:,'PoolQC'].fillna('No')
 all_instances.loc[:,'MiscFeature']=all_instances.loc[:,'MiscFeature'].fillna('No')
 all_instances.loc[:,'Alley']=all_instances.loc[:,'Alley'].fillna('No')
 all_instances.loc[:,'Fence']=all_instances.loc[:,'Fence'].fillna('No')
 all_instances.loc[:,'FireplaceQu']=all_instances.loc[:,'FireplaceQu'].fillna('No')
 all_instances.loc[:,'LotFrontage']=all_instances.loc[:,'LotFrontage'].fillna(0)
 all_instances.loc[:,'GarageCond']=all_instances.loc[:,'GarageCond'].fillna('No')
 all_instances.loc[:,'GarageQual']=all_instances.loc[:,'GarageQual'].fillna('No')
 all_instances.loc[:,'GarageYrBlt']=all_instances.loc[:,'GarageYrBlt'].fillna(0)
 all_instances.loc[:,'GarageFinish']=all_instances.loc[:,'GarageFinish'].fillna('No')
 all_instances.loc[:,'GarageType']=all_instances.loc[:,'GarageType'].fillna('No')
 all_instances.loc[:,'BsmtCond']=all_instances.loc[:,'BsmtCond'].fillna('No')
 all_instances.loc[:,'BsmtExposure']=all_instances.loc[:,'BsmtExposure'].fillna('No_B')
 all_instances.loc[:,'BsmtQual']=all_instances.loc[:,'BsmtQual'].fillna('No')
 all_instances.loc[:,'BsmtFinType2']=all_instances.loc[:,'BsmtFinType2'].fillna('No')
 all_instances.loc[:,'BsmtFinType1']=all_instances.loc[:,'BsmtFinType1'].fillna('No')
 all_instances.loc[:,'MasVnrType']=all_instances.loc[:,'MasVnrType'].fillna('No')
 all_instances.loc[:,'MasVnrArea']=all_instances.loc[:,'MasVnrArea'].fillna(0)
 all_instances.loc[:,'MSZoning']=all_instances.loc[:,'MSZoning'].fillna("RL")
 all_instances.loc[:,'BsmtHalfBath']=all_instances.loc[:,'BsmtHalfBath'].fillna(0)
 all_instances.loc[:,'Utilities']=all_instances.loc[:,'Utilities'].fillna("AllPub")
 all_instances.loc[:,'Functional']=all_instances.loc[:,'Functional'].fillna("Typ")
 all_instances.loc[:,'BsmtFullBath']=all_instances.loc[:,'BsmtFullBath'].fillna(0)
 all_instances.loc[:,'BsmtFinSF2']=all_instances.loc[:,'BsmtFinSF2'].fillna(0)
 all_instances.loc[:,'BsmtFinSF1']=all_instances.loc[:,'BsmtFinSF1'].fillna(0)
 all_instances.loc[:,'Exterior2nd']=all_instances.loc[:,'Exterior2nd'].fillna("VinylSd")
 all_instances.loc[:,'BsmtUnfSF']=all_instances.loc[:,'BsmtUnfSF'].fillna(0)
 all_instances.loc[:,'TotalBsmtSF']=all_instances.loc[:,'TotalBsmtSF'].fillna(0)
 all_instances.loc[:,'Exterior1st']=all_instances.loc[:,'Exterior1st'].fillna("VinylSd")
 all_instances.loc[:,'SaleType']=all_instances.loc[:,'SaleType'].fillna("WD")
 all_instances.loc[:,'Electrical']=all_instances.loc[:,'Electrical'].fillna("SBrkr")
 all_instances.loc[:,'KitchenQual']=all_instances.loc[:,'KitchenQual'].fillna("TA")
 all_instances.loc[:,'GarageArea']=all_instances.loc[:,'GarageArea'].fillna(0)
 all_instances.loc[:,'GarageCars']=all_instances.loc[:,'GarageCars'].fillna(0)
 print("Remain "+str(all_instances.isnull().sum().sum())+" missing values.")
sample_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sample_submission["target"] = predictions
sample_submission.to_csv("submission.csv", index=False )<import_modules>
n2c=['MSSubClass','MoSold']
for i in n2c:
all_instances[i]=all_instances[i].astype(str)
 c2n_=['GarageCond','GarageQual','GarageFinish','FireplaceQu','KitchenQual','HeatingQC','BsmtFinType2','BsmtFinType1','BsmtExposure','BsmtCond','BsmtQual','ExterCond','ExterQual','Fence']
lv4_NA=['PoolQC']
lv5_full=['ExterQual','ExterCond','HeatingQC','KitchenQual']
lv5_NA=['BsmtQual','BsmtCond','FireplaceQu','GarageQual','GarageCond']
Bsmt_Type=['BsmtFinType2','BsmtFinType1']
all_instances['PoolQC']=all_instances['PoolQC'].replace({'No':0,'Fa':1,'TA':2,'Gd':3,'Ex':4})
 for i in lv5_full:
all_instances[i]=all_instances[i].replace({'Po':1,'Fa':2,'TA':3,'Gd':4,'Ex':5})
 for i in lv5_NA:
all_instances[i]=all_instances[i].replace({'No':0,'Po':1,'Fa':2,'TA':3,'Gd':4,'Ex':5})
 for i in Bsmt_Type:
all_instances[i]=all_instances[i].replace({'No':0,'Unf':1,'LwQ':2,'Rec':3,'BLQ':4,'ALQ':5,'GLQ':6})
 all_instances['GarageFinish']=all_instances['GarageFinish'].replace({'No':0,'Unf':1,'RFn':2,'Fin':3})
 all_instances['BsmtExposure']=all_instances['BsmtExposure'].replace({'No_B':0,'No':1,'Mn':2,'Av':3,'Gd':4})
 all_instances['Fence']=all_instances['Fence'].replace({'No':0,'MnWw':1,'GdWo':2,'MnPrv':3,'GdPrv':4})
import tensorflow_hub as hub
import lightgbm as lgb
from lightgbm import LGBMClassifier<load_pretrained>
all_instances["OverallRate"] = all_instances["OverallQual"] * all_instances["OverallCond"]
all_instances["GarageRate"] = all_instances["GarageQual"] * all_instances["GarageCond"]
all_instances["ExterRate"] = all_instances["ExterQual"] * all_instances["ExterCond"]
all_instances["BsmtRate"]=all_instances["BsmtQual"] * all_instances["BsmtCond"]
all_instances["PoolRate"]=all_instances["PoolQC"] * all_instances["PoolArea"]
top_10_corr=list(corr['SalePrice'].sort_values(ascending=False ).index)[1:11]
for i in top_10_corr:
all_instances[i+'_s2']=all_instances[i] ** 2
all_instances[i+'_s3']=all_instances[i] ** 3
all_instances[i+'_sqrt']=np.sqrt(all_instances[i])
 print(all_instances.shape )
module_url = "https://tfhub.dev/google/nnlm-en-dim128/2"
embed = hub.KerasLayer(module_url)
 embeddings = embed(["A long sentence.", "single-word",
"http://example.com"])
 print(embeddings.shape )<load_pretrained>
categorical_features = all_instances.select_dtypes(include = ["object"] ).columns
numerical_features = all_instances.select_dtypes(exclude = ["object"] ).columns
print("Numerical features : " + str(len(numerical_features)))
 print("Categorical features : " + str(len(categorical_features)))
 all_instances_num = all_instances[numerical_features]
all_instances_cat = all_instances[categorical_features]
skewness = all_instances_num.apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.5]
print(str(skewness.shape[0])+ " skewed numerical features to log transform")
 skewed_features = skewness.index
all_instances_num[skewed_features] = np.log1p(all_instances_num[skewed_features])
 all_instances = pd.concat([all_instances_num, all_instances_cat], axis = 1)
 print(all_instances.shape)
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/3" )<categorify>
all_instances=pd.get_dummies(all_instances,drop_first=True)
 print(all_instances.shape )
X_train_embeddings = embed(train_df.text.values)
 X_test_embeddings = embed(test_df.text.values )<init_hyperparams>
new_data=all_instances[:sep]
y=data['SalePrice']
new_test=all_instances[sep:]
params = {
'learning_rate': 0.04,
'n_estimators': 1500,
'colsample_bytree': 0.4,
'metric':'auc'
}<choose_model_class>
X_train,X_test,y_train,y_test=train_test_split(new_data,y,test_size=0.3,random_state=111)
 stdSc = StandardScaler()
X_train.loc[:, numerical_features] = stdSc.fit_transform(X_train.loc[:, numerical_features])
 X_test.loc[:, numerical_features] = stdSc.transform(X_test.loc[:, numerical_features])
text_clf = LGBMClassifier(**params )<train_model>
scorer = make_scorer(mean_squared_error, greater_is_better = False)
 def rmse_cv_train(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y_train, scoring = scorer, cv = 10))
return(rmse)
 def rmse_cv_test(model):
rmse= np.sqrt(-cross_val_score(model, X_test, y_test, scoring = scorer, cv = 10))
return(rmse)
text_clf.fit(X_train_embeddings['outputs'][:5000,:], train_df.target.values[:5000],
eval_set=[(X_train_embeddings['outputs'][:5000,:], train_df.target.values[:5000]), 
(X_train_embeddings['outputs'][5000:,:], train_df.target.values[5000:])],
verbose=200, early_stopping_rounds=20, 
)
<predict_on_test>
lr = LinearRegression()
print("RMSE on Training set :", rmse_cv_train(lr ).mean())
 print("RMSE on Test set :", rmse_cv_test(lr ).mean())
text_clf.fit(X_train_embeddings['outputs'][:5000,:], train_df.target.values[:5000])
 Y_pred = text_clf.predict(X_train_embeddings['outputs'][5000:] )<compute_train_metric>
ridge = RidgeCV(alphas = [0.01, 0.04, 0.08, 0.1, 0.4, 0.8, 1, 4, 8, 10, 40, 80])
 ridge.fit(X_train, y_train)
 alpha= ridge.alpha_
print("Best alpha :", alpha)
 print("Try again for more precision with alphas centered around " + str(alpha))
ridge = RidgeCV(alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8, alpha *.85,
alpha *.9, alpha *.95, alpha, alpha * 1.05, alpha * 1.1, alpha * 1.15,
alpha * 1.25, alpha * 1.3, alpha * 1.35, alpha * 1.4],
cv = 10)
 ridge.fit(X_train, y_train)
 alpha_ridge = ridge.alpha_
print("Best alpha :", alpha_ridge)
 print("Ridge RMSE on Training set :", rmse_cv_train(ridge ).mean())
 print("Ridge RMSE on Test set :", rmse_cv_test(ridge ).mean())
 ridge_=Ridge(alpha=alpha_ridge)
 ridge_.fit(X_train,y_train)
 ridge_predict=ridge_.predict(X_test)
 ridge_RMSE=np.sqrt(mean_squared_error(ridge_predict,y_test))
print("Ridge regression RMSE :",ridge_RMSE)
print(metrics.classification_report(train_df.target[5000:], Y_pred, digits=3),)
 print(metrics.confusion_matrix(train_df.target[5000:], Y_pred))<predict_on_test>
lasso = LassoCV(alphas = [0.0001, 0.0004, 0.0008, 0.001, 0.004, 0.008, 0.01, 0.04, 0.08, 0.1,
0.4, 0.8, 1],
max_iter = 50000, cv = 10)
 lasso.fit(X_train, y_train)
 alpha = lasso.alpha_
print("Best alpha :", alpha)
 print("Try again for more precision with alphas centered around " + str(alpha))
lasso = LassoCV(alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8,
alpha *.85, alpha *.9, alpha *.95, alpha, alpha * 1.05,
alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3, alpha * 1.35,
alpha * 1.4],
max_iter = 50000, cv = 10)
 lasso.fit(X_train, y_train)
 alpha_lasso = lasso.alpha_
print("Best alpha :", alpha_lasso)
 print("Lasso RMSE on Training set :", rmse_cv_train(lasso ).mean())
 print("Lasso RMSE on Test set :", rmse_cv_test(lasso ).mean())
 lasso_=Lasso(alpha=alpha_lasso)
 lasso_.fit(X_train,y_train)
 lasso_predict=lasso_.predict(X_test)
 lasso_RMSE=np.sqrt(mean_squared_error(lasso_predict,y_test))
print("Lasso regression RMSE :",lasso_RMSE )
text_clf.fit(X_train_embeddings['outputs'], train_df.target.values)
 pred_test = text_clf.predict(X_test_embeddings['outputs'] )<prepare_output>
elasticNet = ElasticNetCV(l1_ratio = [0.1, 0.3, 0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95, 1],
alphas = [0.0001, 0.0003, 0.0006, 0.001, 0.003, 0.006,
0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1, 3, 6],
max_iter = 50000, cv = 10)
 elasticNet.fit(X_train, y_train)
 alpha = elasticNet.alpha_
ratio = elasticNet.l1_ratio_
print("Best l1_ratio :", ratio)
 print("Best alpha :", alpha)
 print("Try again for more precision with l1_ratio centered around " + str(ratio))
elasticNet = ElasticNetCV(l1_ratio = [ratio *.85, ratio *.9, ratio *.95, ratio, ratio * 1.05, ratio * 1.1, ratio * 1.15],
alphas = [0.0001, 0.0003, 0.0006, 0.001, 0.003, 0.006, 0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1, 3, 6],
max_iter = 50000, cv = 10)
 elasticNet.fit(X_train, y_train)
 if(elasticNet.l1_ratio_ > 1):
elasticNet.l1_ratio_ = 1
alpha = elasticNet.alpha_
ratio = elasticNet.l1_ratio_
print("Best l1_ratio :", ratio)
 print("Best alpha :", alpha)
 print("Now try again for more precision on alpha, with l1_ratio fixed at " + str(ratio)+
" and alpha centered around " + str(alpha))
elasticNet = ElasticNetCV(l1_ratio = ratio,
alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8, alpha *.85, alpha *.9,
alpha *.95, alpha, alpha * 1.05, alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3,
alpha * 1.35, alpha * 1.4],
max_iter = 50000, cv = 10)
 elasticNet.fit(X_train, y_train)
 if(elasticNet.l1_ratio_ > 1):
elasticNet.l1_ratio_ = 1
alpha_eln = elasticNet.alpha_
ratio_eln = elasticNet.l1_ratio_
print("Best l1_ratio :", ratio_eln)
 print("Best alpha :", alpha_eln)
 print("ElasticNet RMSE on Training set :", rmse_cv_train(elasticNet ).mean())
 print("ElasticNet RMSE on Test set :", rmse_cv_test(elasticNet ).mean())
 elasticNet_=ElasticNet(l1_ratio=ratio_eln,alpha=alpha_eln)
 elasticNet_.fit(X_train,y_train)
 elasticNet_predict=elasticNet_.predict(X_test)
 elasticNet_RMSE=np.sqrt(mean_squared_error(elasticNet_predict,y_test))
print("elasticNet RMSE :",elasticNet_RMSE )
<save_to_csv><EOS> 
Final_model=Ridge(alpha=alpha_ridge)
 stdSc_ = StandardScaler()
new_data.loc[:, numerical_features] = stdSc_.fit_transform(new_data.loc[:, numerical_features])
 new_test.loc[:, numerical_features] = stdSc_.transform(new_test.loc[:, numerical_features])
 Final_model.fit(new_data,y)
 submission=Final_model.predict(new_test)
 submission=np.expm1(submission)
 df_submission=pd.DataFrame({'Id':submission_id,'SalePrice':submission})
 df_submission.to_csv('submission.csv',index=False )