Kaggle competition: Rossmann Store Sales
del(installments_payments )<rename_columns>
sns.set(style='whitegrid', palette='muted')
 data_train = pd.read_csv('.. /input/train.csv',
dtype={
'StateHoliday': 'category',
'SchoolHoliday': 'category'},
parse_dates=['Date'])
 data_test = pd.read_csv('.. /input/test.csv',
dtype={
'StateHoliday': 'category',
'SchoolHoliday': 'category'},
parse_dates=['Date'])
 data_store = pd.read_csv('.. /input/store.csv',
dtype={
'StoreType': 'category',
'Assortment': 'category',
'CompetitionOpenSinceMonth': float,
'CompetitionOpenSinceYear': float,
'Promo2': float,
'Promo2SinceWeek': float,
'Promo2SinceYear': float})
 data_train = pd.merge(data_train, data_store, on='Store', how='left')
 data_train.head(5 )
application_data.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in application_data.columns]<choose_model_class>
def read_csv(files):
data_train = pd.read_csv(files[0],
dtype={
'StateHoliday': 'category',
'SchoolHoliday': 'int'},
parse_dates=['Date'])
 data_test = pd.read_csv(files[1],
dtype={
'StateHoliday': 'category',
'SchoolHoliday': 'int'},
parse_dates=['Date'])
 data_store = pd.read_csv(files[2],
dtype={
'StoreType': 'category',
'Assortment': 'category',
'CompetitionOpenSinceMonth': float,
'CompetitionOpenSinceYear': float,
'Promo2': float,
'Promo2SinceWeek': float,
'Promo2SinceYear': float,
'PromoInterval': str})
 return data_train, data_test, data_store
def combine(data, data_store):
return pd.merge(data, data_store, on='Store', how='left')
 def preprocess(data_train, data_test):
data_train_ = data_train.copy()
data_test_ = data_test.copy()
data_train_ = data_train_[(data_train_['Sales'] > 0)&(data_train_['Open'] != 0)]
data_test_['Open'] = data_test_['Open'].fillna(1)
 def process(data):
