Kaggle competition: Atividade_3_PMR3508
<SOS> metric: rmsle Kaggle data source: atividade_3_pmr3508<prepare_x_and_y>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix,fbeta_score
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
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>
trainDb = pd.read_csv(".. /input/mycalifdb/train.csv")
 testDb = pd.read_csv(".. /input/mycalifdb/test.csv" )
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>
def RMSLE(Y, Y_hat):
sumError = 0.0
assert len(Y)== len(Y_hat)
 for i in range(len(Y)) :
if Y[i] < 0: Y[i] = 0
sumError +=(np.log(Y[i] + 1)- np.log(Y_hat[i] + 1)) **2
return np.sqrt(sumError/len(Y))
df_train['LotArea']=np.log(df_train['LotArea'] )<drop_column>
Xtrain = trainDb.drop("Id", axis = 1 ).drop("median_house_value", axis = 1)
 Xtest = testDb.drop("Id", axis = 1)
 Ytrain = trainDb["median_house_value"]
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>
kNNMethod = KNeighborsRegressor(n_neighbors=10)
 kNNMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(kNNMethod, Xtrain, Ytrain, cv=3)
 Y = kNNMethod.predict(Xtrain)
 print('Pontuação média kNN: ' + str(scores.mean()))
print('RMSLE kNN: ' + str(RMSLE(Y, Ytrain)) )
y_train = df_train['SalePrice']<set_options>
lassoMethod = linear_model.Lasso(alpha = 0.1)
 lassoMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(lassoMethod, Xtrain, Ytrain, cv=3)
 Y = lassoMethod.predict(Xtrain)
 print('Pontuação média Lasso: ' + str(scores.mean()))
print('RMSLE Lasso: ' + str(RMSLE(Y, Ytrain)) )
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>
forestMethod = RandomForestRegressor(max_depth=25, random_state=0, n_estimators=100)
 forestMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(forestMethod, Xtrain, Ytrain, cv=3)
 Y = forestMethod.predict(Xtrain)
 print('Pontuação média Forest: ' + str(scores.mean()))
print('RMSLE Forest: ' + str(RMSLE(Y, Ytrain)) )
<feature_engineering><EOS> 
forestMethod = RandomForestRegressor(max_depth=25, random_state=0, n_estimators=100)
 forestMethod.fit(Xtrain, Ytrain)
 Ytest = forestMethod.predict(Xtest)
 predictionTest = pd.DataFrame({"Id":testDb.Id, "median_house_value":Ytest})
 predictionTest.to_csv("predictTable.csv", index=False)
 predictionTest