Kaggle competition: Walmart Recruiting - Store Sales Forecasting
<SOS> metric: WMAE Kaggle data source: walmart-recruiting-store-sales-forecasting<count_values>
warnings.filterwarnings("ignore")
 enable_pandas_profilling = False
enable_tpot = False
houses_train[rank_columns].apply(pd.Series.value_counts )<categorify>
df_features = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/features.csv.zip')
 df_train = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/train.csv.zip')
 df_stores = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/stores.csv')
 df_test = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/test.csv.zip')
 df_sample_submission = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/sampleSubmission.csv.zip' )
for i in rank_columns:
for j in [houses_train, houses_test]:
j[i]= j[i].map({'None' : 0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5} )<define_variables>
df_feat_stores = df_features.merge(df_stores, how='inner', on='Store' )
outliers = pd.DataFrame({'column' : [], 'min' : [], 'max' : [], 'mean' : [], 'iqr' : [], 'lower_threshold' : [],
'upper_threshold' : [], 'outliers_qty' : []})
 for i in quantitive_columns:
df = houses_train[houses_train[i] != 0][i]
q1 = df.quantile(0.25)
 q3 = df.quantile(0.75)
 cl_min, cl_max, cl_mean = df.min() , df.max() , df.mean()
iqr = q3 - q1
threshold_min = q1 - 3*iqr
threshold_max = q3 + 3*iqr
qty = df[df < threshold_min].count() + df[df > threshold_max].count()
outliers = outliers.append({'column' : i, 'min' : cl_min, 'max' : cl_max, 'mean' : cl_mean, 'iqr' : iqr,
'lower_threshold' : threshold_min, 'upper_threshold' : threshold_max, 'outliers_qty' : qty},
ignore_index=True)
 outliers.sort_values(by='outliers_qty', ascending=False )<categorify>
pd.DataFrame({'Type_Feat_Store': df_feat_stores.dtypes,'Type_Train': df_train.dtypes, 'Type_Test': df_test.dtypes} )
outliers.set_index('column', drop=True, inplace=True)
 outliers = outliers.to_dict(orient='index' )<filter>
df_feat_stores.Date = pd.to_datetime(df_feat_stores.Date)
 df_train.Date = pd.to_datetime(df_train.Date)
 df_test.Date = pd.to_datetime(df_test.Date )
outliers_index_list = []
for i in ['LotArea', 'LotFrontage', 'OpenPorchSF', 'WoodDeckSF', 'TotalBsmtSF', 'GrLivArea']:
a = list(houses_train[(houses_train[i] < outliers[i]['lower_threshold'])| 
(houses_train[i] > outliers[i]['upper_threshold'])].index)
 for i in a:
outliers_index_list.append(i)
 outliers_index_list = list(set(outliers_index_list))
print(len(outliers_index_list))<normalization>
len(df_feat_stores.Date.unique())/52
scaler = StandardScaler()
warnings.filterwarnings('ignore')
 houses_train.loc[:, quantitive_columns] = scaler.fit_transform(houses_train.loc[:, quantitive_columns])
 houses_test.loc[:, quantitive_columns] = scaler.transform(houses_test.loc[:, quantitive_columns])
 warnings.filterwarnings('default' )<concatenate>
df_feat_stores['Week'] = df_feat_stores.Date.dt.week
df_feat_stores['Year'] = df_feat_stores.Date.dt.year
houses_train.shape[0]
houses_full = pd.concat([houses_train, houses_test] )<categorify>
df_train_feats = df_train.merge(df_feat_stores, how='inner', on=['Store','Date','IsHoliday'] ).sort_values(by=['Store','Dept','Date'] ).reset_index(drop=True)
 df_test_feats = df_test.merge(df_feat_stores, how='inner', on=['Store','Date','IsHoliday'] ).sort_values(by=['Store','Dept','Date'] ).reset_index(drop=True )
houses_full = pd.get_dummies(houses_full, drop_first=True)
 houses_train_ohe = houses_full[:houses_train.shape[0]]
houses_test_ohe = houses_full[houses_train.shape[0]:].drop('SalePrice', axis=1 )<create_dataframe>
del df_feat_stores, df_train, df_test
houses_train_ord = houses_train.copy()
houses_test_ord = houses_test.copy()<categorify>
if enable_pandas_profilling:
profile_train = ProfileReport(df_train_feats, title='Training Dataset Report', explorative = True)
 profile_train.to_file(output_file='profile_train.html')
 profile_train
encoder = OrdinalEncoder()
encoder.fit(pd.concat([houses_train[categorical_columns], houses_test[categorical_columns]]))
houses_train_ord[categorical_columns] = encoder.transform(houses_train[categorical_columns] ).astype('int64')
 houses_test_ord[categorical_columns] = encoder.transform(houses_test[categorical_columns] ).astype('int64' )<prepare_x_and_y>
