Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import time
import datetime
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from scipy.stats import skew
submission.Pred = LGB_predict
submission.to_csv(f'LGB.csv', index=False )<load_from_csv>
train_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col=0)
 test_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col=0)
 data = train_data.append(test_data )
opt_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 opt_result = opt_result[opt_result.Season > 2014].reset_index()
opt_result = opt_result[['Season', 'WTeamID', 'LTeamID']]
opt_result.head()<feature_engineering>
for col in data:
datatype = data[col].dtype
if datatype == np.int64 or datatype == np.float64:
data[col] = data[col].fillna(data[col].mean() )
for index, match in opt_result.iterrows() :
opt_result.loc[index, 'ID'] = f"{match.Season}_{'_'.join(str(num)for num in sorted([match.WTeamID,match.LTeamID])) }"
opt_result.loc[index, 'Pred'] = 1 if match.WTeamID < match.LTeamID else 0
opt_result = opt_result[['ID', 'Pred']]
opt_result.head()<load_from_csv>
data['GrLivArea'] = data['GrLivArea'].clip(0, 3500)
 data['TotalBsmtSF'] = data['TotalBsmtSF'].clip(0, 3500)
 data['1stFlrSF'] = data['1stFlrSF'].clip(0, 3500 )
opt_test = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' ).drop(columns='Pred')
 opt_test.head()<save_to_csv>
data['GarageArea'] = data['GarageArea'].clip(0, 1200 )
predict = pd.merge(left=opt_test, right=opt_result, how='left', on='ID')
 predict.Pred = predict.Pred.fillna(0)
 predict.to_csv('Phan_Viet_Hoang.csv', index=False )<set_options>
data['YearBuilt'] = data['YearBuilt'].clip(1950, data['YearBuilt'].max())
 data['YearRemodAdd'] = data['YearRemodAdd'].clip(1950, data['YearRemodAdd'].max() )
warnings.filterwarnings("ignore")
 %matplotlib inline<set_options>
data['BsmtFinSF1'] = data['BsmtFinSF1'].clip(0, 2500)
 data['OpenPorchSF'] = data['OpenPorchSF'].clip(0, 400)
 data['LotArea'] = data['LotArea'].clip(0, 70000 )
pd.options.display.max_columns = None<load_from_csv>
data = data.drop(columns=['PoolArea'] )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<define_variables>
data = data.drop(columns=[
'KitchenAbvGr',
'BsmtFullBath',
'BsmtHalfBath'
])
 data['TotRmsAbvGrd'] = data['TotRmsAbvGrd'].clip(0, 11)
 data['HalfBath'] = data['HalfBath'].clip(0, 1)
 data['BedroomAbvGr'] = data['BedroomAbvGr'].clip(0, 4 )
test_ID = test['Id']<sort_values>
data['GarageYrBlt'] = data['GarageYrBlt'].clip(1950, data['GarageYrBlt'].max() )
train_missing = train.isnull().sum().sort_values(ascending=False)
 train_missing.head(20 )<data_type_conversions>
