Kaggle competition: House Prices - Advanced Regression Techniques
warnings.filterwarnings("ignore" )<load_from_csv>
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 = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )<set_options>
data_size_train = train.memory_usage().sum() / 1024 / 1024
print("Data memory size: %.2f MB" % data_size_train )
pd.set_option("display.max_columns",81 )<sort_values>
data_size_test = test.memory_usage().sum() / 1024 / 1024
print("Data memory size: %.2f MB" % data_size_test )
corr["SalePrice"].sort_values(ascending=False )<prepare_x_and_y>
train = train.drop(["PoolQC", "MiscFeature", "Alley", "Fence", "Street", "Alley"], axis = 1)
 none_values_train = ["FireplaceQu", "GarageCond", "GarageType", "GarageFinish", "GarageQual",
"BsmtExposure", "BsmtFinType2", "BsmtFinType1", "BsmtCond", "BsmtQual",
"MasVnrType", "Electrical"]
for value in none_values_train:
train = train.fillna({value : "None"})
 zero_values_train = ["LotFrontage", "GarageYrBlt", "MasVnrArea"]
for value in zero_values_train:
train = train.fillna({value : 0} )
index = len(train)
 y=train["SalePrice"]
train.drop("SalePrice",axis=1,inplace=True)
 dataset = pd.concat([train,test] ).reset_index(drop=True )<sort_values>
test = test.drop(["PoolQC", "MiscFeature", "Alley", "Fence", "Street", "Alley"], axis = 1)
 none_values_test = ["FireplaceQu", "GarageCond", "GarageType", "GarageFinish", "GarageQual",
"BsmtExposure", "BsmtFinType2", "BsmtFinType1", "BsmtCond", "BsmtQual",
"MasVnrType", "Electrical", "MSZoning", "Utilities", "Functional",
"Exterior2nd", "SaleType", "Exterior1st", "KitchenQual"]
for value in none_values_test:
test = test.fillna({value : "None"})
 zero_values_test = ["LotFrontage", "GarageYrBlt", "MasVnrArea", "BsmtHalfBath", "BsmtFullBath",
"BsmtFinSF2", "BsmtFinSF1", "TotalBsmtSF", "GarageArea", "GarageCars", "BsmtUnfSF"]
for value in zero_values_test:
test = test.fillna({value : 0} )
dataset.isnull().sum() [dataset.isnull().sum() >0].sort_values(ascending=False )<categorify>
train = train.drop(train[train["GrLivArea"] > 4000].index )
df_mszon=dataset.groupby(["Neighborhood","MSZoning"])["Id"].count().reset_index().groupby(["Neighborhood"])
 maximum_mszon = df_mszon.max()
maximum_mszon= maximum_mszon.drop("Id",axis=1)
 max_dict_mszon = maximum_mszon.to_dict()