df_train_feats = df_train_feats.drop(columns=['MarkDown1','MarkDown2','MarkDown3','MarkDown4','MarkDown5'])
 df_test_feats = df_test_feats.drop(columns=['MarkDown1','MarkDown2','MarkDown3','MarkDown4','MarkDown5'] )
x_houses_train_ord = houses_train_ord.drop('SalePrice', axis=1)
 y_houses_train_ord = houses_train_ord['SalePrice']
x_houses_train_ohe = houses_train_ohe.drop('SalePrice', axis=1)
 y_houses_train_ohe = houses_train_ohe['SalePrice']<init_hyperparams>
corr['Weekly_Sales'].sort_values()
lgb_space = {'n_estimators' : hp.quniform('n_estimators', 1000, 3000, 50),
'learning_rate': hp.uniform('learning_rate', 0.00001, 0.4),
'max_depth': hp.quniform('max_depth', 3, 30, 1),
'num_leaves' : hp.quniform('num_leaves', 5, 31, 5),
'min_child_samples' : hp.quniform('min_child_samples', 5, 41, 5),
'subsample' : hp.uniform('subsample', 0.6, 1),
'colsample_bytree' : hp.uniform('colsample_bytree', 0.6, 1),
'reg_alpha' : hp.uniform('reg_alpha', 1, 15),
'reg_lambda' : hp.uniform('reg_lambda', 1, 15)}<train_on_grid>
df_train_feats = df_train_feats.drop(columns=['Fuel_Price', 'Temperature','Date', 'CPI', 'Unemployment'])
 df_test_feats = df_test_feats.drop(columns=['Fuel_Price', 'Temperature','Date', 'CPI', 'Unemployment'] )
def lgb_hyperopt(params):
params = {'n_estimators': int(params['n_estimators']),
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'num_leaves' : int(params['num_leaves']),
'min_child_samples' : int(params['min_child_samples']),
'subsample' : params['subsample'],
'colsample_bytree' : params['colsample_bytree'],
'reg_alpha' : params['reg_alpha'],
'reg_lambda' : params['reg_lambda']
}
lgb = lightgbm.LGBMRegressor(**params)
 score = cross_val_score(lgb, x_houses_train_ord, y_houses_train_ord, scoring='neg_mean_squared_error',
cv=5, n_jobs=-1 ).mean()
return -score<find_best_params>
corr['Weekly_Sales'].sort_values()
lgb_best = fmin(fn=lgb_hyperopt, space=lgb_space, max_evals=40, rstate=np.random.RandomState(12345), algo=tpe.suggest )<choose_model_class>
df_sales_2010_weekly = df_train_feats[df_train_feats.Year==2010]['Weekly_Sales'].groupby(df_train_feats['Week'] ).sum()
df_sales_2011_weekly = df_train_feats[df_train_feats.Year==2011]['Weekly_Sales'].groupby(df_train_feats['Week'] ).sum()
df_sales_2012_weekly = df_train_feats[df_train_feats.Year==2012]['Weekly_Sales'].groupby(df_train_feats['Week'] ).sum()
lgb_final = lightgbm.LGBMRegressor(n_estimators = int(lgb_best['n_estimators']), learning_rate = lgb_best['learning_rate'],
max_depth = int(lgb_best['max_depth']), num_leaves = int(lgb_best['num_leaves']),
min_child_samples = int(lgb_best['min_child_samples']), subsample = lgb_best['subsample'],
colsample_bytree = lgb_best['colsample_bytree'], reg_alpha = lgb_best['reg_alpha'],
reg_lambda = lgb_best['reg_lambda'] )<train_model>
df_train_feats.loc[(df_train_feats.Year==2010)&(df_train_feats.Week==13), 'IsHoliday'] = True
df_train_feats.loc[(df_train_feats.Year==2011)&(df_train_feats.Week==16), 'IsHoliday'] = True
df_train_feats.loc[(df_train_feats.Year==2012)&(df_train_feats.Week==14), 'IsHoliday'] = True
df_test_feats.loc[(df_test_feats.Year==2013)&(df_test_feats.Week==13), 'IsHoliday'] = True
lgb_final.fit(x_houses_train_ord, y_houses_train_ord, categorical_feature=categorical_columns )<compute_train_metric>
def nominal_to_ordinal(x):
if x == 'A':
return 3
elif x == 'B':
return 2
else:
return 1
df_train_feats.Type = df_train_feats.Type.apply(nominal_to_ordinal)
 df_test_feats.Type = df_test_feats.Type.apply(nominal_to_ordinal )
mean_squared_error(y_houses_train_ord, lgb_final.predict(x_houses_train_ord), squared=False )<predict_on_test>
def bool_to_num(x):
if x:
return 1
else:
return 0
df_train_feats.IsHoliday = df_train_feats.IsHoliday.apply(bool_to_num)
 df_test_feats.IsHoliday = df_test_feats.IsHoliday.apply(bool_to_num )
