Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<save_to_csv>
import pandas as pd
import numpy as np
import os
import lightgbm as lgb
sub = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/' + 'sample_submission.csv')
 sub['toxic'] = model.predict(test_dataset, verbose=1)
 sub.to_csv('submission.csv', index=False )<import_modules>
path = ".. /input/m5-forecasting-accuracy"
calendar = pd.read_csv(os.path.join(path, "calendar.csv"))
selling_prices = pd.read_csv(os.path.join(path, "sell_prices.csv"))
sample_submission = pd.read_csv(os.path.join(path, "sample_submission.csv"))
sales = pd.read_csv(os.path.join(path, "sales_train_evaluation.csv"))
!pip install -q googletrans
translator = Translator()
GRU,Embedding, Flatten, Dropout, Add, concatenate, MaxPooling1D,
GlobalAveragePooling1D, GlobalMaxPooling1D,
GlobalMaxPool1D,SpatialDropout1D)
 optimizers, layers, callbacks)
 sns.set(style="darkgrid" )<load_from_csv>
def prep_calendar(df):
df = df.drop(["date", "weekday", "event_type_1", "event_type_2"], axis=1)
 df = df.assign(d = df.d.str[2:].astype(int))
to_ordinal = ["event_name_1", "event_name_2"]
df[to_ordinal] = df[to_ordinal].fillna("1")
 df[to_ordinal] = OrdinalEncoder(dtype="int" ).fit_transform(df[to_ordinal])+ 1
to_int8 = ["wday", "month", "snap_CA", "snap_TX", "snap_WI"] + to_ordinal
df[to_int8] = df[to_int8].astype("int8")
 return df
calendar = prep_calendar(calendar)
 calendar.head()
dir = '/kaggle/input/jigsaw-multilingual-toxic-comment-classification'
train_set1 = pd.read_csv(os.path.join(dir, 'jigsaw-toxic-comment-train.csv'))
train_set2 = pd.read_csv(os.path.join(dir, 'jigsaw-unintended-bias-train.csv'))
train_set2.toxic = train_set2.toxic.round().astype(int)
 valid = pd.read_csv(os.path.join(dir, 'validation.csv'))
test = pd.read_csv(os.path.join(dir, 'test.csv'))<concatenate>
LAGS = [7, 28]
WINDOWS = [7, 28]
FIRST = 1942
LENGTH = 28
def demand_features(df):

 for lag in LAGS:
df[f'lag_t{lag}'] = df.groupby('id')['demand'].transform(lambda x: x.shift(lag)).astype("float32")
 for w in WINDOWS:
df[f'rolling_mean_lag{lag}_w{w}'] = df.groupby('id')[f'lag_t{lag}'].transform(lambda x: x.rolling(w ).mean() ).astype("float32")
 return df
def demand_features_eval(df):

 out = df.groupby('id', sort=False ).last()
for lag in LAGS:
out[f'lag_t{lag}'] = df.groupby('id', sort=False)['demand'].nth(-lag-1 ).astype("float32")
 for w in WINDOWS:
out[f'rolling_mean_lag{lag}_w{w}'] = df.groupby('id', sort=False)['demand'].nth(list(range(-lag-w, -lag)) ).groupby('id', sort=False ).mean().astype("float32")
 return out.reset_index()
def prep_data(df, drop_d=1000):

 df = df.drop(["d_" + str(i+1)for i in range(drop_d)], axis=1)
 df = df.assign(id=df.id.str.replace("_evaluation", ""))
df = df.reindex(columns=df.columns.tolist() + ["d_" + str(FIRST + i)for i in range(LENGTH)])
 df = df.melt(id_vars=["id", "item_id", "store_id", "state_id", "dept_id", "cat_id"], var_name='d', value_name='demand')
 df = df.assign(d=df.d.str[2:].astype("int64"),
demand=df.demand.astype("float32"))
df = demand_features(df)
 df = df[df.d >(drop_d + max(LAGS)+ max(WINDOWS)) ]
df = df.merge(calendar, how="left", on="d")
 df = df.merge(selling_prices, how="left", on=["store_id", "item_id", "wm_yr_wk"])
 df = df.drop(["wm_yr_wk"], axis=1)
 for v in ["item_id", "store_id", "state_id", "dept_id", "cat_id"]:
df[v] = OrdinalEncoder(dtype="int" ).fit_transform(df[[v]] ).astype("int16")+ 1
x = list(set(df.columns)- {'id', 'd', 'demand'})
 test = df[df.d >= FIRST - max(LAGS)- max(WINDOWS)- 28]
df = df[df.d < FIRST]
xtrain, xvalid, ytrain, yvalid = train_test_split(df[x], df["demand"], test_size=0.1, shuffle=True, random_state=54)
 train = lgb.Dataset(xtrain, label = ytrain)
 valid = lgb.Dataset(xvalid, label = yvalid)
 return train, valid, test, x
def fit_model(train, valid):

 params = {
'metric': 'rmse',
'objective': 'poisson',
'seed': 200,
'force_row_wise' : True,
'learning_rate' : 0.08,
'lambda': 0.1,
'num_leaves': 63,
'sub_row' : 0.7,
'bagging_freq' : 1,
'colsample_bytree': 0.7
}
fit = lgb.train(params,
train,
num_boost_round = 2000,
valid_sets = [valid],
early_stopping_rounds = 200,
verbose_eval = 100)
 lgb.plot_importance(fit, importance_type="gain", precision=0, height=0.5, figsize=(6, 10)) ;
return fit
def pred_all(fit, test, x):

 for i, day in enumerate(np.arange(FIRST, FIRST + LENGTH)) :
test_day = demand_features_eval(test[(test.d <= day)&(test.d >= day - max(LAGS)- max(WINDOWS)) ])
 test.loc[test.d == day, "demand"] = fit.predict(test_day[x])
 return test
def pred_to_csv(test, cols=sample_submission.columns, file="submission.csv"):

 test = test.assign(id=test.id + "_" + np.where(test.d < FIRST, "validation", "evaluation"),
F="F" +(test.d - FIRST + LENGTH + 1 - LENGTH *(test.d >= FIRST)).astype("str"))
submission = test.pivot(index="id", columns="F", values="demand" ).reset_index() [cols].fillna(1)
 submission.to_csv(file, index=False)
 return True
train = pd.concat([
train_set1[['comment_text', 'toxic']],
train_set2[['comment_text', 'toxic']].query('toxic==1'),
train_set2[['comment_text', 'toxic']].query('toxic==0' ).sample(n=100000, random_state=0)
 ] )<count_values>
train, valid, test, x = prep_data(sales, 1000 - 28)
 fit = fit_model(train, valid)
 pred = pred_all(fit, test, x)
 pred_to_csv(pred, cols=sample_submission.columns, file="submission.csv" )