Kaggle competition: Walmart Recruiting - Store Sales Forecasting
na_cols = train_X.columns[(train_X.isna().sum() > 0 ).values]
print(f'Columns with NA: {na_cols}')
 train_X.loc[:, na_cols] = CustomImputer().fit_transform(train_X.loc[:, na_cols])
 test_X.loc[:, na_cols] = CustomImputer().fit_transform(test_X.loc[:, na_cols])
 valid_X.loc[:, na_cols] = CustomImputer().fit_transform(valid_X.loc[:, na_cols])
 train_X.loc[:, na_cols[train_X.loc[:, na_cols].dtypes == 'float64']] = train_X.loc[:, na_cols[train_X.loc[:, na_cols].dtypes == 'float64']].fillna(0 )<count_missing_values>
train = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/train.csv.zip",parse_dates=["Date"])
 test = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/test.csv.zip",parse_dates=["Date"] )
train_X.columns[train_X.isna().sum() > 0]<categorify>
add = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/features.csv.zip")
 add.head()
train_X = train_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
valid_X = valid_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
test_X = test_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
train_X = train_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
valid_X = valid_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
test_X = test_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
train_X = train_X.assign(QualPerRm=lambda df: get_quality_per_room(df))
valid_X = valid_X.assign(QualPerRm=lambda df: get_quality_per_room(df))
test_X = test_X.assign(QualPerRm=lambda df: get_quality_per_room(df))<drop_column>
store = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/stores.csv")
 store
ta_leakage_cols = ['YrSold', 'MoSold']
train_X = train_X.drop(ta_leakage_cols, axis=1)
 valid_X = valid_X.drop(ta_leakage_cols, axis=1)
 test_X =test_X.drop(ta_leakage_cols, axis=1 )<define_variables>
train = pd.merge(train,store,on = "Store",how="left")
 test = pd.merge(test,store,on = "Store",how="left" )
cat_features = train_X.columns[train_X.dtypes == 'object']
num_features = train_X.columns[(train_X.dtypes == 'int64')|(train_X.dtypes == 'float64')]<import_modules>
train["year"] = train["Date"].dt.year
train["month"] = train["Date"].dt.month
train["day"] = train["Date"].dt.day
train["week"] = train["Date"].dt.week
test["year"] = test["Date"].dt.year
test["month"] = test["Date"].dt.month
test["day"] = test["Date"].dt.day
test["week"] = test["Date"].dt.week
from sklearn import pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from category_encoders import CatBoostEncoder, OrdinalEncoder<categorify>
train["Type"] = train["Type"].replace({"A":0,"B":1,"C":2})
 test["Type"] = test["Type"].replace({"A":0,"B":1,"C":2} )
ordinal_features = [
'LandSlope',
'Condition1',
'Condition2',
'ExterQual',
'ExterCond',
'BsmtQual',
'BsmtCond',
'BsmtFinType1',
'BsmtFinType2',
'HeatingQC',
'KitchenQual',
'FireplaceQu',
'GarageQual',
'GarageCond',
'PoolQC'
]
nominal_features = cat_features.drop(ordinal_features)
 nominal_transformer = pipeline.Pipeline(steps=[ 
('cat_boost', CatBoostEncoder()), 
('scaler', StandardScaler())
 ])
 num_transformer = pipeline.Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value=0, missing_values=np.nan)) , 
('scaler', StandardScaler())
 ])
 num_ord_transformer = ColumnTransformer(transformers=[ 
('ordinal_transformer', OrdinalEncoder() , ordinal_features), 
('num_transformer', num_transformer, num_features)
 ])
 train_X_nominal = nominal_transformer.fit_transform(train_X[nominal_features], train_y)
 valid_X_nominal = nominal_transformer.transform(valid_X[nominal_features])
 test_X_nominal = nominal_transformer.transform(test_X[nominal_features])
 train_X_num_ord = num_ord_transformer.fit_transform(train_X)
 valid_X_num_ord = num_ord_transformer.transform(valid_X)
 test_X_num_ord = num_ord_transformer.transform(test_X)
 train_X_transformed = np.concatenate(( train_X_nominal, train_X_num_ord), axis=1)
 valid_X_transformed = np.concatenate(( valid_X_nominal, valid_X_num_ord), axis=1)
 test_X_transformed = np.concatenate(( test_X_nominal, test_X_num_ord), axis=1 )<import_modules>
def wmae(y_pred, targ, holiday_week):
sumOfWeights = 0
sumofCol_B_X_Col_E = 0
for i in range(0, len(y_pred)) :
weight = 0
if holiday_week[i]:
weight = 5
else:
weight = 1
Col_B_X_Col_E = abs(targ[i] - y_pred[i])*weight
sumOfWeights += weight
sumofCol_B_X_Col_E += Col_B_X_Col_E
WMAE = sumofCol_B_X_Col_E/sumOfWeights
return WMAE
from sklearn.model_selection import GridSearchCV, TimeSeriesSplit
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error<train_on_grid>
train.groupby("Type")["Weekly_Sales"].mean()
params = {
'learning_rate': np.arange(0.01, 0.11, 0.01)
 }
