Kaggle competition: House Prices - Advanced Regression Techniques
test_predictions = inv_boxcox1p(test_predictions, lam )<save_to_csv>
train= pd.read_csv('.. /input/train.csv')
 test= pd.read_csv('.. /input/test.csv')
 print("shape of train dataset:", train.shape)
 print("shape of test dataset:", test.shape )
solution = pd.DataFrame({"id": test_ID, "SalePrice": test_predictions})
 solution.to_csv("House_Solutions.csv", index = False )<load_from_csv>
correlation= train.corr()
correlation=correlation['SalePrice'].sort_values(ascending=False)
 pos_correlation=correlation.head(25)
 pos_correlation
warnings.filterwarnings('ignore')
 dataset = pandas.read_csv(".. /input/train.csv")
 dataset_test = pandas.read_csv(".. /input/test.csv")
 ID = dataset_test['id']
dataset_test.drop('id',axis=1,inplace=True)
 pandas.set_option('display.max_rows', None)
 pandas.set_option('display.max_columns', None)
 print(dataset.head(5))<filter>
print("Before:",train.shape)
 train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index, inplace=True)
 train.drop(train[(train['GarageArea']>1200)&(train['SalePrice']<100000)].index, inplace=True)
 train.drop(train[(train['TotalBsmtSF']>6000)&(train['SalePrice']<200000)].index, inplace=True)
 train.reset_index(drop=True, inplace=True)
 print("After:",train.shape )
dataset = dataset.iloc[:,1:]<statistical_test>
data= pd.concat([train.drop(['SalePrice'], axis=1), test] )
def categories(series):
return range(int(series.min()), int(series.max())+ 1)
 def chi_square_of_df_cols(df, col1, col2):
df_col1, df_col2 = df[col1], df[col2]
le1 = LE()
df_col1 = le1.fit_transform(df_col1)
 df_col2 = le1.fit_transform(df_col2)
 result = [[sum(( df_col1 == cat1)&(df_col2 == cat2))
for cat2 in categories(df_col2)]
for cat1 in categories(df_col1)]
return scs.chi2_contingency(result )<statistical_test>
train['GarageQual'].fillna('None', inplace=True)
 test['GarageQual'].fillna('None', inplace=True)
 train['GarageFinish'].fillna('None', inplace=True)
 test['GarageFinish'].fillna('None', inplace=True)
 train['GarageYrBlt'].fillna('None', inplace=True)
 test['GarageYrBlt'].fillna('None', inplace=True)
 train['GarageType'].fillna('None', inplace=True)
 test['GarageType'].fillna('None', inplace=True)
 train['GarageCond'].fillna('None', inplace=True)
 test['GarageCond'].fillna('None', inplace=True)
 test.loc[test['Id']==2577, 'GarageType']='None'
test['GarageCars'].fillna(0, inplace=True)
 test['GarageArea'].fillna(0, inplace=True )
<prepare_x_and_y>
data[(data['TotalBsmtSF']!=0)&(data['BsmtCond'].isnull() ==True)][['Id','TotalBsmtSF','BsmtCond','BsmtQual','BsmtExposure',
'BsmtFinType1','BsmtFinSF1','BsmtUnfSF']]
y = dataset.loss
dataset.drop(["loss"], axis=1, inplace=True )<define_variables>
test.loc[test['Id']==2041, 'BsmtCond']='Gd'
test.loc[test['Id']==2186, 'BsmtCond']='TA'
test.loc[test['Id']==2525, 'BsmtCond']='TA'
train['BsmtCond'].fillna('None', inplace=True)
 test['BsmtCond'].fillna('None', inplace=True )
shapeTrain = dataset.shape<concatenate>
data[(data['TotalBsmtSF']!=0)&(data['BsmtExposure'].isnull() ==True)][['Id','TotalBsmtSF','BsmtCond','BsmtQual','BsmtExposure',
'BsmtFinType1','BsmtFinSF1','BsmtUnfSF']]
data = dataset.append(dataset_test )<count_values>
train.loc[train['Id']==949, 'BsmtExposure']='Gd'
test.loc[test['Id']==1488, 'BsmtExposure']='Gd'
test.loc[test['Id']==2349, 'BsmtExposure']='Gd'
train['BsmtExposure'].fillna('None', inplace=True)
 test['BsmtExposure'].fillna('None', inplace=True )
catCols = data.columns[:116]
WasteCat = []
for col in catCols:
if data[col].value_counts() [0] > 310000:
print(col)
 WasteCat.append(col)
 print(len(WasteCat))<drop_column>
