Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<set_options>
def exportResults(classifier, idnumber,validationdf):
validationdf=pd.DataFrame({"Id" : list(validationdf.index), "SalePrice" : list(classifier.predict(validationdf)) })
 validationdf.to_csv("test{}.csv".format(idnumber), index=False )
print(time.time() - start_time)
 gc.collect()<import_modules>
print(check_output(["ls", ".. /input"] ).decode("utf8"))
train = pd.read_csv(".. /input/train.csv", index_col=0)
 validation = pd.read_csv(".. /input/test.csv", index_col=0)
 target = train['SalePrice']
df_train = train.drop('SalePrice', axis=1)
 df_train['training_set'] = True
validation['training_set'] = False
df_full = pd.concat([df_train, validation])
import pandas as pd
import numpy as np
import torch<load_from_csv>
df_full.drop(['FireplaceQu','MiscFeature'], inplace=True, axis=1)
 df_full['Alley']=df_full.loc[:,'Alley'].fillna('Asphalt')
 df_full['PoolQC']=df_full.loc[:,'PoolQC'].fillna('NA')
 df_full['Fence']=df_full.loc[:,'Fence'].fillna('NA')
 print(df_full.info())
 for feature in df_full.select_dtypes(include=['object'] ).columns:
replaceValue=df_full.loc[:,feature].value_counts().idxmax()
df_full[feature]=df_full.loc[:,feature].fillna(replaceValue)
 df_full = pd.get_dummies(df_full)
 print(df_full.shape)
 print(df_full.info())
df_train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 df_train.head()<load_from_csv>
df_train_skewed = df_full_skewed[df_full_skewed['training_set']==True]
df_train_skewed = df_train_skewed.drop('training_set', axis=1)
 df_test_skewed = df_full_skewed[df_full_skewed['training_set']==False]
df_test_skewed = df_test_skewed.drop('training_set', axis=1)
 X_train, X_test, y_train, y_test = train_test_split(df_train_skewed, target_skewed, test_size=0.2, random_state=42 )
df_test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 df_test.head()<count_values>
rf_skewed = RandomForestRegressor(n_estimators=100, n_jobs=-1)
 rf_skewed.fit(df_train_skewed, target_skewed)
 cv_num=4
scores = cross_val_score(rf_skewed, df_train_skewed, target_skewed, cv=cv_num, scoring='neg_mean_squared_error')
 print("cross val scores(skewed dataset):" , np.sqrt(( -scores)))
 print('average root mean squared log error(skewed dataset)=', np.mean(np.sqrt(-scores)))
 preds_skewed = np.expm1(rf_skewed.predict(df_test_skewed))
df_train.target.value_counts()<split>
model_lasso = LassoCV(alphas = np.logspace(-4, -2, 20), cv = 4, n_jobs=4, max_iter=500)
 model_lasso.fit(df_train_skewed, target_skewed)
 scores = cross_val_score(model_lasso, df_train_skewed, target_skewed, cv=cv_num, scoring='neg_mean_squared_error')
 print("cross val scores(lasso):" , np.sqrt(-scores))
print('average root mean squared log error(lasso)=', np.mean(np.sqrt(-scores)))
 preds_lasso_skewed = np.expm1(model_lasso.predict(df_test_skewed))
xtrain,xval,ytrain,yval = train_test_split(df_train.index.values, df_train.target.values,
test_size = 0.2, random_state=15, stratify = df_train.target.values)
 print(len(xtrain),len(xval))<feature_engineering>
reoptimize=False
nestim = 1921
if(reoptimize):
learning_rate_grid = np.linspace(0.05, 0.05, 1)
 max_depth_grid = np.linspace(1, 5, 5, dtype='int')
 n_estimators_grid = np.linspace(1000, 2500, 250, dtype='int')
 xgbgrid={
'n_estimators' : n_estimators_grid }
xgb_cv=GridSearchCV(XGBRegressor(colsample_bytree=0.2), xgbgrid, cv=cv_num, refit=True, n_jobs=-1, scoring='neg_mean_squared_log_error')
 xgb_cv.fit(df_train_skewed, target_skewed)
 print(xgb_cv.best_params_)
 nestim = xgb_cv.best_params_['n_estimators']
df_train['set_type'] = 'nil'*df_train.shape[0]
df_train.loc[xtrain, 'set_type'] = 'train'
df_train.loc[xval, 'set_type'] = 'val'
df_train.head(10 )<groupby>
class CustomEnsembleRegressor(BaseEstimator, RegressorMixin):
def __init__(self, regressors=None):
self.regressors = regressors
def fit(self, X, y):
for regressor in self.regressors:
regressor.fit(X, y)
 def predict(self, X):
self.predictions_ = list()
for regressor in self.regressors:
self.predictions_.append(( regressor.predict(X ).ravel()))
return(np.mean(self.predictions_, axis=0))
xgb1 = XGBRegressor(colsample_bytree=0.02,
learning_rate=0.05,
max_depth=3,
n_estimators=nestim 
)
 xgb2 = XGBRegressor(colsample_bytree=0.2,
learning_rate=0.05,
max_depth=3,
n_estimators=nestim,
seed = 1234 
)
 xgb3 = XGBRegressor(colsample_bytree=0.2,
learning_rate=0.05,
max_depth=3,
n_estimators=nestim,
seed = 1337 
)
 xgb_ens = CustomEnsembleRegressor([xgb1,xgb2,xgb3])
 xgb_ens.fit(df_train_skewed, target_skewed)
 scores = cross_val_score(cv=cv_num,estimator=xgb_ens,X = df_train_skewed,y = target_skewed, n_jobs = -1, scoring='neg_mean_squared_error')
 print("cross val scores(xgboost ensemble):" , np.sqrt(-scores))
print('average root mean squared log error(xgboost ensemble)=', np.mean(np.sqrt(-scores)))
 preds_xgb_skewed = np.expm1(xgb_ens.predict(df_test_skewed))
<load_pretrained><EOS> 
preds_avg=(preds_lasso_skewed+preds_xgb_skewed)/2
my_submission_avg = pd.DataFrame({'Id': df_test_skewed.index, 'SalePrice': preds_avg})
 my_submission_avg.to_csv('submission_avg.csv', index=False )