Kaggle competition: House Prices - Advanced Regression Techniques
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features )<compute_train_metric>
%reload_ext autoreload
%autoreload 2
path = Path('/kaggle/input/house-prices-advanced-regression-techniques/')
 path.ls()
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))<compute_test_metric>
train_df = pd.read_csv(path/'train.csv')
 train_df.head()
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred)) 
<predict_on_test>
n = len(train_df); n
stacked_averaged_models.fit(train.values, y_train)
 stacked_train_pred = stacked_averaged_models.predict(train.values)
 stacked_pred = np.expm1(stacked_averaged_models.predict(test.values))
print(rmsle(y_train, stacked_train_pred))<predict_on_test>
idx = np.random.permutation(range(n)) [:200]
idx.sort()
small_train_df = train_df.iloc[idx[:100]]
small_test_df = train_df.iloc[idx[100:]]
small_cont_vars = ['YearBuilt', '1stFlrSF', '2ndFlrSF']
small_cat_vars = ['Neighborhood', 'Exterior1st']
small_train_df = small_train_df[small_cat_vars + small_cont_vars + ['SalePrice']]
small_test_df = small_test_df[small_cat_vars + small_cont_vars + ['SalePrice']]
model_xgb.fit(train, y_train)
 xgb_train_pred = model_xgb.predict(train)
 xgb_pred = np.expm1(model_xgb.predict(test))
print(rmsle(y_train, xgb_train_pred))<predict_on_test>
categorify = Categorify(small_cat_vars, small_cont_vars)
 categorify(small_train_df)
 categorify(small_test_df, test=True )
model_lgb.fit(train, y_train)
 lgb_train_pred = model_lgb.predict(train)
 lgb_pred = np.expm1(model_lgb.predict(test.values))
print(rmsle(y_train, lgb_train_pred)) 
<compute_test_metric>
fill_missing = FillMissing(small_cat_vars, small_cont_vars)
 fill_missing(small_train_df)
 fill_missing(small_test_df, test=True )
print('RMSLE score on train data:')
 print(rmsle(y_train,stacked_train_pred*0.70 +
xgb_train_pred*0.15 + lgb_train_pred*0.15)) 
<define_variables>
train_df = pd.read_csv(path/'train.csv')
 test_df = pd.read_csv(path/'test.csv' )
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15<create_dataframe>
train_df['set'] = 'train'
test_df['set'] = 'test'
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble<save_to_csv>
procs=[FillMissing, Categorify, Normalize]
sub.to_csv('submission.csv',index=False )<set_options>
cat_vars = ['MSSubClass', 'MSZoning', 'Street', 'Alley', 'LandContour', 'Utilities', 'LotConfig', 'Neighborhood', 'Condition1',
'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'Foundation', 'Heating', 'Electrical', 'GarageType', 'GarageFinish', 'PavedDrive', 'Fence', 'MiscFeature',
'SaleType', 'SaleCondition','CentralAir'] + ['ExterCond', 'ExterQual','BsmtCond', 'BsmtQual', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2','KitchenQual','Functional', 'FireplaceQu', 'GarageQual', 'GarageCond', 'PoolQC','HeatingQC','LotShape',]
cont_vars = ['LotFrontage', 'LotArea', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'MasVnrArea',
'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath', 'TotRmsAbvGrd', 'Fireplaces',
'GarageYrBlt','MiscVal', 'MoSold', 'YrSold',
'GarageCars', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea',
]
%matplotlib inline 
<load_from_csv>
df = pd.concat([train_df,test_df])
 df.head()
print(len(df))
sample_submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.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")
<drop_column>
fill_missing = FillMissing(cat_vars, cont_vars)
 fill_missing(df)
 fill_missing(df, test=True)
 categorify = Categorify(cat_vars, cont_vars)
 categorify(df)
 categorify(df, test=True)
 normalize = Normalize(cat_vars, cont_vars)
 normalize(df)
 normalize(df, test=True )
train.drop('Id', axis= 1, inplace= False)
<drop_column>
dep_var = 'SalePrice'
train_df = df.loc[df.set == 'train']
test_df = df.loc[df.set == 'test']
test.drop('Id', axis= 1, inplace= False )<define_variables>
max_log_y = np.log(np.max(train_df['SalePrice'])*1.2)
 y_range = torch.tensor([0, max_log_y], device=defaults.device )
train1= train
test1= test<concatenate>
def rmsle(inp,targ):
"Mean squared error between `inp` and `targ`."
return torch.sqrt(msle(inp, targ))
df = pd.concat([train1, test1], axis=0,sort=False)
<count_missing_values>
learn = tabular_learner(data, layers=[1000,500], ps=[0.02,0.04], emb_drop=0.05,
y_range=y_range, metrics=rmsle, loss_func=rmsle)
 learn.model_dir = '/kaggle/working/'
df.isnull().sum()<data_type_conversions>
learn.model
df["PoolQC"] = df["PoolQC"].fillna("None")
 df["MiscFeature"] = df["MiscFeature"].fillna("None")
 df["Alley"] = df["Alley"].fillna("None")
 df['Fence'] = df['Fence'].fillna('None')
 df['PoolQC'] = df['PoolQC'].fillna('None')
 df['GarageCond'] = df['GarageCond'].fillna('None')
 df['GarageQual'] = df['GarageQual'].fillna('None')
 df['GarageFinish'] = df['GarageFinish'].fillna('None')
 df['GarageType'] = df['GarageType'].fillna('None')
 df['FireplaceQu'] = df['FireplaceQu'].fillna('None')
 df['BsmtFinType2'] = df['BsmtFinType2'].fillna('None')
 df['BsmtFinType1'] = df['BsmtFinType1'].fillna('None')
 df['BsmtExposure'] = df['BsmtExposure'].fillna('None')
 df['BsmtCond'] = df['BsmtCond'].fillna('None')
 df['BsmtQual'] = df['BsmtQual'].fillna('None' )<count_missing_values>
learn.lr_find()
df.isnull().sum()<create_dataframe>
learn.fit_one_cycle(50, 1e-2, wd=0.5 )
df_missing =(df.isnull().sum() / len(df)) * 100
df_missing = df_missing.drop(df_missing[df_missing == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :df_missing})
 missing_data.head(30 )<create_dataframe>
learn.save('lr 1e-1' )
df = df.fillna(df.median())
 df_missing =(df.isnull().sum() / len(df)) * 100
df_missing = df_missing.drop(df_missing[df_missing == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :df_missing})
 missing_data.head(30 )<correct_missing_values>
test_data =(TabularList.from_df(test_df, path=path, cat_names=cat_vars, cont_names=cont_vars, procs=data.processor))
print(type(test_data[0]))
print(test_data[0]['Id'] )
df = df.fillna(df.median() )<create_dataframe>
test_data =(TabularList.from_df(test_df, path=path, cat_names=cat_vars, cont_names=cont_vars, procs=data.processor))
results = []
ids = []
for i in range(len(test_data)) :
print(i)
 ids.append(test_data[i]['Id'])
 pred = learn.predict(test_data[i])
 pred = torch.exp(pred[1])
 results.append(float(pred))
print(results)
df = df.fillna(df.median())
 df_missing =(df.isnull().sum() / len(df)) * 100
df_missing = df_missing.drop(df_missing[df_missing == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :df_missing})
 missing_data.head(30 )<count_values>
submission = pd.DataFrame({'Id': ids, 'SalePrice':results})
 print(submission)
 submission.to_csv("submission.csv", index=False )