data[(data['TotalBsmtSF']!=0)&(data['BsmtQual'].isnull() ==True)][['Id','TotalBsmtSF','BsmtQual','BsmtCond','BsmtExposure',
'BsmtFinType1','BsmtFinSF1','BsmtUnfSF']]
data.drop(["cont1","cont11"], axis=1, inplace=True )<categorify>
test.loc[test['Id']==2218, 'BsmtQual']='Fa'
test.loc[test['Id']==2219, 'BsmtQual']='TA'
train['BsmtQual'].fillna('None', inplace=True)
 test['BsmtQual'].fillna('None', inplace=True )
catCols = data.columns[:116]
for col in catCols:
print(col)
 data[col] = LE().fit_transform(data[col] )<prepare_output>
data[(data['TotalBsmtSF']!=0)&(data['BsmtFinType1'].isnull() ==True)][['Id','TotalBsmtSF','BsmtQual','BsmtCond','BsmtExposure',
'BsmtFinType1','BsmtFinSF1','BsmtUnfSF']]
y = np.log1p(y )<prepare_x_and_y>
data[(data['TotalBsmtSF']!=0)&(data['BsmtFullBath'].isnull() ==True)][['Id','TotalBsmtSF','BsmtQual','BsmtCond',
'BsmtExposure','BsmtFullBath']]
Xtrain = data.iloc[:shapeTrain[0],:]<choose_model_class>
train['BsmtFinType1'].fillna('None', inplace=True)
 test['BsmtFinType1'].fillna('None', inplace=True)
 test['BsmtFinSF1'].fillna(0, inplace=True)
 train['BsmtFinType2'].fillna('None', inplace=True)
 test['BsmtFinType2'].fillna('None', inplace=True)
 test['BsmtUnfSF'].fillna(0, inplace=True)
 test['TotalBsmtSF'].fillna(0, inplace=True)
 test['BsmtFullBath'].fillna(0, inplace=True)
 test['BsmtHalfBath'].fillna(0, inplace=True)
 test['BsmtFinSF1'].fillna(0, inplace=True)
 test['BsmtFinSF2'].fillna(0, inplace=True )
model3 = XGBR(n_estimators=1000,nthread=4,min_child_weight=1.75)
<split>
train['FireplaceQu'].fillna('None', inplace=True)
 test['FireplaceQu'].fillna('None', inplace=True)
 train.loc[train['LotFrontage'].isnull() ==True, 'LotFrontage']= train['LotFrontage'].mean()
test.loc[test['LotFrontage'].isnull() ==True, 'LotFrontage']= test['LotFrontage'].mean()
train['MasVnrType'].fillna('None', inplace=True)
 test['MasVnrType'].fillna('None', inplace=True)
 train['MasVnrArea'].fillna(0, inplace=True)
 test['MasVnrArea'].fillna(0, inplace=True)
 train['PoolQC'].fillna('None', inplace=True)
 test['PoolQC'].fillna('None', inplace=True)
 train['MiscFeature'].fillna('None', inplace=True)
 test['MiscFeature'].fillna('None', inplace=True)
 train['Alley'].fillna('None', inplace=True)
 test['Alley'].fillna('None', inplace=True)
 train['Fence'].fillna('None', inplace=True)
 test['Fence'].fillna('None', inplace=True)
 train['MSZoning'].fillna('RL', inplace=True)
 test['MSZoning'].fillna('RL', inplace=True)
 train['Electrical'].fillna('SBrkr', inplace=True)
 test['Functional'].fillna('Typ', inplace=True)
 test['SaleType'].fillna('WD', inplace=True)
 test['Exterior1st'].fillna('VinylSd', inplace=True)
 test['KitchenQual'].fillna('TA', inplace=True)
 test['Exterior2nd'].fillna('VinylSd', inplace=True )
X_train,X_test,y_train,y_test = tts(Xtrain,y,test_size=0.2,random_state = 1 )<train_model>
train.drop('Utilities', axis=1, inplace=True)
 test.drop('Utilities', axis=1, inplace=True )
model3.fit(X_train,y_train)
 print("XGB fitted" )<predict_on_test>
