Kaggle competition: House Prices - Advanced Regression Techniques
blender_of_ensamble = lm.LinearRegression()
blender_of_ensamble.fit(X_stack_train, y_stack_train )<predict_on_test>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_train.head()
pred_result_test = get_predictions(X_val, ensemble_of_regressors, y_val )<predict_on_test>
df_train.set_index('Id', inplace=True)
 df_test.set_index('Id', inplace=True )
X_stack_test = pred_result_test
y_stack_pred = blender_of_ensamble.predict(X_stack_test)
 X_stack_test.shape, y_stack_pred.shape<compute_test_metric>
df_train.drop(['GarageArea','1stFlrSF','TotRmsAbvGrd','2ndFlrSF'], axis=1, inplace=True)
 df_test.drop(['GarageArea','1stFlrSF','TotRmsAbvGrd','2ndFlrSF'], axis=1, inplace=True )
m.r2_score(y_stack_pred, y_val )<choose_model_class>
df_train = df_train[df_train['GrLivArea']<4500]
estimators2 = [ 
('svr', svm.SVR(kernel='rbf', C=2, epsilon=0.01, gamma=0.0005)) , 
('en', lm.ElasticNet(alpha=0.1, l1_ratio=0.03)) , 
('gbr', e.GradientBoostingRegressor(learning_rate=0.05, max_depth=3, n_estimators=1200)) , 
('lgbm', lgb.LGBMRegressor(lambda_l1=0.01, lambda_l2=0.1, learning_rate=0.05,
min_data_in_leaf=25, n_estimators=2000, num_leaves=4)) , 
('xgb', xgb.XGBRegressor(learning_rate=0.01, max_depth=4, n_estimators=4000))
]<choose_model_class>
def check_nulls(df):
percent_missing =(df.isnull().sum() * 100 / len(df)).sort_values()
return round(percent_missing,2 )
s_reg = e.StackingRegressor(
 estimators=estimators2,
cv=20,
n_jobs=-1
)<split>
check_nulls(df_train )
X_train, X_val, y_train, y_val = ms.train_test_split(X, y, test_size=0.2 )<train_model>
check_nulls(df_test )
s_reg.fit(X_train, y_train )<compute_test_metric>
categorical_list = [col for col in df_train.columns if df_train[col].dtypes == object]
numerical_list = [col for col in df_train.columns if df_train[col].dtypes != object]
print('Categories:', categorical_list)
 print('Numbers:', numerical_list )
s_reg.score(X_val, y_val )<train_model>
def fill_missing_values(df):
lst = ["Alley","BsmtQual","BsmtCond","BsmtExposure","BsmtFinType1",
"BsmtFinType2","Fence","FireplaceQu","GarageType","GarageFinish",
"GarageQual","GarageCond","Electrical","GarageFinish","MiscFeature","MasVnrType","PoolQC"]
for col in lst:
df[col] = df[col].fillna("Not present")
 lst = ['GarageYrBlt','MasVnrArea','BsmtFinSF1','BsmtFinSF2','TotalBsmtSF',
'BsmtUnfSF','BsmtFullBath','BsmtHalfBath','MasVnrArea','GarageCars']
for col in lst:
df[col] = df[col].fillna(0)
 lst = ['Utilities','MSZoning','Exterior1st','Exterior2nd','Electrical','KitchenQual']
for col in lst:
df[col] = df[col].fillna(df[col].mode() [0])
 df['Functional'] = df['Functional'].fillna('Typ')
 df['SaleType'] = df['SaleType'].fillna('Normal')
 df['LotFrontage'] = df['LotFrontage'].fillna(df.LotFrontage.mean())
 df.drop('PoolQC', axis=1, inplace=True)
s_reg.fit(X, y )<load_from_csv>
fill_missing_values(df_train)
 fill_missing_values(df_test )
hp_pred = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<drop_column>
print(df_train.isnull().sum().sum())
 print(df_test.isnull().sum().sum() )