data['Year'] = data['Date'].dt.year
data['Month'] = data['Date'].dt.month
data['Day'] = data['Date'].dt.day
data['DayOfWeek'] = data['Date'].dt.dayofweek
data['DayOfYear'] = data['Date'].dt.dayofyear
data['WeekOfYear'] = data['Date'].dt.weekofyear
data['Quarter'] =(data['Date'].dt.month - 1)// 3 + 1
mappings = {'0': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
data['StoreType'].replace(mappings, inplace=True)
 data['Assortment'].replace(mappings, inplace=True)
 data['StateHoliday'].replace(mappings, inplace=True)
 data['CompetitionDistance'] = data['CompetitionDistance'].fillna(data['CompetitionDistance'].median())
 data['CompetitionOpen'] = 12 *(data['Year'] - data['CompetitionOpenSinceYear'])+(data['Month'] - data['CompetitionOpenSinceMonth'] ).apply(lambda x: x if x > 0 else 0)
 data['Promo2Open'] = 12 *(data['Year'] - data['Promo2SinceYear'])+(data['WeekOfYear'] - data['Promo2SinceWeek'])/ 4.0
data['Promo2Open'] = data['Promo2Open'].apply(lambda x: x if x > 0 else 0)
 data['PromoInterval'] = data['PromoInterval'].fillna('')
 data['InPromoMonth'] = data.apply(lambda x: 1 if(x['Date'].strftime('%b')if not x['Date'].strftime('%b')== 'Sep' else 'Sept')in x['PromoInterval'].split(',')else 0, axis=1)
 data_meanlog_salesbystore = data_train_.groupby(['Store'])['Sales'].mean().reset_index(name='MeanLogSalesByStore')
 data_meanlog_salesbystore['MeanLogSalesByStore'] = np.log1p(data_meanlog_salesbystore['MeanLogSalesByStore'])
 data = data.merge(data_meanlog_salesbystore, on=['Store'], how='left', validate='m:1')
 data_meanlog_salesbydow = data_train_.groupby(['DayOfWeek'])['Sales'].mean().reset_index(name='MeanLogSalesByDOW')
 data_meanlog_salesbydow['MeanLogSalesByDOW'] = np.log1p(data_meanlog_salesbystore['MeanLogSalesByStore'])
 data = data.merge(data_meanlog_salesbydow, on=['DayOfWeek'], how='left', validate='m:1')
 data_meanlog_salesbymonth = data_train_.groupby(['Month'])['Sales'].mean().reset_index(name='MeanLogSalesByMonth')
 data_meanlog_salesbymonth['MeanLogSalesByMonth'] = np.log1p(data_meanlog_salesbymonth['MeanLogSalesByMonth'])
 data = data.merge(data_meanlog_salesbymonth, on=['Month'], how='left', validate='m:1')
 return data
features = [
'Store', 'DayOfWeek', 'Year', 'Month', 'Day', 'DayOfYear', 'WeekOfYear', 'Quarter', 'Open', 'Promo',
'StateHoliday', 'SchoolHoliday', 'StoreType', 'Assortment',
'CompetitionDistance', 'CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear', 'CompetitionOpen',
'Promo2', 'Promo2SinceWeek', 'Promo2SinceYear', 'Promo2Open', 'InPromoMonth',
'MeanLogSalesByStore', 'MeanLogSalesByDOW', 'MeanLogSalesByMonth']
data_train_ = process(data_train_)
 data_test_ = process(data_test_)
 return(data_train_[features], np.log1p(data_train_['Sales'])) , data_test_[features]
model = lgb.LGBMClassifier()<filter>
pd.set_option('display.max_columns', None)
 pd.set_option('display.expand_frame_repr', False)
 pd.set_option('max_colwidth', -1)
 data_train, data_test, data_store = read_csv(['.. /input/train.csv', '.. /input/test.csv', '.. /input/store.csv'])
 data_train = combine(data_train, data_store)
 data_test = combine(data_test, data_store)
(X_train, y_train), X_test = preprocess(data_train, data_test )
train_data = application_data[application_data.IS_TRAIN == 1.0]<filter>
def rmspe(y_true, y_pred):
err = np.sqrt(np.mean(( 1 - y_pred / y_true)** 2))
return err
def rmspe_xgb(y_pred, y_true):
y_true = y_true.get_label()
err = rmspe(y_true, y_pred)
 return 'rmspe', err
test_data = application_data[application_data.IS_TRAIN == 0.0]<drop_column>
class Model:
def __init__(self, params=None, **kwargs):
self.params = params
self.kwargs = kwargs
def train(self, X, y):
X_train, X_test, y_train, y_test = X[41088:], X[:41088], y[41088:], y[:41088]
dtrain = xgb.DMatrix(X_train, label=y_train)
 dvalid = xgb.DMatrix(X_test, label=y_test)
 watchlist = [(dvalid, 'eval'),(dtrain, 'train')]
self.bst = xgb.train(self.params, dtrain, self.kwargs['num_boost_round'], evals=watchlist,
feval=rmspe_xgb, early_stopping_rounds=self.kwargs['early_stopping_rounds'],
verbose_eval=True)
 def predict(self, X, weight=0.995):
y_pred = np.expm1(weight * self.bst.predict(xgb.DMatrix(X)))
 return y_pred
def save_model(self, filename):
joblib.dump(self.bst, filename)
 def load_model(self, filename):
self.bst = joblib.load(filename )
train_data.drop(columns='IS_TRAIN', inplace=True)
 test_data.drop(columns='IS_TRAIN', inplace=True )<drop_column>
params = {
'eta': 0.03,
'max_depth': 10,
'subsample': 0.8,
'colsample_bytree': 0.7,
'lambda': 0.2,
'silent': 1,
'seed': 12
}
m = Model(params, num_boost_round=5000, early_stopping_rounds=50)
 m.train(X_train, y_train )
del(application_data )<find_best_params>
weight = 1.0
y_pred = m.predict(X_test, weight=weight)
 result = pd.DataFrame({'Id': data_test['Id'], 'Sales': y_pred})
 result.to_csv('submission.csv', index=False)
 result