print("Train dataset:
",train.isnull().sum().sort_values(ascending=False ).head(5))
print("
Test dataset:
",test.isnull().sum().sort_values(ascending=False ).head(5))
Xtest = data.iloc[shapeTrain[0]:,:]
predTestXGB = model3.predict(X_test)
<compute_test_metric>
train['renovated']= 1
train.loc[(train['YearBuilt']!=train['YearRemodAdd']),'renovated' ]=0
test['renovated']= 1
test.loc[(test['YearBuilt']!=test['YearRemodAdd']),'renovated' ]=0
train['Total_porch']=(train['OpenPorchSF'])+train['ScreenPorch']+train['3SsnPorch']
train['BsmtFinSF_total']=(train['BsmtFinSF1']**2)+train['BsmtFinSF2']
train['Bath']=(train['FullBath']**2)+ train['HalfBath']
train['BsmtBath']=(train['BsmtFullBath'])+train['BsmtHalfBath']
train['TotalSF'] = train['TotalBsmtSF'] + train['1stFlrSF'] + train['2ndFlrSF']
train['Total_garage']=(train['GarageCars']**5)+ train['GarageArea']
test['Total_porch']=(test['OpenPorchSF'])+test['ScreenPorch']+test['3SsnPorch']
test['BsmtFinSF_total']=(test['BsmtFinSF1']**2)+test['BsmtFinSF2']
test['Bath']=(test['FullBath']**2)+ test['HalfBath']
test['BsmtBath']=(test['BsmtFullBath'])+test['BsmtHalfBath']
test['TotalSF'] = test['TotalBsmtSF'] + test['1stFlrSF'] + test['2ndFlrSF']
test['Total_garage']=(test['GarageCars']**5)+ test['GarageArea']
<predict_on_test>
y=np.asarray(train['SalePrice'])
 y=np.log(y+1)
 X_train = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[: train.shape[0]]
X_test = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[train.shape[0]:]
X_train.shape, X_test.shape
y_test = np.expm1(y_test)
<compute_test_metric>
def return_rmse(model):
return np.sqrt(-cross_val_score(model, X_train, y, cv=5, scoring="neg_mean_squared_error")).mean()
for j in range(101):
print("j = " +str(j))
for i in range(j):
predTest =(( predTestRandom)*(j-i)/100 + i*predTestGrad/100 +(100-j)*predTestXGB/100)
 predTest =(np.expm1(predTest))
print("i = " + str(i))
print(mae(y_test,predTest)) 
<compute_test_metric>
RR= Ridge(alpha=15)
 return_rmse(RR )
<predict_on_test>
LSR = Lasso(alpha=0.0005)
 return_rmse(LSR )
predAns =(( model.predict(Xtest)) *(j-i)/100 +(100-j)*model3.predict(Xtest)/100 )<prepare_output>
EN = ElasticNet(alpha=0.01,l1_ratio=0.1)
 return_rmse(EN )
predAns = np.expm1(predAns )<create_dataframe>
RR.fit(X_train, y)
 LSR.fit(X_train, y)
 EN.fit(X_train, y)
 y_pred = RR.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers1=np.where(abs(z)> abs(z ).std() * 3)[0]
outliers1
output = pandas.DataFrame({"id":ID,"loss":predAns} )<save_to_csv>
y_pred = LSR.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers2=np.where(abs(z)> abs(z ).std() * 3)[0]
outliers2
output.to_csv("output.csv",index=False )<load_from_csv>
y_pred = EN.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers3=list(np.where(abs(z)> abs(z ).std() * 3)) [0]
outliers3
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 features = [c for c in train.columns if c not in ['ID_code', 'target']]
target = train['target']
print("Data is ready!" )<count_missing_values>
outliers = []
for i in outliers1:
if(i in outliers2)&(i in outliers3):
outliers.append(i)
 train = train.drop(outliers )
print("Missing data at training")
 train.isnull().values.any()<count_missing_values>
y = train["SalePrice"]
y = np.log(y+1)
 X_train = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[: train.shape[0]]
X_test = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[train.shape[0]:]
print("Missing data at test")
 test.isnull().values.any()<drop_column>
RR= Ridge(alpha=15)
 return_rmse(RR )
train = train.drop(["ID_code", "target"], axis=1 )<set_options>
LSR = Lasso(alpha=0.0004)
 return_rmse(LSR )
sns.set_style('whitegrid')
 sns.countplot(target)
 sns.set_style('whitegrid' )<count_duplicates>
EN = ElasticNet(alpha=0.001,l1_ratio=0.5)
 return_rmse(EN )
<init_hyperparams><EOS> 
RR.fit(X_train, y)
 LSR.fit(X_train, y)
 EN.fit(X_train, y)
 predict = 0.4 * RR.predict(X_test)+ 0.3 * EN.predict(X_test)+ 0.3 * LSR.predict(X_test)
 predict= np.exp(predict)-1
sample_submission= pd.DataFrame({'Id':np.asarray(test.Id), 'SalePrice':predict})
 sample_submission.to_csv("submit.csv", index=False )