Kaggle competition: Predict Future Sales
numerical_columns_df['GarageYrBlt'] = numerical_columns_df['GarageYrBlt'].fillna(numerical_columns_df['YrSold']-35)
 numerical_columns_df['LotFrontage'] = numerical_columns_df['LotFrontage'].fillna(68)
<drop_column>
import numpy as np
import pandas as pd
import pickle
import time
import sys
import gc
import os
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor
object_columns_df = object_columns_df.drop(['Heating','RoofMatl','Condition2','Street','Utilities'],axis=1)
<filter>
INPUT_DIR='.. /input/analysed-finaldata/'
Neg_val = numerical_columns_df[numerical_columns_df['Age_House'] < 0]
Neg_val 
<feature_engineering>
final_tr_data=pd.read_pickle(INPUT_DIR+'final_tr_data_.pkl' )
numerical_columns_df['TotalBsmtBath'] = numerical_columns_df['BsmtFullBath'] + numerical_columns_df['BsmtFullBath']*0.5
numerical_columns_df['TotalBath'] = numerical_columns_df['FullBath'] + numerical_columns_df['HalfBath']*0.5
numerical_columns_df['TotalSA']=numerical_columns_df['TotalBsmtSF'] + numerical_columns_df['1stFlrSF'] + numerical_columns_df['2ndFlrSF'] 
<categorify>
dates = final_tr_data['date_block_num']
last_block = dates.max()
print('Test `date_block_num` is {0}'.format(last_block))
X_train = final_tr_data.loc[dates < last_block]
X_test = final_tr_data.loc[dates == last_block]
columns_to_delete = ['date_block_num', 'value']
X_train = X_train.drop(columns_to_delete, axis=1)
 X_test = X_test.drop(columns_to_delete, axis=1)
 X_train = X_train.values
X_test = X_test.values
y_train = final_tr_data.loc[dates < last_block, 'value'].values
y_test = final_tr_data.loc[dates == last_block, 'value'].values
X_valid_train = final_tr_data.loc[dates < last_block-1]
X_valid_test = final_tr_data.loc[dates == last_block-1]
columns_to_delete = ['date_block_num', 'value']
X_valid_train = X_valid_train.drop(columns_to_delete, axis=1)
 X_valid_test = X_valid_test.drop(columns_to_delete, axis=1)
 X_valid_train = X_valid_train.values
X_valid_test = X_valid_test.values
y_valid_train = final_tr_data.loc[dates < last_block-1, 'value'].values
y_valid_test = final_tr_data.loc[dates == last_block-1, 'value'].values
del dates
del final_tr_data
gc.collect()
bin_map = {'TA':2,'Gd':3, 'Fa':1,'Ex':4,'Po':1,'None':0,'Y':1,'N':0,'Reg':3,'IR1':2,'IR2':1,'IR3':0,"None" : 0,
"No" : 2, "Mn" : 2, "Av": 3,"Gd" : 4,"Unf" : 1, "LwQ": 2, "Rec" : 3,"BLQ" : 4, "ALQ" : 5, "GLQ" : 6
}
object_columns_df['ExterQual'] = object_columns_df['ExterQual'].map(bin_map)
 object_columns_df['ExterCond'] = object_columns_df['ExterCond'].map(bin_map)
 object_columns_df['BsmtCond'] = object_columns_df['BsmtCond'].map(bin_map)
 object_columns_df['BsmtQual'] = object_columns_df['BsmtQual'].map(bin_map)
 object_columns_df['HeatingQC'] = object_columns_df['HeatingQC'].map(bin_map)
 object_columns_df['KitchenQual'] = object_columns_df['KitchenQual'].map(bin_map)
 object_columns_df['FireplaceQu'] = object_columns_df['FireplaceQu'].map(bin_map)
 object_columns_df['GarageQual'] = object_columns_df['GarageQual'].map(bin_map)
 object_columns_df['GarageCond'] = object_columns_df['GarageCond'].map(bin_map)
 object_columns_df['CentralAir'] = object_columns_df['CentralAir'].map(bin_map)
 object_columns_df['LotShape'] = object_columns_df['LotShape'].map(bin_map)
 object_columns_df['BsmtExposure'] = object_columns_df['BsmtExposure'].map(bin_map)
 object_columns_df['BsmtFinType1'] = object_columns_df['BsmtFinType1'].map(bin_map)
 object_columns_df['BsmtFinType2'] = object_columns_df['BsmtFinType2'].map(bin_map)
 PavedDrive = {"N" : 0, "P" : 1, "Y" : 2}
object_columns_df['PavedDrive'] = object_columns_df['PavedDrive'].map(PavedDrive )<categorify>
lr = LinearRegression()
lr.fit(X_train, y_train)
 pred_lr = lr.predict(X_test ).clip(0,20 )