X_pred = hp_pred.drop(columns=unused_features )<count_missing_values>
def label_encode(df):
df['MSSubClass'] = df['MSSubClass'].astype(object)
 df = df.replace({"Alley" : {"Not present" : 0, "Grvl" : 1, "Pave" : 2},
"BsmtCond" : {"Not present" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"Not present" : 0, "No" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"Not present" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4,
"ALQ" : 5, "GLQ" : 6},
"BsmtFinType2" : {"Not present" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4,
"ALQ" : 5, "GLQ" : 6},
"BsmtQual" : {"Not present" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"CentralAir" : {"N" : 0, "Y" : 1},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"Not present" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5,
"Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageCond" : {"Not present" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"Not present" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageFinish" :{"Not present" : 0, "Unf" : 1, "RFn" : 2, "Fin" : 3},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"LandSlope" : {"Sev" : 1, "Mod" : 2, "Gtl" : 3},
"LotShape" : {"IR3" : 1, "IR2" : 2, "IR1" : 3, "Reg" : 4},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"PoolQC" : {"Not present" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3, "Ex" : 4},
"Street" : {"Grvl" : 1, "Pave" : 2},
"Utilities" : {"ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3, "AllPub" : 4},
"Fence": {"Not present" : 0, "MnWw" : 1, "GdWo" : 2, "MnPrv" : 3, "GdPrv" : 4 }}, 
)
 return df
X_pred.isna().sum().sum()<normalization>
df_train = label_encode(df_train)
 df_test = label_encode(df_test )
X_pred[numerical_features] = numerical_imputer.transform(X_pred[numerical_features] )<categorify>
df_train['SalePrice']=np.log(df_train['SalePrice'])
 _ = sns.distplot(df_train["SalePrice"] )
X_pred[date_features] = date_imputer.transform(X_pred[date_features] )<categorify>
cat_list = [col for col in df_train.columns if df_train[col].dtypes == object]
num_list = [col for col in df_train.columns if df_train[col].dtypes != object]
X_pred[categorical_features_to_impute_mode] = categorical_mode_imputer.transform(X_pred[categorical_features_to_impute_mode])
 X_pred[categorical_features_to_impute_none] = categorical_none_imputer.transform(X_pred[categorical_features_to_impute_none] )<feature_engineering>
categorical_data = df_train[cat_list]
numerical_data = df_train[num_list]
df_train = categorical_data.join(numerical_data )
X_pred[['LotFrontage', 'LotArea', '1stFlrSF', 'GrLivArea']] = np.log(X_pred[['LotFrontage', 'LotArea', '1stFlrSF', 'GrLivArea']] )<feature_engineering>
num_list.remove('SalePrice')
 cat_test = df_test[cat_list]
num_test = df_test[num_list]
df_test = cat_test.join(num_test )
create_features(X_pred )<categorify>
X = df_train.drop('SalePrice', axis=1 ).values
y = df_train['SalePrice'].values
df_test_values = df_test.values
X_pred_oh = pd.DataFrame(one_hot_encoder.transform(X_pred[categorical_oh_features]), columns=one_hot_encoder.get_feature_names(categorical_oh_features))
X_pred_oh<concatenate>
def encode(X):
labelencoder = LabelEncoder()
for i in range(len(cat_list)) :
X[:,i] = labelencoder.fit_transform(X[:,i])
 for i in range(len(cat_list)) :
onehotencoder = OneHotEncoder(categorical_features=[i])
 X = onehotencoder.fit_transform(X ).toarray()
X = X[:,i:]
X_pred_oh.index = X_pred.index
X_pred.drop(categorical_oh_features, axis=1, inplace=True)
 X_pred = pd.concat([X_pred, X_pred_oh], axis=1)
 X_pred<create_dataframe>
encode(X)
 encode(df_test_values )
X_pred[categorical_l_features] = pd.DataFrame(ordinal_encoder.transform(X_pred[categorical_l_features]), columns=X_pred[categorical_l_features].columns)
 X_pred[categorical_l_features]<create_dataframe>
sc = StandardScaler()
X = sc.fit_transform(X)
 df_test_values = sc.transform(df_test_values )
X_pred = pd.DataFrame(scaler.transform(X_pred),columns=X_pred.columns)
 X_pred<predict_on_test>
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2 )
<create_dataframe>
def rmsle(y, y_pred):
assert len(y)== len(y_pred)
 terms_to_sum = [(math.log(y_pred[i] + 1)- math.log(y[i] + 1)) ** 2.0 for i,pred in enumerate(y_pred)]
return(sum(terms_to_sum)*(1.0/len(y)))** 0.5
y_pred = pd.DataFrame()
y_pred['Id'] = hp_pred.Id<predict_on_test>
lm = LinearRegression()
lm.fit(X_train,y_train)
 y_pred_reg = lm.predict(X_test )
y_pred['SalePrice'] = np.exp(s_reg.predict(X_pred))<save_to_csv>
lm.intercept_
y_pred.to_csv('submission.csv', index=False )<set_options>
rmsle(np.exp(y_test), np.exp(y_pred_reg))
%matplotlib inline<load_from_csv>
lasso_model = Lasso()
lasso_model.fit(X_train,y_train )
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
y_pred_lasso = lasso_model.predict(X_test )
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<sort_values>
rmsle(np.exp(y_test), np.exp(y_pred_lasso))
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<data_type_conversions>
lcv = LassoCV()
lcv.fit(X_train,y_train )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )<data_type_conversions>
lcv.alpha_
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )<data_type_conversions>
y_pred_lassocv = lcv.predict(X_test )
all_data["Alley"] = all_data["Alley"].fillna("None" )<data_type_conversions>
rmsle(np.exp(y_test),np.exp(y_pred_lassocv))
all_data["Fence"] = all_data["Fence"].fillna("None" )<data_type_conversions>
model = Lasso(lcv.alpha_)
 model.fit(X,y)
 y_pred = model.predict(df_test_values)
 predictions = np.exp(y_pred )
<categorify><EOS> 
result=pd.DataFrame({'Id':df_test.index, 'SalePrice':predictions})
 result.to_csv('submission.csv', index=False )