Kaggle competition: Atividade_3_PMR3508
<SOS> metric: rmsle Kaggle data source: atividade_3_pmr3508<feature_engineering>
import pandas as pd
import sklearn
import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_log_error
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import AdaBoostRegressor
df_test['LotArea']=np.log(df_test['LotArea'] )<categorify>
train_data = pd.read_csv('.. /input/train.csv',
engine='python')
 test_data = pd.read_csv('.. /input/test.csv',
engine='python' )
df_test_work = df_test[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
lencoders_test = {}
for colt in df_test_work.select_dtypes(include=['object'] ).columns:
lencoders_test[colt] = LabelEncoder()
df_test_work[colt] = lencoders_test[colt].fit_transform(df_test_work[colt] )<choose_model_class>
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
corr_matrix = abs(train_data.corr())
 display(corr_matrix['median_house_value'].sort_values(ascending=False))
reg_final = xgboost.XGBRegressor(base_score=bestP['base_score'],
colsample_bylevel=bestP['colsample_bylevel'], colsample_bynode=bestP['colsample_bynode'],
colsample_bytree=bestP['colsample_bytree'],
gamma=bestP['gamma'], learning_rate=bestP['learning_rate'],
max_depth=int(bestP['max_depth']), n_estimators=int(bestP['n_estimators']),
random_state=42, reg_lambda=bestP['reg_lambda'], subsample=bestP['subsample'])
 reg_final.fit(x_train,y_train)
 y_pred = reg_final.predict(df_test_work)
 prediction = pd_out.DataFrame(y_pred)
 output = pd_out.concat([df_test_work['Id'],prediction], axis=1)
 output.columns=['Id','SalePrice']
output.to_csv('HousePrice_submission.csv', index=False)
 print("Submission successful" )<load_from_csv>
Xtrain = train_data
Xtrain = Xtrain.drop('Id', axis=1)
 Xtrain = Xtrain.drop('median_house_value', axis=1)
 Ytrain = train_data['median_house_value']
df= pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv' )<sort_values>
def rmsle(real, predicted):
sum=0.0
for x in range(len(predicted)) :
if predicted[x]<0 or real[x]<0:
continue
p = np.log(predicted[x]+1)
 r = np.log(real[x]+1)
 sum = sum +(p - r)**2
return(( sum/len(predicted)) **0.5 )
total = df.isnull().sum().sort_values(ascending=False)
 percent =(df.isnull().sum() /df.isnull().count() ).sort_values(ascending=False)
 missing = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing.head(20 )<drop_column>
def results(Xtrain, Ytrain):
R=[]
accuracy = 0.0
error = 1.0
best_accuracy = 0
best_error = 0
n = 11
knn = KNeighborsRegressor(n_neighbors=n)
 knn.fit(Xtrain, Ytrain)
 scores = cross_val_score(knn, Xtrain, Ytrain, cv=10)
 Ypred = knn.predict(Xtrain)
 knn_score = scores.mean()
knn_rmsle = rmsle(Ytrain, Ypred)
 R.append('knn score = ')
 R.append(knn_score)
 R.append('knn rmsle = ')
 R.append(knn_rmsle)
 R.append('
')
 if knn_score>accuracy:
accuracy = knn_score
best_accuracy = 'knn'
if knn_rmsle<error:
error = knn_rmsle
best_error = 'knn'
b = 0.1
reg = linear_model.Lasso(alpha = b)
 reg.fit(Xtrain, Ytrain)
 scores = cross_val_score(reg, Xtrain, Ytrain, cv=10)
 Ypred=reg.predict(Xtrain)
 lasso_score = scores.mean()
lasso_rmsle = rmsle(Ytrain, Ypred)
 R.append('lasso score = ')
 R.append(lasso_score)
 R.append('lasso rmsle = ')
 R.append(lasso_rmsle)
 R.append('
')
 if lasso_score>accuracy:
accuracy = lasso_score
best_accuracy = 'lasso'
if lasso_rmsle<error:
error = lasso_rmsle
best_error = 'lasso'
b = 0.1
rid = linear_model.Ridge(alpha = b)
 rid.fit(Xtrain, Ytrain)
 scores = cross_val_score(rid, Xtrain, Ytrain, cv=10)
 Ypred = rid.predict(Xtrain)
 ridge_score = scores.mean()
ridge_rmsle = rmsle(Ytrain, Ypred)
 R.append('ridge score = ')
 R.append(ridge_score)
 R.append('ridge rmsle = ')
 R.append(ridge_rmsle)
 R.append('
')
 if ridge_score>accuracy:
accuracy = ridge_score
best_accuracy = 'ridge'
if ridge_rmsle<error:
error = ridge_rmsle
best_error = 'ridge'
b = 0.1
lars = linear_model.LassoLars(alpha = b)
 lars.fit(Xtrain, Ytrain)
 scores = cross_val_score(lars, Xtrain, Ytrain, cv=10)
 lars_score = scores.mean()
Ypred = lars.predict(Xtrain)
 lars_rmsle = rmsle(Ytrain, Ypred)
 R.append('lars score = ')
 R.append(lars_score)
 R.append('lars rmsle = ')
 R.append(lars_rmsle)
 R.append('
')
 if lars_score>accuracy:
accuracy = lars_score
best_accuracy = 'lars'
if lars_rmsle<error:
error = lars_rmsle
best_error = 'lars'
d = 4
forest = RandomForestRegressor(max_depth=d, random_state=0, n_estimators=100)
 forest.fit(Xtrain, Ytrain)
 scores = cross_val_score(forest, Xtrain, Ytrain, cv=10)
 Ypred = forest.predict(Xtrain)
 forest_score = scores.mean()
forest_rmsle = rmsle(Ytrain, Ypred)
 R.append('forest score = ')
 R.append(forest_score)
 R.append('forest rmsle = ')
 R.append(forest_rmsle)
 R.append('
')
 if forest_score>accuracy:
accuracy = forest_score
best_accuracy = 'forest'
if forest_rmsle<error:
error = forest_rmsle
best_error = 'forest'
for i in R:
print(i)
 print('best accuracy =', accuracy, 'with', best_accuracy)
 print('minimum log error =', error, 'with', best_error )
df.drop(['Alley'],axis=1,inplace=True)
 df.drop(['PoolQC'],axis=1,inplace=True)
 df.drop(['MiscFeature'],axis=1,inplace=True)
 df.drop(['Fence'],axis=1,inplace=True)
 df.drop(['FireplaceQu'],axis=1,inplace=True)
 df['LotFrontage']=df['LotFrontage'].fillna(df['LotFrontage'].mean())
 df['GarageCond']=df['GarageCond'].fillna(df['GarageCond'].mode() [0])
 df['GarageType']=df['GarageType'].fillna(df['GarageType'].mode() [0])
 df['GarageYrBlt']=df['GarageYrBlt'].fillna(df['GarageYrBlt'].mean())
 df['GarageFinish']=df['GarageFinish'].fillna(df['GarageFinish'].mode() [0])
 df['GarageQual']=df['GarageQual'].fillna(df['GarageQual'].mode() [0])
 df['BsmtExposure']=df['BsmtExposure'].fillna(df['BsmtExposure'].mode() [0])
 df['BsmtFinType2']=df['BsmtFinType2'].fillna(df['BsmtFinType2'].mode() [0])
 df['BsmtFinType1']=df['BsmtFinType1'].fillna(df['BsmtFinType1'].mode() [0])
 df['BsmtCond']=df['BsmtCond'].fillna(df['BsmtCond'].mode() [0])
 df['BsmtQual']=df['BsmtQual'].fillna(df['BsmtQual'].mode() [0])
 df['MasVnrArea']=df['MasVnrArea'].fillna(df['MasVnrArea'].mean())
 df['MasVnrType']=df['MasVnrType'].fillna(df['MasVnrType'].mode() [0])
 df['Electrical']=df['Electrical'].fillna(df['Electrical'].mode() [0] )<categorify>
knn = KNeighborsRegressor(n_neighbors=10)
 knn.fit(Xtrain, Ytrain)
 scores = cross_val_score(knn, Xtrain, Ytrain, cv=10)
 Ypred = knn.predict(Xtrain)
 scores.mean()
lencoders = {}
for col in df.select_dtypes(include=['object'] ).columns:
lencoders[col] = LabelEncoder()
df[col] = lencoders[col].fit_transform(df[col] )<train_model>
rmsle(Ytrain, Ypred )
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_1 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='log2', max_depth = 4))
clf_2 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='auto', max_depth = 4))
clf_1.fit(x_t, y_t)
 clf_2.fit(x_t, y_t)
 sel_feat_1 = x_t.columns[(clf_1.get_support())]
sel_feat_2 = x_t.columns[(clf_2.get_support())]
print(sel_feat_1)
 print(sel_feat_2 )<train_model>
reg = linear_model.Lasso(alpha = 0.1)
 reg.fit(Xtrain, Ytrain)
 scores = cross_val_score(reg, Xtrain, Ytrain, cv=10)
 Ypred=reg.predict(Xtrain)
 scores.mean()
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_3 = SelectFromModel(DecisionTreeClassifier(max_features='log2'))
clf_4 = SelectFromModel(DecisionTreeClassifier(max_features='auto'))
clf_3.fit(x_t, y_t)
 clf_4.fit(x_t, y_t)
 sel_feat_3 = x_t.columns[(clf_3.get_support())]
sel_feat_4 = x_t.columns[(clf_4.get_support())]
print(sel_feat_3)
 print(sel_feat_4 )<train_model>
rmsle(Ytrain, Ypred )
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_5 = SelectFromModel(ExtraTreesClassifier(max_features='log2'))
clf_6 = SelectFromModel(ExtraTreesClassifier(max_features='auto'))
clf_5.fit(x_t, y_t)
 clf_6.fit(x_t, y_t)
 sel_feat_5 = x_t.columns[(clf_5.get_support())]
sel_feat_6 = x_t.columns[(clf_5.get_support())]
print(sel_feat_5)
 print(sel_feat_6 )<prepare_x_and_y>
rid = linear_model.Ridge(alpha = 0.5)
 rid.fit(Xtrain, Ytrain)
 scores = cross_val_score(rid, Xtrain, Ytrain, cv=10)
 Ypred = rid.predict(Xtrain)
 scores.mean()
clf_1 = RandomForestClassifier()
x_train_1 = df[['LotFrontage', 'LotArea', 'Neighborhood', 'Condition1','Condition2',
'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'ExterQual', 'BsmtFinSF1','BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional',
'GarageYrBlt', 'GarageCars', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal']]
y_train = df['SalePrice']
viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1.poof()
clf_2 = DecisionTreeClassifier()
x_train_2 = df[['LotFrontage', 'LotArea', 'LotShape', 'LotConfig', 'Neighborhood',
'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC',
'1stFlrSF', '2ndFlrSF', 'GrLivArea', 'BedroomAbvGr','TotRmsAbvGrd',
'Fireplaces','GarageType','GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2.poof()
clf_3 = ExtraTreesClassifier()
x_train_3 = df[['MSSubClass', 'LotFrontage', 'LotArea', 'LotShape', 'LotConfig',
'Neighborhood', 'OverallQual', 'OverallCond', 'YearBuilt',
'YearRemodAdd', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'MasVnrArea', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF',
'TotalBsmtSF', 'HeatingQC', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'BsmtFullBath', 'BedroomAbvGr', 'TotRmsAbvGrd', 'Fireplaces',
'GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3.poof()<drop_column>
rmsle(Ytrain, Ypred )
df_train = df[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch','MoSold', 'YrSold','SalePrice']] 
<feature_engineering>
bay = linear_model.BayesianRidge()
bay.fit(Xtrain, Ytrain)
 scores = cross_val_score(bay, Xtrain, Ytrain, cv=10)
 Ypred = bay.predict(Xtrain)
 scores.mean()
df_train['LotArea']=np.log(df_train['LotArea'] )<drop_column>
rmsle(Ytrain, Ypred )
x_train = df_train[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]<prepare_x_and_y>
lars = linear_model.LassoLars(alpha = 0.1)
 lars.fit(Xtrain, Ytrain)
 scores = cross_val_score(lars, Xtrain, Ytrain, cv=10)
 Ypred = lars.predict(Xtrain)
 scores.mean()
y_train = df_train['SalePrice']<set_options>
rmsle(Ytrain, Ypred )
valgrid = {'n_estimators':hp.quniform('n_estimators', 1500, 2500, 25),
'gamma':hp.uniform('gamma', 0.01, 0.05),
'base_score':hp.uniform('base_score',0.6,0.9),
'learning_rate':hp.uniform('learning_rate', 0.00001, 0.03),
'max_depth':hp.quniform('max_depth', 3,8,1),
'subsample':hp.uniform('subsample', 0.50, 0.95),
'colsample_bytree':hp.uniform('colsample_bytree', 0.50, 0.95),
'colsample_bylevel':hp.uniform('colsample_bylevel', 0.50, 0.95),
'colsample_bynode':hp.uniform('colsample_bynode', 0.50, 0.95),
'reg_lambda':hp.uniform('reg_lambda', 1, 20)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'gamma': params['gamma'],
'base_score': params['base_score'],
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'colsample_bylevel': params['colsample_bylevel'],
'colsample_bynode': params['colsample_bynode'],
'reg_lambda': params['reg_lambda']}
xb_a= xgb.XGBRegressor(**params)
 score = cross_val_score(xb_a, x_train, y_train, scoring='neg_mean_squared_error', cv=5, n_jobs=-1 ).mean()
return -score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(42), algo=tpe.suggest )<load_from_csv>
california_sea=[(41.990352, -124.216535),(41.936725, -124.199048),(41.862157, -124.220161),(41.758672, -124.240793),(41.730317, -124.162807),(41.672629, -124.139878),(41.722746, -124.151351),(41.671813, -124.136762),(41.618963, -124.109252),(41.470737, -124.072740),(41.383226, -124.066948),(41.308172, -124.094492),(41.212278, -124.121904), 
(41.137176, -124.165918),(41.062165, -124.165618),(41.020596, -124.115740),(40.928851, -124.143028),(40.858028, -124.126245),(40.812048, -124.181163),(40.728511, -124.235831),(40.649059, -124.301387),(40.586325, -124.344954),(40.511043, -124.388365),(40.440002, -124.409806),(40.395399, -124.383960),(40.322914, -124.349643),(40.241803, -124.337706),(40.186635, -124.253402),(40.122885, -124.169203),(40.067673, -124.068499),(40.008009, -124.029231), 
(39.922813, -123.945453),(39.837566, -123.873007),(39.735216, -123.828474),(39.654186, -123.789622),(39.564619, -123.761930),(39.399528, -123.821626),(39.201588, -123.770073),(39.076989, -123.691566),(38.960637, -123.724138),(38.879044, -123.662811),(38.754580, -123.507611),(38.634199, -123.386034),(38.496411, -123.193367),(38.336876, -123.061865),(38.259117, -122.974368), 
(38.151338, -122.952917),(38.060918, -122.980669),(37.996318, -123.002792), 
(38.026254, -122.926130),(38.004306, -122.827828),(37.931906, -122.744687),(37.902923, -122.652017),(37.872444, -122.594173),(37.880984, -122.392446), 
(37.815555, -122.367515),(37.628327, -122.331577),(37.542968, -122.455670), 
(37.370235, -122.414093),(37.290236, -122.415691),(37.167091, -122.356855), 
(37.088046, -122.276348),(36.987005, -122.157357),(36.951905, -122.049790), 
(36.969554, -121.914753),(36.925477, -121.862435),(36.824092, -121.802024), 
(36.620740, -121.851334),(36.480625, -121.934216),(36.282719, -121.866908), 
(36.162592, -121.678018),(35.990860, -121.498031),(35.827849, -121.382193), 
(35.671399, -121.272296),(35.608589, -121.143265),(35.453082, -120.919491), 
(35.297750, -120.877400),(35.189759, -120.819107),(35.180890, -120.736397), 
(35.097645, -120.628863),(34.932680, -120.660285),(34.842040, -120.610177), 
(34.742216, -120.618143),(34.583391, -120.639685),(34.528043, -120.518413), 
(34.457687, -120.472919),(34.458791, -120.347644),(34.469789, -120.138306), 
(34.422313, -119.903627),(34.399196, -119.699791),(34.408922, -119.552255),(34.335795, -119.408499),(34.288024, -119.329889),(34.199208, -119.247261),(34.115993, -119.153777),(34.041474, -118.899965),(34.035682, -118.855901),(34.018486, -118.822894),(34.003602, -118.805037),(34.016106, -118.785710), 
(34.029683, -118.744327),(34.037409, -118.667109),(34.036912, -118.580005), 
(34.009365, -118.502919),(33.984242, -118.472597),(33.960222, -118.454035),(33.867022, -118.402873),(33.810913, -118.390523),(33.770287, -118.420867),(33.716625, -118.060214),(33.606537, -117.889392),(33.385674, -117.578771),(33.270497, -117.443285),(33.127431, -117.326314),(33.053581, -117.291643),(32.831417, -117.277875),(32.683026, -117.189643),(32.536805, -117.122224)]
df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 total = df_test[['MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']].isnull().sum().sort_values(ascending=False)
 percent =(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_test = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_test.head(20 )<feature_engineering>
train_data3 = train_data2
train_data3["rooms_per_household"] = train_data3["total_rooms"]/train_data3["households"]
train_data3["bedrooms_per_room"] = train_data3["total_bedrooms"]/train_data3["total_rooms"]
train_data3["population_per_household"] = train_data3["population"]/train_data3["households"]
train_data3["income_per_person"] = train_data3["median_income"]/train_data3["population_per_household"]
train_data3['mean_rooms'] = train_data3['total_rooms']/train_data3['households']
train_data3['rooms_per_person'] = train_data3['total_rooms']/train_data3['population']
train_data3['mean_bedrooms'] = train_data3['total_bedrooms']/train_data3['households']
train_data3['bedrooms_per_person'] = train_data3['total_bedrooms']/train_data3['households']
train_data3['persons_per_household'] = train_data3['population']/train_data3['households']
train_data3['total_income'] = train_data3['median_income']*train_data3['households']
df_test['LotFrontage']=df_test['LotFrontage'].fillna(df_test['LotFrontage'].mean())
 df_test['GarageFinish']=df_test['GarageFinish'].fillna(df_test['GarageFinish'].mode() [0])
 df_test['GarageYrBlt']=df_test['GarageYrBlt'].fillna(df_test['GarageYrBlt'].mean())
 df_test['GarageType']=df_test['GarageType'].fillna(df_test['GarageType'].mode() [0])
 df_test['BsmtExposure']=df_test['BsmtExposure'].fillna(df_test['BsmtExposure'].mode() [0])
 df_test['BsmtFinType1']=df_test['BsmtFinType1'].fillna(df_test['BsmtFinType1'].mode() [0])
 df_test['MasVnrType']=df_test['MasVnrType'].fillna(df_test['MasVnrType'].mode() [0])
 df_test['MasVnrArea']=df_test['MasVnrArea'].fillna(df_test['MasVnrArea'].mean())
 df_test['BsmtFullBath']=df_test['BsmtFullBath'].fillna(df_test['BsmtFullBath'].mode() [0])
 df_test['Functional']=df_test['Functional'].fillna(df_test['Functional'].mode() [0])
 df_test['KitchenQual']=df_test['KitchenQual'].fillna(df_test['KitchenQual'].mode() [0])
 df_test['GarageArea']=df_test['GarageArea'].fillna(df_test['GarageArea'].mean())
 df_test['BsmtUnfSF']=df_test['BsmtUnfSF'].fillna(df_test['BsmtUnfSF'].mean())
 df_test['Exterior2nd']=df_test['Exterior2nd'].fillna(df_test['Exterior2nd'].mode() [0])
 df_test['Exterior1st']=df_test['Exterior1st'].fillna(df_test['Exterior1st'].mode() [0])
 df_test['BsmtFinSF1']=df_test['BsmtFinSF1'].fillna(df_test['BsmtFinSF1'].mean())
 df_test['BsmtFinSF2']=df_test['BsmtFinSF2'].fillna(df_test['BsmtFinSF2'].mean())
 df_test['TotalBsmtSF']=df_test['TotalBsmtSF'].fillna(df_test['TotalBsmtSF'].mean())
 df_test['GarageCars']=df_test['GarageCars'].fillna(df_test['GarageCars'].mean() )<feature_engineering>
Xtrain3 = train_data3
Xtrain3 = Xtrain3.drop('Id', axis=1)
 Xtrain3 = Xtrain3.drop('median_house_value', axis=1)
 results(Xtrain3, Ytrain )
df_test['LotArea']=np.log(df_test['LotArea'] )<categorify>
d = 8
n = 100
boost = AdaBoostRegressor(DecisionTreeRegressor(max_depth=d),
n_estimators=n)
 boost.fit(Xtrain3, Ytrain)
 Ypred = boost.predict(Xtrain3)
 boost_rmsle = rmsle(Ytrain, Ypred)
 print('error =', boost_rmsle )
df_test_work = df_test[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
lencoders_test = {}
for colt in df_test_work.select_dtypes(include=['object'] ).columns:
lencoders_test[colt] = LabelEncoder()
df_test_work[colt] = lencoders_test[colt].fit_transform(df_test_work[colt] )<choose_model_class>
X_train, X_validation, y_train, y_validation = train_test_split(Xtrain3, Ytrain, train_size=0.7)
 model=CatBoostRegressor(iterations=200, depth=6, learning_rate=0.2, loss_function='RMSE')
 model.fit(Xtrain3, Ytrain,eval_set=(X_validation, y_validation),plot=True)
 Ypred = model.predict(Xtrain3)
 model_rmsle = rmsle(Ytrain, Ypred)
 print('error =', model_rmsle )
reg_final = xgboost.XGBRegressor(base_score=bestP['base_score'],
colsample_bylevel=bestP['colsample_bylevel'], colsample_bynode=bestP['colsample_bynode'],
colsample_bytree=bestP['colsample_bytree'],
gamma=bestP['gamma'], learning_rate=bestP['learning_rate'],
max_depth=int(bestP['max_depth']), n_estimators=int(bestP['n_estimators']),
random_state=42, reg_lambda=bestP['reg_lambda'], subsample=bestP['subsample'])
 reg_final.fit(x_train,y_train)
 y_pred = reg_final.predict(df_test_work)
 prediction = pd_out.DataFrame(y_pred)
 output = pd_out.concat([df_test_work['Id'],prediction], axis=1)
 output.columns=['Id','SalePrice']
output.to_csv('HousePrice_submission.csv', index=False)
 print("Submission successful" )<install_modules>
test_data2 = test_data
sea_distance=[]
for i,c in test_data2.iterrows() :
b=np.array(( c['latitude'],c['longitude']))
D=[]
for j in california_sea:
D.append(np.linalg.norm(b-j))
sea_distance.append(min(D))
test_data2 = test_data2.join(pd.Series(sea_distance,name="sea_distance"))
Los_Angeles =(34.155652, -118.600019)
 San_Francisco =(37.775, -122.4183)
 la_sf_distance = []
for i,c in test_data2.iterrows() :
b=np.array(( c['latitude'],c['longitude']))
D=[]
D.append(np.linalg.norm(b-Los_Angeles))
D.append(np.linalg.norm(b-San_Francisco))
la_sf_distance.append(min(D))
test_data2 = test_data2.join(pd.Series(la_sf_distance,name="LA_SF_distance"))
test_data2["rooms_per_household"] = test_data2["total_rooms"]/test_data2["households"]
test_data2["bedrooms_per_room"] = test_data2["total_bedrooms"]/test_data2["total_rooms"]
test_data2["population_per_household"] = test_data2["population"]/test_data2["households"]
test_data2["income_per_person"] = test_data2["median_income"]/test_data2["population_per_household"]
test_data2['mean_rooms'] = test_data2['total_rooms']/test_data2['households']
test_data2['rooms_per_person'] = test_data2['total_rooms']/test_data2['population']
test_data2['mean_bedrooms'] = test_data2['total_bedrooms']/test_data2['households']
test_data2['bedrooms_per_person'] = test_data2['total_bedrooms']/test_data2['households']
test_data2['persons_per_household'] = test_data2['population']/test_data2['households']
test_data2['total_income'] = test_data2['median_income']*test_data2['households']
!pip install face_recognition<categorify>
Xtest2 = test_data2.drop('Id', axis=1 )
path = '.. /input/comlfaces/'
known_face_encodings = []
known_face_names = []
for i in os.listdir(path):
try:
known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(path+i)) [0])
 known_face_names.append(i.split('_')[0].capitalize() + " " + i.split('_')[1].capitalize())
 print('added ' + i)
 except:
print('FAILED ' + i)
 print('
Done!' )<load_from_csv>
forest = RandomForestRegressor(max_depth=21, random_state=0, n_estimators=1000)
 forest.fit(Xtrain3, Ytrain)
 Ypred = forest.predict(Xtrain3)
 forest_rmsle = rmsle(Ytrain, Ypred)
 print('log error =', forest_rmsle)
 prediction = forest.predict(Xtest2 )
<load_from_csv><EOS> 
arq = open("submission.csv", "w")
 arq.write("Id,median_house_value
")
 for i, j in zip(test_data2['Id'], prediction):
arq.write(str(i)+ "," + str(j)+"
")
 arq.close()