Kaggle competition: House Prices - Advanced Regression Techniques
search.best_score_<train_model>
train_df1=pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test_df1=pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
stacked_reg = search.best_estimator_
stacked_reg.fit(X=train_data, y=y )<predict_on_test>
df1=pd.concat([train_df1,test_df1],axis=0 )
final_pred = scaler.inverse_transform(stacked_reg.predict(test_data))<save_to_csv>
Missing_df=pd.DataFrame(df1.isnull().sum().sort_values(ascending=False)/len(df1))
Missing_df.head(40 )
submission = pd.DataFrame()
submission['Id'] = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')['Id']
submission['SalePrice'] = final_pred
submission.to_csv('House Price Prediction.csv',index=False)
 pd.read_csv('House Price Prediction.csv', index_col=0 ).head(8 )<load_from_csv>
df1.drop(['PoolQC','MiscFeature','Alley','Fence'],axis=1,inplace=True )
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 print(train.head() , "
")
 print(train.info() )<create_dataframe>
for feature in cat_features_list:
df1[feature].fillna(df1[feature].mode() [0],inplace=True)
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
prices = pd.DataFrame({"price":train["SalePrice"], "log(1+price)":np.log1p(train["SalePrice"])})
 prices.hist()<categorify>
num_features_list.remove('SalePrice')
 for feature in num_features_list:
df1[feature].fillna(df1[feature].median() ,inplace=True )
all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean())
 print("all_data.shape: ", all_data.shape, "
")
 print("successfully dummy encoded!" )<prepare_x_and_y>
df1.isnull().sum().sort_values(ascending=False ).head(50 )
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice
del all_data
print(X_train.shape)
 print(X_test.shape )<define_variables>
df1['YearsOld']=df1['YrSold']-df1['YearBuilt']
print(numeric_feats, "
")
 print("number of numeric features: ", len(numeric_feats), "
")
 numeric_data = X_train[numeric_feats]<define_variables>
df1.skew().sort_values(ascending=False )
<features_selection>
skewed_fearures=df1.skew().sort_values(ascending=False ).head(20 ).index.tolist()
skewed_fearures.remove('SalePrice')
 df1[skewed_fearures]=np.log1p(df1[skewed_fearures] )
feature_cols = X_train.columns
selector = SelectKBest(score_func = f_regression, k=10)
 X_new = selector.fit_transform(X_train[feature_cols], y)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index = X_train.index,
columns=feature_cols)
 selected_columns = selected_features.columns[selected_features.var() != 0]
print("selected_columns: ", selected_columns, "
")
 scores = pd.Series(selector.scores_)
 sorted_scores = sorted(scores)[::-1][0:10]
print("sorted_scores: ", sorted_scores, "
")
 sns.barplot(sorted_scores, selected_columns, orient = "h" )<features_selection>
df2=pd.get_dummies(df1[cat_features_list],drop_first=True)
 df1.drop(cat_features_list,axis=1,inplace=True)
selector = SelectKBest(score_func = mutual_info_regression, k=10)
 print("started fitting.....
 ")
 X_new = selector.fit_transform(X_train[feature_cols], y)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index = X_train.index,
columns=feature_cols)
 selected_columns = selected_features.columns[selected_features.var() != 0]
print("selected_columns: ", selected_columns, "
")
 scores = pd.Series(selector.scores_)
 sorted_scores = sorted(scores)[::-1][0:10]
print("sorted_scores: ", sorted_scores, "
")
 sns.barplot(sorted_scores, selected_columns, orient = "h" )<import_modules>
df3=pd.concat([df1,df2],axis=1)
 d3 = df3.loc[:,~df3.columns.duplicated() ]
from IPython.display import display
import eli5
from eli5.sklearn import PermutationImportance<import_modules>
df3.drop(['Id'],axis=1,inplace=True)
 df3.shape
<train_model>
X=df3.drop('SalePrice',axis=1)
 Y=df3['SalePrice']
X_train=X.iloc[:1460]
X_test=X.iloc[1460:,]
Y_train=Y.iloc[:1460]
Y_test=Y.iloc[1460:]
Y_train=np.log1p(Y_train )
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = Ridge().fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))<import_modules>
lr_model=LinearRegression()
lr_model.fit(X_train,Y_train)
 Y_pred_lr=lr_model.predict(X_test)
 Y_lr_train=lr_model.predict(X_train )
<train_model>
MSE=np.sqrt(mean_squared_error(Y_train,Y_lr_train))
print("RMSE of the Linear Regression Model is :",np.sqrt(MSE))
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = Lasso().fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))<train_model>
r2_score=lr_model.score(X_train,Y_train)
 print("R2_score of Liner Regression",r2_score )
print("fitting model...")
 my_model = Lasso(alpha = 0.001 ).fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))<import_modules>
adj_r2 =(1 -(1 - r2_score)*(( X_train.shape[0] - 1)/ 
(X_train.shape[0] - X_train.shape[1] - 1)))
 print("Adjusted R_Sqaured of Linear Regression Model is :",adj_r2 )
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = RandomForestRegressor(n_estimators=100, random_state=1 ).fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))<train_model>
classifier=xgboost.XGBRegressor()
booster=['gbtree','gblinear']
base_score=[0.25,0.5,0.75,1]
<set_options>
n_estimators = [100, 500, 900, 1100, 1500]
max_depth = [2, 3, 5, 10, 15]
booster=['gbtree','gblinear']
learning_rate=[0.05,0.1,0.15,0.20]
min_child_weight=[1,2,3,4]
hyperparameter_grid = {
'n_estimators': n_estimators,
'max_depth':max_depth,
'learning_rate':learning_rate,
'min_child_weight':min_child_weight,
'booster':booster,
'base_score':base_score
}
%matplotlib inline
color = sns.color_palette()
sns.set_style('darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn 
<load_from_csv>
random_cv = RandomizedSearchCV(estimator=classifier,
param_distributions=hyperparameter_grid,
cv=5, n_iter=50,
scoring = 'neg_mean_squared_error',n_jobs = 4,
verbose = 5,
return_train_score = True,
random_state=42 )
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<count_missing_values>
random_cv.fit(X_train,Y_train )
train.isna().sum().sort_values(ascending=False )<sort_values>
random_cv.best_estimator_
train.isna().sum().sort_values(ascending=False)[:20]<sort_values>
regressor=xgboost.XGBRegressor(base_score=0.75, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.15, max_delta_step=0, max_depth=2,
min_child_weight=3, monotone_constraints='() ',
n_estimators=500, n_jobs=0, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None )
( train.isna().sum() / train.shape[0] ).sort_values(ascending=False)[:20]<prepare_x_and_y>
regressor.fit(X_train,Y_train )
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>
Y_pred_xgb=regressor.predict(X_test)
 Y_xgb_train=regressor.predict(X_train )
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})
 missing_data.head(20 )<data_type_conversions>
print("RMSE of XgBosst Model is",np.sqrt(mean_squared_error(Y_train,Y_xgb_train)) )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )<data_type_conversions>
r2_score=lr_model.score(X_train,Y_train)
 print("R2_score of XgBosst Model is ",r2_score )
<data_type_conversions><EOS> 
sub_df=pd.DataFrame(data={'Id':test_df1['Id'].values,'SalePrice':np.exp(Y_pred_xgb)})
 sub_df.to_csv('submission.csv',index=False )