Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<set_options>
%matplotlib inline
pd.options.display.max_columns = 100
pd.options.display.max_rows = 100
matplotlib.style.use('ggplot' )
%matplotlib inline
sns.set(style="ticks", color_codes=True)
<define_variables>
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.describe()
seed = 7
np.random.seed(seed )<load_from_csv>
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train_copy = train.copy()
test_copy = test.copy()
all_data = pd.concat([train, test], sort = False)
 train.head()<groupby>
train["SalePrice"] = np.log1p(train["SalePrice"] )
train.groupby('Pclass' ).Survived.mean()<groupby>
ntrain = train.shape[0]
ntest = test.shape[0]
y_train_sale = train.SalePrice.values
combined = pd.concat(( train, test)).reset_index(drop=True)
 combined.drop(['SalePrice'], axis=1, inplace=True )
train.groupby('Sex' ).Survived.mean()<groupby>
all_data_na =(combined.isnull().sum() / len(combined)) * 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
train.groupby(['SibSp'] ).Survived.mean()<groupby>
combined['MasVnrArea'] = combined['MasVnrArea'].fillna(0.0)
 combined["MasVnrType"] = combined["MasVnrType"].fillna("None")
 combined['LotFrontage'] = combined['LotFrontage'].fillna(combined['LotFrontage'].median())
 combined['BsmtFinSF1'] = combined['BsmtFinSF1'].fillna(0.0)
 combined['BsmtFinSF2'] = combined['BsmtFinSF2'].fillna(0.0)
 combined['BsmtUnfSF'] = combined['BsmtUnfSF'].fillna(0.0)
 combined['TotalBsmtSF'] = combined['TotalBsmtSF'].fillna(0.0)
 combined['BsmtFullBath'] = combined['BsmtFullBath'].fillna(0)
 combined['BsmtHalfBath'] = combined['BsmtHalfBath'].fillna(0)
 combined['GarageYrBlt'] = combined['GarageYrBlt'].fillna(0)
 combined['GarageCars'] = combined['GarageCars'].fillna(0)
 combined['GarageArea'] = combined['GarageArea'].fillna(0)
 combined['GarageFinish'] = combined['GarageFinish'].fillna('None')
 combined['MSZoning'] = combined['MSZoning'].fillna(combined['MSZoning'].mode() [0])
 combined = combined.drop(['Utilities'], axis=1)
 combined["Functional"] = combined["Functional"].fillna("Typ")
 combined['Electrical'] = combined['Electrical'].fillna(combined['Electrical'].mode() [0])
 combined['KitchenQual'] = combined['KitchenQual'].fillna(combined['KitchenQual'].mode() [0])
 combined['Exterior1st'] = combined['Exterior1st'].fillna(combined['Exterior1st'].mode() [0])
 combined['Exterior2nd'] = combined['Exterior2nd'].fillna(combined['Exterior2nd'].mode() [0])
 combined['SaleType'] = combined['SaleType'].fillna(combined['SaleType'].mode() [0])
 combined['MSSubClass'] = combined['MSSubClass'].fillna("None")
 combined['PoolQC'] = combined['PoolQC'].fillna('None')
 combined['MiscFeature'] = combined['MiscFeature'].fillna('None')
 combined['Alley'] = combined['Alley'].fillna('None')
 combined['Fence'] = combined['Fence'].fillna('None')
 combined['FireplaceQu'] = combined['FireplaceQu'].fillna('None')
 combined["LotFrontage"] = combined.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
combined[col] = combined[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
combined[col] = combined[col].fillna('None' )
train.groupby(['Parch'] ).Survived.mean()<feature_engineering>
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(combined[c].values))
combined[c] = lbl.transform(list(combined[c].values))
all_data['Title'] = all_data['Name'].str.extract('([A-Za-z]+)\.', expand=True )<count_values>
combined['TotalSF'] = combined['TotalBsmtSF'] + combined['1stFlrSF'] + combined['2ndFlrSF']
all_data['Title'].value_counts()<categorify>
combined = pd.get_dummies(combined )
mappings = {'Dr':'Respected_Male', 'Col':'Respected_Male', 'Major':'Respected_Male', 'Capt':'Respected_Male',
'Mme':'Noble', 'Mlle':'Noble', 'Countess': 'Noble', 'Lady': 'Noble', 'Sir':'Noble',
'Ms' : 'Miss', 'Rev': 'Other', 'Jonkheer': 'Other', 'Dona': 'Other', 'Don': 'Other'
}
all_data.replace({'Title': mappings}, inplace = True )<count_values>
train = combined[:ntrain]
test = combined[ntrain:]
all_data['Title'].value_counts()<define_variables>
x_train, x_test, y_train, y_test = train_test_split(train, y_train_sale, test_size=0.1, random_state=200 )
one_hot_encoding_list = []
one_hot_encoding_list.append('Title' )<count_missing_values>
from sklearn import ensemble, tree, linear_model
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
all_data['Age'].isnull().sum()<feature_engineering>
def get_score(prediction, lables):
print('R2: {}'.format(r2_score(prediction, lables)))
 print('RMSE: {}'.format(np.sqrt(mean_squared_error(prediction, lables))))
def train_test(estimator, x_trn, x_tst, y_trn, y_tst):
prediction_train = estimator.predict(x_trn)
 get_score(prediction_train, y_trn)
 prediction_test = estimator.predict(x_tst)
 get_score(prediction_test, y_tst )
title_grouped = all_data.groupby(['Title'])
 for title in all_data.Title.unique() :
all_data.loc[(all_data.Age.isnull())&(all_data.Title == title), 'Age'] = title_grouped.get_group(title ).Age.mean()<feature_engineering>
GBR = ensemble.GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05, max_depth=3, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10, loss='huber' ).fit(x_train, y_train)
 train_test(GBR, x_train, x_test, y_train, y_test )
all_data['AgeBin'] = pd.qcut(all_data['Age'], 5 )<categorify>
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0007000000000000001, random_state=1)).fit(x_train, y_train)
 train_test(lasso, x_train, x_test, y_train, y_test )
label = preprocessing.LabelEncoder()
all_data['AgeBin'] = label.fit_transform(all_data['AgeBin'])
 cat_plot('AgeBin', data = all_data )<feature_engineering>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 ).fit(x_train, y_train)
 train_test(model_lgb, x_train, x_test, y_train, y_test )
all_data['FamilySize'] = all_data['SibSp'] + all_data['Parch'] + 1<count_values>
GB_model = GBR.fit(train, y_train_sale)
 gbr_labels = np.expm1(GB_model.predict(test))
lasso_model = lasso.fit(train, y_train_sale)
 lasso_labels = np.expm1(lasso_model.predict(test))
lgb_model = model_lgb.fit(train, y_train_sale)
 lgb_labels = np.expm1(lgb_model.predict(test))
all_data['FamilySize'].value_counts(dropna = False )<feature_engineering>
output = lgb_labels*0.50 + lasso_labels*0.25 + gbr_labels*0.25
<count_missing_values><EOS> 
pd.DataFrame({'Id': test.Id, 'SalePrice': output} ).to_csv('submission.csv', index =False )