Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<drop_column>
import numpy as np
import pandas as pd
from scipy.stats import skew
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.linear_model import Ridge
from sklearn.linear_model import LinearRegression
from mlxtend.regressor import StackingRegressor
from sklearn.metrics import mean_squared_error
from math import sqrt
data['familysize-fare+age+pclass'] = data.total_family_size - data.Fare+data.Age+data.Pclass
data.drop('total_family_size', axis=1, inplace=True)
 data.corr()<feature_engineering>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
data['age_fare'] = data.Fare-data.Age
data['agegrp_fare'] = data.Fare*data.age_grp
data.corr()<count_missing_values>
df_train = df_train.drop(
 df_train[(df_train['GrLivArea']>4000)&(df_train['SalePrice']<300000)].index )
data.isna().sum()<categorify>
all_data = pd.concat(( df_train.loc[:,'MSSubClass':'SaleCondition'],
df_test.loc[:,'MSSubClass':'SaleCondition']))
dict(list(zip(['Mr', 'Mrs', 'Miss', 'Master', 'rare'], [1,2,3,4,5])) )<feature_engineering>
all_data = all_data.drop(['Utilities'], axis=1 )
data['suff+fare'] = data.suffix+data.Fare
data['suff+Pclass'] = data.suffix-data.Pclass
data['suff-fs-fare+age+cls'] = data.suffix-data['familysize-fare+age+pclass']
data['suff+age_fare'] = data.suffix+data.age_fare
data['suff+agegrp_fare'] = data.suffix+data.agegrp_fare
data.corr()<count_missing_values>
all_data["PoolQC"] = all_data["PoolQC"].fillna("None")
 all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None")
 all_data["Alley"] = all_data["Alley"].fillna("None")
 all_data["Fence"] = all_data["Fence"].fillna("None")
 all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None")
 for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None')
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0])
 all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["Functional"] = all_data["Functional"].fillna("Typ")
 all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0])
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0])
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
 all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )
data.isnull().sum()<count_missing_values>
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0)
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0)
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )
data.isnull().sum()<categorify>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
data.Embarked = data.Embarked.map({'C':1,'S':2, 'Q':3} )<feature_engineering>
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
data['embarked_suffix'] = data.Embarked/data.suffix
data.corr()<feature_engineering>
df_train["SalePrice"] = np.log1p(df_train["SalePrice"] )
data['pclass-suff+age'] = data.Pclass-data['suff+age_fare']
data['pclass/suff'] = data.Pclass/data.suffix
data.corr()<categorify>
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = all_data[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )
data.Sex = data.Sex.map({'male':0, 'female':1})
 data.corr()<create_dataframe>
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(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
v0 = data.copy()<create_dataframe>
all_data = pd.get_dummies(all_data )
data = v0.copy()<create_dataframe>
X_train = all_data[:df_train.shape[0]]
X_test = all_data[df_train.shape[0]:]
y = df_train.SalePrice
v1 = data.copy()<prepare_x_and_y>
lr = LinearRegression(
 n_jobs = -1 
)
 rd = Ridge(
 alpha = 4.84 
)
 rf = RandomForestRegressor(
 n_estimators = 12,
max_depth = 3,
n_jobs = -1 
)
 gb = GradientBoostingRegressor(
 n_estimators = 40,
max_depth = 2 
)
 nn = MLPRegressor(
 hidden_layer_sizes =(90, 90),
alpha = 2.75
)
alldata = data.copy()
x = alldata.drop('Survived', axis=1)
 x = x[sorted(x)]
y = alldata.Survived<split>
model = StackingRegressor(
 regressors=[rf, gb, nn, rd],
meta_regressor=lr 
)
 model.fit(X_train, y )
trainx, testx, trainy, testy = train_test_split(x, y, test_size=0.1, random_state=4355)
 xgb = xgboost(n_estimators=8, max_depth=3, random_state=345)
 xgb.fit(trainx, trainy)
 print(xgb.score(trainx, trainy))
print(xgb.score(testx, testy))
impcol = x.columns[xgb.feature_importances_.astype(bool)]
x = x[impcol]
impx = x.copy()
impy = y.copy()
model(x,y)
 trainx, testx, trainy, testy = train_test_split(x, y, test_size=0.1, random_state=4355)
 xgb = xgboost(n_estimators=8, max_depth=3, random_state=345)
 xgb.fit(trainx, trainy)
 print(xgb.score(trainx, trainy))
print(xgb.score(testx, testy))<prepare_x_and_y>
y_pred = model.predict(X_train)
 print(sqrt(mean_squared_error(y, y_pred)) )
alldata1 = corrdata.copy()
x = alldata1.drop('Survived', axis=1)
 x = x[sorted(x)]
y = alldata.Survived
corx = x.copy()
cory = y.copy()<compute_test_metric>
Y_pred = model.predict(X_test )
<train_model><EOS> 
sub = pd.DataFrame()
sub['Id'] = df_test['Id']
sub['SalePrice'] = np.expm1(Y_pred)
 sub.to_csv('submission.csv',index=False )