submission_lgb = pd.DataFrame({'Id': houses_test.index, 'SalePrice' : lgb_final.predict(houses_test_ord)} )<save_to_csv>
X_train = df_train_feats.drop('Weekly_Sales', axis= 1 ).values
y_train = df_train_feats.Weekly_Sales.values
submission_lgb.to_csv('new_submission_lgb.csv', index=False )<init_hyperparams>
if enable_tpot:
X_tpot_train, X_tpot_test, y_tpot_train, y_tpot_test = train_test_split(X_train, y_train, train_size=0.75, test_size=0.25, random_state=42)
 tpot = TPOTRegressor(generations=5, population_size=50, verbosity=2, random_state=42)
 tpot.fit(X_tpot_train, y_tpot_train)
 print(tpot.score(X_tpot_test, y_tpot_test))
tpot.export('tpot_pipeline.py' )
xgb_space = {'n_estimators':hp.quniform('n_estimators', 1000, 4000, 50),
'gamma':hp.uniform('gamma', 0.01, 0.1),
'learning_rate':hp.uniform('learning_rate', 0.00001, 0.4),
'max_depth':hp.quniform('max_depth', 3,30,1),
'subsample':hp.uniform('subsample', 0.60, 0.95),
'colsample_bytree':hp.uniform('colsample_bytree', 0.60, 0.98),
'colsample_bylevel':hp.uniform('colsample_bylevel', 0.60, 0.98),
'reg_lambda': hp.uniform('reg_lambda', 1, 20)
 }<train_on_grid>
model = RandomForestRegressor(n_estimators=100, max_depth=27, max_features=6, min_samples_split=8, min_samples_leaf=1)
 model.fit(X_train, y_train)
 preds = model.predict(X_train)
 rmse_all = np.sqrt(mean_squared_error(y_train, preds))
print("RMSE: %f" %(rmse_all))
del model
def xgb_hyperopt(params):
params = {'n_estimators': int(params['n_estimators']),
'gamma': params['gamma'],
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'colsample_bylevel': params['colsample_bylevel'],
'reg_lambda': params['reg_lambda']
}
xgb = xgboost.XGBRegressor(**params)
 score = cross_val_score(xgb, x_houses_train_ohe, y_houses_train_ohe, scoring='neg_mean_squared_error',
cv=5, n_jobs=-1 ).mean()
return -score<find_best_params>
model = RandomForestRegressor(n_estimators=50, max_depth=27, max_features=6, min_samples_split=8, min_samples_leaf=1)
 model.fit(X_train, y_train)
 preds = model.predict(X_train)
 rmse_all = np.sqrt(mean_squared_error(y_train, preds))
print("RMSE: %f" %(rmse_all))
del model
xgb_best = fmin(fn=xgb_hyperopt, space=xgb_space, max_evals=40, rstate=np.random.RandomState(12345), algo=tpe.suggest )<choose_model_class>
model = RandomForestRegressor(n_estimators=50, max_depth=27, max_features=6, min_samples_split=4, min_samples_leaf=1)
 model.fit(X_train, y_train)
 preds = model.predict(X_train)
 rmse_all = np.sqrt(mean_squared_error(y_train, preds))
print("RMSE: %f" %(rmse_all))
del model
xgb_final = xgboost.XGBRegressor(n_estimators = int(xgb_best['n_estimators']), colsample_bytree = xgb_best['colsample_bytree'],
gamma = xgb_best['gamma'], learning_rate = xgb_best['learning_rate'],
max_depth = int(xgb_best['max_depth']), subsample = xgb_best['subsample'],
colsample_bylevel = xgb_best['colsample_bylevel'], reg_lambda = xgb_best['reg_lambda'] )<train_model>
model = RandomForestRegressor(n_estimators=50, max_depth=27, max_features=6, min_samples_split=3, min_samples_leaf=1)
 model.fit(X_train, y_train)
 preds = model.predict(X_train)
 rmse_all = np.sqrt(mean_squared_error(y_train, preds))
print("RMSE: %f" %(rmse_all))
del model
xgb_final.fit(x_houses_train_ohe, y_houses_train_ohe )<compute_train_metric>
model = RandomForestRegressor(n_estimators=50, max_depth=27, max_features=6, min_samples_split=2, min_samples_leaf=1)
 model.fit(X_train, y_train)
 preds = model.predict(X_train)
 rmse_all = np.sqrt(mean_squared_error(y_train, preds))
print("RMSE: %f" %(rmse_all))
mean_squared_error(y_houses_train_ohe, xgb_final.predict(x_houses_train_ohe), squared=False )<create_dataframe>
predicted = model.predict(df_test_feats )
<save_to_csv><EOS> 
df_sample_submission['Weekly_Sales'] = predicted
df_sample_submission.to_csv('submission.csv',index=False )