optimizer = GridSearchCV(XGBRegressor(n_estimators=500, tree_method='gpu_hist', max_depth=4, min_child_weight=3, gamma=0, colsample_bytree=0.4, subsample=0.7, learning_rate=0.05), params, cv=TimeSeriesSplit(n_splits=3), n_jobs=-1)
 optimizer.fit(train_X_transformed, train_y)
 valid_score = mean_absolute_error(valid_y, optimizer.predict(valid_X_transformed))<find_best_params>
feature = ["Store","Dept","year","month","day","week","IsHoliday", "Size"]
optimizer.best_params_<compute_test_metric>
X_train = train[feature]
X_test = test[feature]
print(f'Mean validation score is {valid_score}' )<train_model>
y_train = train["Weekly_Sales"]
xgb_params = {
'colsample_bytree': 0.4,
'n_estimators': 1000,
'min_child_weight': 3,
'max_depth': 6,
'subsample': 0.4,
'learning_rate': 0.01,
'gamma': 0,
'reg_lambda': 0.02
}
model = XGBRegressor(tree_method='gpu_hist', **xgb_params ).fit(train_X_transformed, train_y )<compute_test_metric>
X_train1, X_test1, y_train1, y_test1 = train_test_split(X_train,y_train, test_size=0.2 )
train_score = mean_absolute_error(train_y, model.predict(train_X_transformed))
print(f'Mean train score is {train_score}' )<compute_test_metric>
%%time
model = RandomForestRegressor(n_estimators=10,n_jobs=4)
valid_score = mean_absolute_error(valid_y, model.predict(valid_X_transformed))
print(f'Mean validation score is {valid_score}' )<save_to_csv>
model.fit(X_train1,y_train1 )
preds_test = model.predict(test_X_transformed)
 output = pd.DataFrame({'Id': test_X.index,
'SalePrice': preds_test})
 output.to_csv('submission.csv', index=False )<import_modules>
holidays = X_test1['IsHoliday'].to_numpy()
y_test1 = y_test1.to_numpy()
import pandas as pd
import numpy as np
from sklearn.preprocessing import OrdinalEncoder, StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
from hyperopt import fmin, tpe, hp, anneal, Trials
import lightgbm
import xgboost
import warnings<load_from_csv>
result = model.predict(X_test1 )
houses_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 houses_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id' )<count_duplicates>
WMAE = wmae(result, y_test1, holidays)
 print(WMAE )
houses_train.duplicated().sum()<define_variables>
grid_search.best_params_
categorical_columns, quantitive_columns, int_columns = [], [], []
for i in houses_train.columns:
if houses_train[i].dtype == 'O':
categorical_columns.append(i)
 elif houses_train[i].dtype == 'float64':
quantitive_columns.append(i)
 else:
int_columns.append(i)
 int_columns.remove('SalePrice')
 print(len(categorical_columns)+ len(int_columns)+ len(quantitive_columns))<concatenate>
import xgboost as xgb
prep = pd.concat([houses_train, houses_test])
 prep.info()<count_missing_values>
data_dmatrix = xgb.DMatrix(data=X_train,label=y_train )
print('Missing values Train:', houses_train.isna().sum().sum())
 print('Missing values Test:', houses_test.isna().sum().sum() )<data_type_conversions>
xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
for i in ['Alley', 'Fence']:
for j in [houses_train, houses_test]:
j[i] = j[i].fillna(0)
 j[i] = j[i].apply(lambda x: x if x == 0 else 1 )<data_type_conversions>
xg_reg.fit(X_train1,y_train1)
 result = xg_reg.predict(X_test1 )
for i in ['MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath',
'GarageYrBlt', 'GarageCars', 'GarageArea']:
for j in [houses_train, houses_test]:
j[i] = j[i].fillna(0 )<data_type_conversions>
WMAE = wmae(result, y_test1, holidays)
 print(WMAE )
for i in ['LotFrontage']:
average = houses_train[i].mean()
for j in [houses_train, houses_test]:
j[i] = j[i].fillna(average )<categorify>
%%time
model = RandomForestRegressor(n_estimators= 500)
 model.fit(X_train,y_train)
 result = model.predict(X_test )
for i in ['MSZoning', 'Exterior1st', 'Exterior2nd', 'Electrical', 'KitchenQual', 'SaleType']:
mode = houses_train[i].mode() [0]
for j in [houses_train, houses_test]:
j[i] = j[i].fillna(mode )<categorify>
sub = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/sampleSubmission.csv.zip" )
for i in ['Utilities', 'MasVnrType', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'Functional',
'FireplaceQu', 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond', 'PoolQC', 'MiscFeature']:
for j in [houses_train, houses_test]:
j[i] = j[i].fillna('None' )<count_missing_values>
sub["Weekly_Sales"] = result
sub.head()
<count_values><EOS> 
sub.to_csv("walmart_predict_sub.csv",index=False )