Kaggle competition: Corporación Favorita Grocery Sales Forecasting
<SOS> metric: NWRMSLE Kaggle data source: favorita-grocery-sales-forecasting<categorify>
from datetime import date, timedelta
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
x_predfinal=pd.get_dummies(x_test )<import_modules>
df_train = pd.read_csv(
 '.. /input/train.csv', usecols=[1, 2, 3, 4, 5],
dtype={'onpromotion': bool},
converters={'unit_sales': lambda u: np.log1p(
 float(u)) if float(u)> 0 else 0},
parse_dates=["date"],
skiprows=range(1, 66458909)
)
 df_test = pd.read_csv(
 ".. /input/test.csv", usecols=[0, 1, 2, 3, 4],
dtype={'onpromotion': bool},
parse_dates=["date"]
).set_index(
 ['store_nbr', 'item_nbr', 'date'] 
)
 items = pd.read_csv(
 ".. /input/items.csv",
).set_index("item_nbr" )
from sklearn.ensemble import BaggingRegressor<import_modules>
df_2017 = df_train[df_train.date.isin(
 pd.date_range("2017-05-31", periods=7 * 11)) ].copy()
del df_train
from sklearn import tree<import_modules>
promo_2017_train = df_2017.set_index(
 ["store_nbr", "item_nbr", "date"])[["onpromotion"]].unstack(
 level=-1 ).fillna(False)
 promo_2017_train.columns = promo_2017_train.columns.get_level_values(1)
 promo_2017_test = df_test[["onpromotion"]].unstack(level=-1 ).fillna(False)
 promo_2017_test.columns = promo_2017_test.columns.get_level_values(1)
 promo_2017_test = promo_2017_test.reindex(promo_2017_train.index ).fillna(False)
 promo_2017 = pd.concat([promo_2017_train, promo_2017_test], axis=1)
 del promo_2017_test, promo_2017_train
from sklearn.model_selection import train_test_split 
<split>
items = items.reindex(df_2017.index.get_level_values(1))
items.head()
X_train1, X_test1, y_train1, y_test1 = train_test_split(xx, y1_val, test_size=0.2)
 X_train2, X_test2, y_train2, y_test2 = train_test_split(xx, y2_val, test_size=0.2 )<choose_model_class>
def get_timespan(df, dt, minus, periods):
return df[
pd.date_range(dt - timedelta(days=minus), periods=periods)
 ]
model = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))<train_model>
def prepare_dataset(t2017, is_train=True):
X = pd.DataFrame({
"mean_3_2017": get_timespan(df_2017, t2017, 3, 3 ).mean(axis=1 ).values,
"mean_7_2017": get_timespan(df_2017, t2017, 7, 7 ).mean(axis=1 ).values,
"mean_14_2017": get_timespan(df_2017, t2017, 14, 14 ).mean(axis=1 ).values,
"promo_14_2017": get_timespan(promo_2017, t2017, 14, 14 ).sum(axis=1 ).values
})
 for i in range(16):
X["promo_{}".format(i)] = promo_2017[
t2017 + timedelta(days=i)].values.astype(np.uint8)
 if is_train:
y = df_2017[
pd.date_range(t2017, periods=16)
 ].values
return X, y
return X
model.fit(X_train1, y_train1 )<compute_test_metric>
print("Preparing dataset...")
 t2017 = date(2017, 6, 21)
 X_l, y_l = [], []
for i in range(6):
delta = timedelta(days=7 * i)
 X_tmp, y_tmp = prepare_dataset(
 t2017 + delta 
)
 X_l.append(X_tmp)
 y_l.append(y_tmp)
 X_train = pd.concat(X_l, axis=0)
 y_train = np.concatenate(y_l, axis=0)
 del X_l, y_l
X_test = prepare_dataset(date(2017, 8, 16), is_train=False )
model.score(X_test1,y_test1 )<predict_on_test>
print("Training and predicting models...")
 params = {
'num_leaves': 2**5 - 1,
'objective': 'regression_l2',
'max_depth': 8,
'min_data_in_leaf': 50,
'learning_rate': 0.05,
'feature_fraction': 0.75,
'bagging_fraction': 0.75,
'bagging_freq': 1,
'metric': 'l2',
'num_threads': 4
}
anssss=model.predict(x_predfinal )<feature_engineering>
MAX_ROUNDS = 1000
val_pred = []
test_pred = []
cate_vars = []
for i in range(16):
print("=" * 50)
 print("Step %d" %(i+1))
print("=" * 50)
 dtrain = lgb.Dataset(
 X_train, label=y_train[:, i],
categorical_feature=cate_vars,
weight=pd.concat([items["perishable"]] * 6)* 0.25 + 1 
)
 bst = lgb.train(
 params, dtrain, num_boost_round=700,
valid_sets=[dtrain], verbose_eval=50 
)
 print("
".join(( "%s: %.2f" % x)for x in sorted(
 zip(X_train.columns, bst.feature_importance("gain")) ,
key=lambda x: x[1], reverse=True 
)))
 test_pred.append(bst.predict(
 X_test, num_iteration=700))
a1=np.around(anssss )<data_type_conversions>
a1=a1.astype(int )<choose_model_class>
print("Making submission...")
 y_test = np.array(test_pred ).transpose()
df_preds = pd.DataFrame(
 y_test, index=df_2017.index,
columns=pd.date_range("2017-08-16", periods=16)
 ).stack().to_frame("unit_sales")
 df_preds.index.set_names(["store_nbr", "item_nbr", "date"], inplace=True)
 submission = df_test[["id"]].join(df_preds, how="left" ).fillna(0)
 submission["unit_sales"] = np.clip(np.expm1(submission["unit_sales"]), 0, 1000 )
model2 = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))<train_model>
submission.to_csv('lgb6w.csv', float_format='%.4f', index=None )