train_data['DateSold'] = '01/' + train_data['MoSold'].astype(str)+ '/' + train_data['YrSold'].astype(str)
 train_data['DateSold'] = pd.to_datetime(train_data['DateSold'], format='%d/%m/%Y')
 train_data['DateSold'] =(train_data['DateSold'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
 train_data.plot.scatter(x="DateSold", y="SalePrice" )
train['MiscFeature'].fillna('None', inplace=True)
 train['Fence'].fillna('No Fence', inplace=True)
 train['PoolQC'].fillna('No Pool', inplace=True)
 train['Alley'].fillna('No alley access', inplace=True)
 train['FireplaceQu'].fillna('No Fireplace', inplace=True)
 train['LotFrontage'].fillna(train['LotFrontage'].median() , inplace=True)
 train['GarageCond'].fillna('No Garage', inplace=True)
 train['GarageType'].fillna('No Garage', inplace=True)
 train['GarageYrBlt'].fillna(round(train['GarageYrBlt'].median() , 1), inplace=True)
 train['GarageFinish'].fillna('No Garage', inplace=True)
 train['GarageQual'].fillna('No Garage', inplace=True)
 train['BsmtExposure'].fillna('No Basement', inplace=True)
 train['BsmtFinType2'].fillna('No Basement', inplace=True)
 train['BsmtFinType1'].fillna('No Basement', inplace=True)
 train['BsmtCond'].fillna('No Basement', inplace=True)
 train['BsmtQual'].fillna('No Basement', inplace=True)
 train['MasVnrArea'].fillna(0.0, inplace=True)
 train['MasVnrType'].fillna('None', inplace=True)
 train['Electrical'].fillna('Mixed', inplace=True )<define_variables>
data['DateSold'] = '01/' + data['MoSold'].astype(str)+ '/' + data['YrSold'].astype(str)
 data['DateSold'] = pd.to_datetime(data['DateSold'], format='%d/%m/%Y')
 data['DateSold'] =(data['DateSold'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
 data = data.drop(columns=['MoSold', 'YrSold'] )
best_columns = ['OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea',\
'TotalBsmtSF', '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', \
'YearBuilt', 'YearRemodAdd', 'MasVnrArea', 'Fireplaces', \
'BsmtFinSF1', 'LotFrontage', 'WoodDeckSF', '2ndFlrSF', 'OpenPorchSF']<sort_values>
numerical_columns = data.select_dtypes(include=['int64', 'float64'])
 skewness_of_feats = numerical_columns.apply(lambda x: skew(x)).sort_values(ascending=False)
 print(skewness_of_feats )
test_missing = test.isnull().sum().sort_values(ascending=False)
 test_missing.head(35 )<data_type_conversions>
data['LowQualFinSF'] = np.log1p(data['LowQualFinSF'])
 data['LotArea'] = np.log1p(data['LotArea'])
 data['BsmtFinSF2'] = np.log1p(data['BsmtFinSF2'])
 data['ScreenPorch'] = np.log1p(data['ScreenPorch'])
 data['EnclosedPorch'] = np.log1p(data['EnclosedPorch'])
 data['MasVnrArea'] = np.log1p(data['MasVnrArea'])
 data['OpenPorchSF'] = np.log1p(data['OpenPorchSF'])
 data['WoodDeckSF'] = np.log1p(data['WoodDeckSF'])
 data['LotFrontage'] = np.log1p(data['LotFrontage'])
 data['1stFlrSF'] = np.log1p(data['1stFlrSF'] )
test['MiscFeature'].fillna('None', inplace=True)
 test['Fence'].fillna('No Fence', inplace=True)
 test['PoolQC'].fillna('No Pool', inplace=True)
 test['Alley'].fillna('No alley access', inplace=True)
 test['FireplaceQu'].fillna('No Fireplace', inplace=True)
 test['LotFrontage'].fillna(train['LotFrontage'].median() , inplace=True)
 test['GarageCond'].fillna('No Garage', inplace=True)
 test['GarageType'].fillna('No Garage', inplace=True)
 test['GarageYrBlt'].fillna(round(test['GarageYrBlt'].median() , 1), inplace=True)
 test['GarageFinish'].fillna('No Garage', inplace=True)
 test['GarageQual'].fillna('No Garage', inplace=True)
 test['BsmtExposure'].fillna('No Basement', inplace=True)
 test['BsmtFinType2'].fillna('No Basement', inplace=True)
 test['BsmtFinType1'].fillna('No Basement', inplace=True)
 test['BsmtCond'].fillna('No Basement', inplace=True)
 test['BsmtQual'].fillna('No Basement', inplace=True)
 test['MasVnrArea'].fillna(0.0, inplace=True)
 test['MasVnrType'].fillna('None', inplace=True)
 test['MSZoning'].fillna('RL', inplace=True)
 test['Utilities'].fillna('AllPub', inplace=True)
 test['Functional'].fillna('Typ', inplace=True)
 test['BsmtFullBath'].fillna(0.0, inplace=True)
 test['BsmtHalfBath'].fillna(0.0, inplace=True)
 test['BsmtFinSF2'].fillna(0.0, inplace=True)
 test['BsmtUnfSF'].fillna(0.0, inplace=True)
 test['TotalBsmtSF'].fillna(test['TotalBsmtSF'].median() , inplace=True)
 test['SaleType'].fillna('WD', inplace=True)
 test['BsmtFinSF1'].fillna(0.0, inplace=True)
 test['GarageCars'].fillna(2.0, inplace=True)
 test['GarageArea'].fillna(0.0, inplace=True)
 test['KitchenQual'].fillna('TA', inplace=True)
 test['Exterior1st'].fillna('VinylSd', inplace=True)
 test['Exterior2nd'].fillna('VinylSd', inplace=True )<concatenate>
data = data.drop(columns=[
'PoolQC',
'Fence',
'MiscFeature',
'Alley',
'FireplaceQu'
] )
train_test = pd.concat([train,test], axis=0, sort=False )<drop_column>
for col in data:
datatype = data[col].dtype
if datatype == 'object':
data[col] = data[col].fillna('None' )
train.drop(['Id', 'Utilities', 'Street', 'LowQualFinSF', 'PoolArea'], axis=1, inplace=True)
 test.drop(['Id', 'Utilities', 'Street', 'LowQualFinSF', 'PoolArea'], axis=1, inplace=True)
 train_test.drop(['Id', 'Utilities', 'Street', 'LowQualFinSF', 'PoolArea'], axis=1, inplace=True )<data_type_conversions>
labels = train_data.pop('SalePrice')
 data = data.drop(columns=['SalePrice'] )
train['MSSubClass'] = train['MSSubClass'].apply(str)
 train['OverallCond'] = train['OverallCond'].astype(str)
 train['YrSold'] = train['YrSold'].astype(str)
 train['MoSold'] = train['MoSold'].astype(str)
 test['MSSubClass'] = test['MSSubClass'].apply(str)
 test['OverallCond'] = test['OverallCond'].astype(str)
 test['YrSold'] = test['YrSold'].astype(str)
 test['MoSold'] = test['MoSold'].astype(str)
 train_test['MSSubClass'] = train_test['MSSubClass'].apply(str)
 train_test['OverallCond'] = train_test['OverallCond'].astype(str)
 train_test['YrSold'] = train_test['YrSold'].astype(str)
 train_test['MoSold'] = train_test['MoSold'].astype(str)
<define_variables>
features = pd.get_dummies(data )
numeric = ['LotFrontage',
'LotArea',
'YearBuilt',
'YearRemodAdd',
'MasVnrArea',
'BsmtFinSF1',
'BsmtFinSF2',
'BsmtUnfSF',
'TotalBsmtSF',
'1stFlrSF',
'2ndFlrSF',
'GrLivArea',
'GarageArea',
'WoodDeckSF',
'OpenPorchSF',
'EnclosedPorch',
'3SsnPorch',
'ScreenPorch',
'MiscVal',
'GarageYrBlt']<define_variables>
train_features = features[:train_data.shape[0]]
test_features = features[train_data.shape[0]:]
categorical = set(columns)- set(numeric )<set_options>
model = LinearRegression(fit_intercept=False, copy_X=True)
 model.fit(train_features, labels )
sns.set_style('whitegrid' )<drop_column>
predictions = model.predict(train_features)
 mean_squared_error(
 np.log(np.expm1(labels)) ,
np.log(np.expm1(predictions)) ,
squared=False
)
train.drop(['PoolQC', 'Condition2'], axis=1, inplace=True)
 test.drop(['PoolQC', 'Condition2'], axis=1, inplace=True)
 train_test.drop(['PoolQC', 'Condition2'], axis=1, inplace=True )<drop_column>
ridge_model = Ridge(alpha=0.5, fit_intercept=False, copy_X=True)
 ridge_model.fit(train_features, labels )
categorical.remove('PoolQC')
 categorical.remove('Condition2')
 categorical.remove('SalePrice' )<categorify>
ridge_predictions = ridge_model.predict(train_features)
 mean_squared_error(
 np.log(np.expm1(labels)) ,
np.log(np.expm1(ridge_predictions)) ,
squared=False
)
LE = LabelEncoder()<categorify>
lasso_model = Lasso(alpha=0.5, fit_intercept=False, copy_X=True)
 lasso_model.fit(train_features, labels )
for col in categorical:
train[col] = LE.fit_transform(train[col] )<choose_model_class>
lasso_predictions = lasso_model.predict(train_features)
 mean_squared_error(
 np.log(np.expm1(labels)) ,
np.log(np.expm1(lasso_predictions)) ,
squared=False
)
rf_clf = RandomForestRegressor(random_state=42, n_estimators=500, max_depth=8, criterion='mse' )<train_model>
elastic_model = ElasticNet(alpha=0.5, fit_intercept=False, copy_X=True)
 elastic_model.fit(train_features, labels )
%time rf_clf.fit(train.drop(['SalePrice'], axis=1), train['SalePrice'] )<compute_train_metric>
elastic_predictions = elastic_model.predict(train_features)
 mean_squared_error(
 np.log(np.expm1(labels)) ,
np.log(np.expm1(elastic_predictions)) ,
squared=False
)
%time cross_val_score(rf_clf, train.drop(['SalePrice'], axis=1), train['SalePrice'], cv=5 )<define_variables>
predictions = ridge_model.predict(test_features )
<concatenate><EOS> 
submission = pd.DataFrame(data={'SalePrice': np.expm1(predictions)}, index=test_data.index)
 submission.index = submission.index.rename('Id')
 submission.to_csv('submission_file.csv' )