def mapper_mszon(x):
for index,val in zip(max_dict_mszon,max_dict_mszon.values()):
for index1,val1 in val.items() :
if(x==index1):
return val1
dataset.loc[dataset["MSZoning"].isnull() ,"MSZoning"] = dataset.loc[dataset["MSZoning"].isnull() ,"Neighborhood"].apply(lambda x: mapper_mszon(x))<categorify>
upper = corr.where(np.triu(np.ones(corr.shape), k = 1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(upper[column] > 0.90)]
print(to_drop )
df_lotf=dataset.groupby(["Neighborhood","LotConfig"])["LotFrontage"].mean()
max_dict_lotf = df_lotf.to_dict()
def mapper_lotf(x1,x2):
for index,val in zip(max_dict_lotf,max_dict_lotf.values()):
if(( x1==index[0])&(x2==index[1])) :
return val
dataset.loc[dataset["LotFrontage"].isnull() ,"LotFrontage"] = dataset.loc[dataset["LotFrontage"].isnull() ,["Neighborhood","LotConfig"]].apply(lambda x: mapper_lotf(x[0],x[1]),axis=1)
 dataset["LotFrontage"] = dataset.groupby(["Neighborhood"])["LotFrontage"].transform(lambda x: x.median() )<data_type_conversions>
from sklearn.linear_model import Ridge, Lasso, LinearRegression
from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor, StackingRegressor
from sklearn.model_selection import GridSearchCV, train_test_split, cross_val_score, learning_curve
from sklearn.preprocessing import StandardScaler, RobustScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor
from catboost import CatBoostRegressor
import lightgbm as lgb
dataset["Alley"].fillna("NA",inplace=True)
 dataset["Utilities"].fillna("NA",inplace=True)
 dataset["Exterior1st"].fillna("NA",inplace=True)
 dataset["Exterior2nd"].fillna("NA",inplace=True)
 dataset["MasVnrType"].fillna("NA",inplace=True)
 dataset["MasVnrArea"].fillna(0,inplace=True)
 dataset["BsmtQual"].fillna("NA",inplace=True)
 dataset["BsmtCond"].fillna("NA",inplace=True)
 dataset["BsmtExposure"].fillna("No",inplace=True)
 dataset["BsmtFinType1"].fillna("NA",inplace=True)
 dataset["BsmtFinSF1"].fillna(0,inplace=True)
 dataset["BsmtFinType2"].fillna("NA",inplace=True)
 dataset["BsmtFinSF2"].fillna(0,inplace=True)
 dataset["BsmtUnfSF"].fillna(0,inplace=True)
 dataset["TotalBsmtSF"].fillna(0,inplace=True)
 dataset["BsmtFullBath"].fillna(0,inplace=True)
 dataset["BsmtHalfBath"].fillna(0,inplace=True)
 dataset["FireplaceQu"].fillna("NA",inplace=True)
 dataset["GarageType"].fillna("NA",inplace=True)
 dataset["GarageYrBlt"].fillna(0,inplace=True)
 dataset["GarageFinish"].fillna("NA",inplace=True)
 dataset["GarageCars"].fillna(0,inplace=True)
 dataset["GarageArea"].fillna(0,inplace=True)
 dataset["GarageQual"].fillna("NA",inplace=True)
 dataset["GarageCond"].fillna("NA",inplace=True)
 dataset["PoolQC"].fillna("NA",inplace=True)
 dataset["Fence"].fillna("NA",inplace=True)
 dataset["MiscFeature"].fillna("NA",inplace=True )<categorify>
all_data = pd.concat([train.drop("SalePrice", axis = 1), test])
 all_data = pd.get_dummies(all_data)
 train_dummy = all_data[0:1456]
test_dummy = all_data[1456:2915]
train_dummy["SalePrice"] = train["SalePrice"]
X = train_dummy.drop(["SalePrice", "Id"], axis = 1 ).values
Y = train_dummy["SalePrice"].values
dataset["Functional"].fillna("Typ",inplace=True)
 dataset["Electrical"].fillna(dataset["Electrical"].mode() [0],inplace=True)
 dataset["KitchenQual"].fillna(dataset["KitchenQual"].mode() [0],inplace=True)
 dataset["SaleType"].fillna("Oth",inplace=True )<data_type_conversions>
scaler = StandardScaler()
X = scaler.fit_transform(X)
 test_dummy = test_dummy.drop("Id", axis = 1)
 test_dummy = scaler.fit_transform(test_dummy )
dataset["MSSubClass"]=dataset["MSSubClass"].astype("category" )<prepare_output>
pipe_cat = Pipeline([ 
("scaler", StandardScaler()), 
("cat", CatBoostRegressor(verbose = 0)) ])
 params_cat = {
"cat__depth" : [3, 4, 5],
"cat__learning_rate" : [0.001, 0.01, 0.1, 1],
"cat__n_estimators" : [300, 400, 500]}
model_cat = CatBoostRegressor(verbose = 0,
depth = 5,
learning_rate = 0.1,
n_estimators = 500)
 model_cat.fit(X, Y)
 model_score = cross_val_score(model_cat, X, Y, cv = 5, n_jobs = -1)
 Y_preds = model_cat.predict(X)
 print("R2 : " + str(model_score.mean()))
print("RMSE : " + str(np.sqrt(mean_squared_error(Y, Y_preds))))
y= np.log1p(y )<categorify>
pipe_gbm = Pipeline([ 
("gbm", lgb.LGBMRegressor())])
 params_gbm = {
"gbm__n_estimators" : [300, 500, 700],
"gbm__learning_rate" : [0.001, 0.01, 0.1],
"gbm__max_depth" : [3, 6, 9],
"gbm__max_bin" : [32, 64, 128, 256, 512]}
model_gbm = lgb.LGBMRegressor(verbose = 0,
learning_rate = 0.1,
max_bin = 32,
max_depth = 4,
n_estimators = 500)
 model_gbm.fit(X, Y)
 model_score = cross_val_score(model_gbm, X, Y, cv = 5, n_jobs = -1)
 Y_preds = model_gbm.predict(X)
 print("R2 : " + str(model_score.mean()))
print("RMSE : " + str(np.sqrt(mean_squared_error(Y, Y_preds))))
dataset = dataset.replace({"Alley" : {"Grvl" : 1, "Pave" : 2,"NA":0},
"BsmtCond" : {"NA" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"No" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtQual" : {"NA" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"NA" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5,
"Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageCond" : {"NA" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"NA" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageFinish" : {"Fin" : 3, "RFn" : 2, "Unf" : 1, "NA" : 0},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"PoolQC" : {"NA" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3, "Ex" : 4},
"Street" : {"Grvl" : 1, "Pave" : 2, "NA":0},
"Utilities" : {"ELO" : 1, "NASeWa" : 2, "NASewr" : 3, "AllPub" : 4}}
)<feature_engineering>
estimators = [("CatBoost", model_cat), 
("LightGBM", model_gbm)]
stacker = StackingRegressor(estimators = estimators)
 stacker.fit(X, Y)
 stacker_score = cross_val_score(stacker, X, Y, cv = 5, n_jobs = -1)
 Y_pred_stack = stacker.predict(X)
 Y_preds_sub = stacker.predict(test_dummy)
 print("R2 : " + str(stacker_score.mean()))
print("RMSE : " + str(np.sqrt(mean_squared_error(Y, Y_pred_stack))))
<feature_engineering><EOS> 
submission = pd.DataFrame({"Id" : test.Id, "SalePrice" : Y_preds_sub})
 submission.to_csv("submission_reg.csv", index = False)
 submission.head()