rest_object_columns = object_columns_df.select_dtypes(include=['object'])
 object_columns_df = pd.get_dummies(object_columns_df, columns=rest_object_columns.columns)
<concatenate>
submit = pd.DataFrame({'ID':range(len(pred_lr)) , 'item_cnt_month': pred_lr})
 submit.to_csv('submit_lr.csv', index=False )
df_final = pd.concat([object_columns_df, numerical_columns_df], axis=1,sort=False)
 df_final.head()<drop_column>
def validate(estimator, X_train_, y_train_, X_val_, y_val_, grid_params):
keys = grid_params.keys()
vals = grid_params.values()
parameters = []
rmses = []
rmses_train = []
return_obj ={}
prods = product(*vals)
 for idx, instance in enumerate(prods):
print('-'*50)
 print('model {0}:'.format(idx))
model_params = dict(zip(keys, instance))
parameters.append(model_params)
 print(model_params)
 model = estimator(**model_params)
 model.fit(X_train_, y_train_)
 pred_test = model.predict(X_val_)
 mse = mean_squared_error(y_val_, pred_test)
 rmse = np.sqrt(mse)
 print('RMSE: {0}'.format(rmse))
rmses = rmses + [rmse]
best_rmse_so_far = np.min(rmses)
 print('Best rmse so far: {0}'.format(best_rmse_so_far))
best_model_params_so_far = parameters[np.argmin(rmses)]
print('Best model params so far: {0}'.format(best_model_params_so_far))
del best_rmse_so_far
del best_model_params_so_far
del pred_test
del model
gc.collect()
rmses = np.array(rmses)
 best_rmse = np.min(rmses)
 print('Best rmse: {0}'.format(best_rmse))
best_model_params = parameters[np.argmin(rmses)]
print('Best model params: {0}'.format(best_model_params))
return_obj['rmses'] = rmses
return_obj['best_rmse'] = best_rmse
return_obj['best_model_params'] = best_model_params
return return_obj
df_final = df_final.drop(['Id',],axis=1)
 df_train = df_final[df_final['train'] == 1]
df_train = df_train.drop(['train',],axis=1)
 df_test = df_final[df_final['train'] == 0]
df_test = df_test.drop(['SalePrice'],axis=1)
 df_test = df_test.drop(['train',],axis=1 )<prepare_x_and_y>
alphas = [10, 100, 1000,2000, 3000, 4000, 5000]
grid_params = {'alpha':alphas}
val_res = validate(Ridge, X_valid_train, y_valid_train,
X_valid_test, y_valid_test, grid_params )
target= df_train['SalePrice']
df_train = df_train.drop(['SalePrice'],axis=1)
<split>
best_alpha=3000
ridge_model = Ridge(best_alpha)
 ridge_model.fit(X_train, y_train)
 predictions = ridge_model.predict(X_test )
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )<choose_model_class>
submit = pd.DataFrame({'ID':range(len(pred_lr)) , 'item_cnt_month': pred_lr})
 submit.to_csv('submit_ridge.csv', index=False )
xgb =XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1)
 lgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=12000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.4,
)<train_model>
best_params = {'learning_rate': 0.16, 'n_estimators': 500,
'max_depth': 6, 'min_child_weight': 7,
'subsample': 0.9, 'colsample_bytree': 0.7, 'nthread': -1,
'scale_pos_weight': 1, 'random_state': 42,
'tree_method': 'gpu_hist', 'predictor': 'gpu_predictor', 'gpu_id': 0}
xgb.fit(x_train, y_train)
 lgbm.fit(x_train, y_train,eval_metric='rmse')
<predict_on_test>
model = XGBRegressor(**best_params )
predict1 = xgb.predict(x_test)
 predict = lgbm.predict(x_test)
<compute_test_metric>
model = XGBRegressor(**best_params)
 model.fit(X_valid_train,
y_valid_train,
eval_metric="rmse",
eval_set=[(X_valid_test, y_valid_test)],
verbose=True,
early_stopping_rounds = 50 )
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict1))))
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict)))) 
<train_model>
pred = model.predict(X_test ).clip(0,20 )
xgb.fit(df_train, target)
 lgbm.fit(df_train, target,eval_metric='rmse')
<predict_on_test>
submit = pd.DataFrame({'ID':range(len(pred)) , 'item_cnt_month': pred})
 submit.to_csv('submit.csv', index=False )
predict4 = lgbm.predict(df_test)
 predict3 = xgb.predict(df_test)
 predict_y =(predict3*0.45 + predict4 * 0.55)
<save_to_csv>
filename = 'xgboost_model.sav'
joblib.dump(model, filename )
<install_modules><EOS> 
filename = 'test_data.sav'
joblib.dump(X_test, filename )