Kaggle competition: M5 Forecasting - Accuracy
tokenizer = AutoTokenizer.from_pretrained(MODEL )<prepare_x_and_y>
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print("Memory usage of dataframe is {:.2f} MB".format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == "int":
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype("category")
 end_mem = df.memory_usage().sum() / 1024**2
print("Memory usage after optimization is: {:.2f} MB".format(end_mem))
print("Decreased by {:.1f}%".format(100 *(start_mem - end_mem)/ start_mem))
return df
%%time
x_train = regular_encode(train.comment_text.values,
tokenizer, maxlen=MAX_LEN)
 x_valid = regular_encode(valid.comment_text.values,
tokenizer, maxlen=MAX_LEN)
 x_test = regular_encode(test.content.values, tokenizer,
maxlen=MAX_LEN)
 y_train = train.toxic.values
y_valid = valid.toxic.values<define_variables>
submission = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sample_submission.csv')
 sales_df = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv')
 calendar_df = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 prices_df = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv' )
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(BATCH_SIZE)
 )<compute_train_metric>
submission = reduce_mem_usage(submission, True)
 sales_df = reduce_mem_usage(sales_df, True)
 calendar_df = reduce_mem_usage(calendar_df, True)
 prices_df = reduce_mem_usage(prices_df, True )
def label_smoothing(y_true,y_pred):
return tf.keras.losses.binary_crossentropy(y_true,y_pred,label_smoothing=0.15)
 def focal_loss(gamma=2., alpha=.2):
def focal_loss_fixed(y_true, y_pred):
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.mean(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) - K.mean(( 1 - alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
return focal_loss_fixed<choose_model_class>

 sales_df.sample(3 )
def build_model(transformer, max_len=512):

 input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 sequence_output = transformer(input_word_ids)[0]
cls_token = sequence_output[:, 0, :]
x = Dropout(0.3 )(cls_token)
 out = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=input_word_ids, outputs=out)
 model.compile(Adam(lr=1e-5), loss=label_smoothing,
metrics=[tf.keras.metrics.AUC() ])
 return model<choose_model_class>

 calendar_df.head(5 )
with strategy.scope() :
transformer_layer = TFAutoModel.from_pretrained(MODEL)
 model = build_model(transformer_layer, max_len=MAX_LEN)
 model.summary()<train_model>
calendar_df.event_name_1.value_counts()
calls = callback()
n_steps = x_train.shape[0] // BATCH_SIZE
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
callbacks = calls,
epochs=EPOCHS
)<train_model>
calendar_df.event_type_1.value_counts()
n_steps = x_valid.shape[0] // BATCH_SIZE
train_history_2 = model.fit(
 valid_dataset.repeat() ,
steps_per_epoch=n_steps,
epochs=EPOCHS
)<save_to_csv>
calendar_df.event_name_2.value_counts()
sub = pd.read_csv(dir + '/sample_submission.csv')
 sub['toxic'] = model.predict(test_dataset, verbose=1)
 sub.to_csv('submission.csv', index=False )<import_modules>
calendar_df.event_type_2.value_counts()
import os,re,gc,pickle,random,sys
import numpy as np
import pandas as pd
import tensorflow as tf
import transformers
from transformers import TFAutoModel
from tensorflow.data.experimental import sample_from_datasets
from tensorflow.data import Dataset
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm.notebook import tqdm<define_variables>
sales_df['sum_for_peariod'] = sales_df.iloc[:, 6:].sum(axis=1 )
np.random.seed(1234)
 random.seed(1234)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
with strategy.scope() :
tf.random.set_seed(1234 )<define_variables>
sales_df[sales_df['sum_for_peariod'] == sales_df['sum_for_peariod'].min() ]
MAX_LEN = 192
HEAD="cls"
MODEL = 'jplu/tf-xlm-roberta-large'
TRANSFER = ".. /input/buffer/submission-9455.csv"
PATH = "/kaggle/input/jigsaw-multilingual-toxic-comment-classification/"
INPATH = ".. /input/jwtc-xlmroberta-encoding-192-pickle/datain/"
FORM = "training/encode_{}.pkl"
langs = ["en","en2","es","fr","it","pt","ru","tr"]<data_type_conversions>
sales_df[sales_df['sum_for_peariod'] == sales_df['sum_for_peariod'].max() ]
trans = pd.read_csv(TRANSFER ).toxic.values.astype('float32' )<randomize_order>
sales_df.groupby('cat_id' ).sum_for_peariod.sum()
def pick_load_format(path):
with open(INPATH+path,"rb")as f:
return pickle.load(f)
 def get_cong(n,verb=True):
tot = round(1+(n*2)/10_000)*10_000
if verb: print("Pos: {}, Sample neg: {}, Total: {}".format(n,tot-n,tot))
return tot-n
def load_balance_shuffle_train(seed=1214):
train = []
for i in langs:
df1 = pick_load_format(FORM.format(i+"_l1"))
sample_size = get_cong(df1.shape[0])
 df0 = pick_load_format(FORM.format(i+"_l0")).sample(n=sample_size, random_state=seed)
 train += [df1,df0]
train = pd.concat(train ).sample(frac=1, random_state=seed)
 train.reset_index(inplace=True,drop=True)
 return np.stack(train.comment_text.values, axis=0 ).astype("int32"), train.toxic.values.astype("float32")
 def balance_shuffle(df,col="comment_text",pos_thred=0.6,neg_thred=0.4,balance=True,seed=1214):
label = df.toxic.values
df_pos = df[label>=pos_thred]
df_neg = df[label<=neg_thred]
psize,nsize = df_pos.shape[0],df_neg.shape[0]
cong = get_cong(psize,balance)
 if balance and nsize>cong:
df_neg = df_neg.sample(n=cong, random_state=seed)
 df0 = pd.concat([df_pos,df_neg] ).sample(frac=1, random_state=seed)
 df0.reset_index(inplace=True,drop=True)
 return np.stack(df0[col].values, axis=0 ).astype("int32"), df0.toxic.values
def load_data(pos_thred=0.6,neg_thred=0.4):
train = load_balance_shuffle_train()
valid = balance_shuffle(pick_load_format("valid.pkl"),balance=False)
 df_test = pick_load_format("test.pkl")
 df_test["toxic"] = trans
quesdo = balance_shuffle(df_test,col="content",balance=False,
pos_thred=pos_thred,neg_thred=neg_thred)
 return train,valid,quesdo,np.stack(df_test.content.values, axis=0 ).astype("int32" )<train_model>
sales_df.groupby(['cat_id', 'store_id'] ).sum_for_peariod.agg([min, max, np.mean, np.std] )
%%time
train,valid,quesdo,test = load_data(pos_thred=0.6,neg_thred=0.4)
 train_size,valid_size,quesdo_size = len(train[1]),len(valid[1]),len(quesdo[1])
 print(train_size,valid_size,quesdo_size )<categorify>
events_days = pd.concat([sales_df.loc[:, :'state_id'],
sales_df[calendar_df[:1913][~calendar_df.event_type_1[:1913].isna() ].d]],
axis = 1)
 events_days.sample(5 )
def make_dataset_pipeline(dataset, cache=False,repeat_and_shuffle=False,shuffle_size=128_000,seed=386491):
if cache: dataset = dataset.cache()
if repeat_and_shuffle:
dataset = dataset.repeat().shuffle(shuffle_size,seed)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def build_datasets(train,valid,quesdo,test):
dtrain = Dataset.from_tensor_slices(train)
 dvalid = Dataset.from_tensor_slices(valid)
 dquesdo = Dataset.from_tensor_slices(quesdo)
 dtest = Dataset.from_tensor_slices(test)
 train_dataset = make_dataset_pipeline(dtrain,True, repeat_and_shuffle=True)
 valid_dataset = make_dataset_pipeline(dvalid, True,repeat_and_shuffle=True)
 quesdo_dataset = make_dataset_pipeline(dquesdo, True,repeat_and_shuffle=True)
 validset = make_dataset_pipeline(dvalid)
 testset = make_dataset_pipeline(dtest)
 return train_dataset,valid_dataset,quesdo_dataset,validset,testset
def mix_dataset(dss,szs,weight=None,seed=1214):
if weight is None: weight = np.ones(len(szs))
prop = np.array(szs)*weight
return sample_from_datasets(dss,prop/np.sum(prop),seed),sum(szs )<categorify>
not_events_days = pd.concat([sales_df.loc[:, :'state_id'],
sales_df[calendar_df[:1913][(calendar_df.event_type_1[:1913].isna())&(calendar_df.event_type_2[:1913].isna())].d]],
axis = 1)
 not_events_days.sample(5 )
train_dataset,valid_dataset,quesdo_dataset,validset,testset = build_datasets(train,valid,quesdo,test)
 train_dataset,train_size = mix_dataset([train_dataset,quesdo_dataset],[train_size,quesdo_size],weight=np.array([1,3]))<choose_model_class>
events_days.groupby(['store_id', 'cat_id'] ).sum()
def get_cls(x):
return x[:, 0, :]
dic = {"mean":GlobalAveragePooling1D() ,
"max":GlobalMaxPool1D() ,
"cls":get_cls}
def build_model(transformer,head="cls" , loss='binary_crossentropy',
max_len=512, drop_rate=None, lr=1e-5,seed=940208):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 sequence_output = transformer(input_word_ids)[0]
x = dic[head](sequence_output)
 if drop_rate is not None:
x = Dropout(drop_rate )(x)
 out = Dense(1, activation='sigmoid',kernel_initializer=GlorotUniform(seed))(x)
 model = Model(inputs=input_word_ids, outputs=out)
 model.compile(Adam(lr=lr), loss=loss, metrics=[AUC() ])
 return model<load_pretrained>
not_events_days.groupby(['store_id', 'cat_id'] ).sum()
%%time
with strategy.scope() :
transformer_layer = TFAutoModel.from_pretrained(MODEL)
 model = build_model(transformer_layer,head=HEAD,loss='binary_crossentropy',
max_len=MAX_LEN,lr=1e-5)
 model.summary()<choose_model_class>
snapCA = pd.concat([not_events_days.loc[not_events_days.state_id=='CA', :'state_id'],
not_events_days.loc[not_events_days.state_id=='CA', calendar_df[:1913][(calendar_df[:1913].snap_CA==1)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1)
 snapTX = pd.concat([not_events_days.loc[not_events_days.state_id=='TX', :'state_id'],
not_events_days.loc[not_events_days.state_id=='TX', calendar_df[:1913][(calendar_df[:1913].snap_TX==1)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1)
 snapWI = pd.concat([not_events_days.loc[not_events_days.state_id=='WI', :'state_id'],
not_events_days.loc[not_events_days.state_id=='WI', calendar_df[:1913][(calendar_df[:1913].snap_WI==1)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1)
 not_snapCA = pd.concat([not_events_days.loc[not_events_days.state_id=='CA', :'state_id'],
not_events_days.loc[not_events_days.state_id=='CA', calendar_df[:1913][(calendar_df[:1913].snap_CA==0)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1)
 not_snapTX = pd.concat([not_events_days.loc[not_events_days.state_id=='TX', :'state_id'],
not_events_days.loc[not_events_days.state_id=='TX', calendar_df[:1913][(calendar_df[:1913].snap_TX==0)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1)
 not_snapWI = pd.concat([not_events_days.loc[not_events_days.state_id=='WI', :'state_id'],
not_events_days.loc[not_events_days.state_id=='WI', calendar_df[:1913][(calendar_df[:1913].snap_WI==0)&(calendar_df.event_type_1[:1913].isna())].d]],
axis = 1 )
model_path = "xlm-roberta.h5"
checkpoint = ModelCheckpoint(model_path, monitor='val_auc', mode="max",
save_best_only=True, save_weights_only=True, verbose=1)
 rp = ReduceLROnPlateau(monitor='val_auc', factor=0.8, patience=3, verbose=1, mode='max')
 callback_list = [checkpoint,rp]<train_model>
prices_df[(prices_df.item_id == 'HOBBIES_1_001')&(prices_df.store_id == 'TX_1')].wm_yr_wk.values
%%time
N_STEPS = train_size //(BATCH_SIZE*4)
 EPOCHS = 8
train_history = model.fit(
 train_dataset,
steps_per_epoch=N_STEPS,
validation_data=validset,
callbacks=callback_list,
epochs=EPOCHS
)<set_options>
sales_df[(sales_df.item_id == 'HOBBIES_1_001')&(sales_df.store_id=='CA_1')].groupby('store_id' ).sum()
del model
gc.collect()
tf.tpu.experimental.initialize_tpu_system(tpu )<load_pretrained>
pd.options.display.max_columns = 50
CAL_DTYPES={"event_name_1": "category", "event_name_2": "category", "event_type_1": "category",
"event_type_2": "category", "weekday": "category", 'wm_yr_wk': 'int16', "wday": "int16",
"month": "int16", "year": "int16", "snap_CA": "float32", 'snap_TX': 'float32', 'snap_WI': 'float32' }
PRICE_DTYPES = {"store_id": "category", "item_id": "category", "wm_yr_wk": "int16","sell_price":"float32" }
FIRST_DAY = 800
h = 28
max_lags = 366
tr_last = 1913
fday = datetime(2016,4, 25)
 fday
%%time
with strategy.scope() :
transformer_layer = TFAutoModel.from_pretrained(MODEL)
 model = build_model(transformer_layer,head=HEAD,loss='binary_crossentropy', max_len=MAX_LEN,lr=5e-6)
 model.load_weights(model_path )<train_model>
def create_dt(is_train = True, nrows = None, first_day = 1200):
prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES)
 for col, col_dtype in PRICE_DTYPES.items() :
if col_dtype == "category":
prices[col] = prices[col].cat.codes.astype("int16")
 prices[col] -= prices[col].min()
cal = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 cal["date"] = pd.to_datetime(cal["date"])
 for col, col_dtype in CAL_DTYPES.items() :
if col_dtype == "category":
cal[col] = cal[col].cat.codes.astype("int16")
 cal[col] -= cal[col].min()
start_day = max(1 if is_train else tr_last-max_lags, first_day)
 numcols = [f"d_{day}" for day in range(start_day,tr_last+1)]
catcols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numcol:"float32" for numcol in numcols}
dtype.update({col: "category" for col in catcols if col != "id"})
 dt = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_validation.csv",
nrows = nrows, usecols = catcols + numcols, dtype = dtype)
 for col in catcols:
if col != "id":
dt[col] = dt[col].cat.codes.astype("int16")
 dt[col] -= dt[col].min()
if not is_train:
for day in range(tr_last+1, tr_last+ 2*h +1):
dt[f"d_{day}"] = np.nan
dt = pd.melt(dt,
id_vars = catcols,
value_vars = [col for col in dt.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 dt = dt.merge(cal, on= "d", copy = False)
 dt = dt.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 return dt
%%time
n_steps = valid_size //(BATCH_SIZE)
 EPOCHS = 1
train_history_2 =model.fit(
 valid_dataset,
steps_per_epoch=n_steps,
epochs= EPOCHS
)<save_to_csv>
def create_fea(dt):
lags = [7, 28]
lag_cols = [f"lag_{lag}" for lag in lags ]
for lag, lag_col in zip(lags, lag_cols):
dt[lag_col] = dt[["id","sales"]].groupby("id")["sales"].shift(lag)
 wins = [7, 28]
for win in wins :
for lag,lag_col in zip(lags, lag_cols):
dt[f"rmean_{lag}_{win}"] = dt[["id", lag_col]].groupby("id")[lag_col].transform(lambda x : x.rolling(win ).mean())
 date_features = {
"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day",
}
for date_feat_name, date_feat_func in date_features.items() :
if date_feat_name in dt.columns:
dt[date_feat_name] = dt[date_feat_name].astype("int16")
 else:
dt[date_feat_name] = getattr(dt["date"].dt, date_feat_func ).astype("int16")
sub = pd.read_csv(PATH + "sample_submission.csv")
 sub['toxic'] = model.predict(testset, verbose=1)
 sub.to_csv('submission.csv', index=False )<load_from_csv>
df = reduce_mem_usage(df )
data = {k.split('/')[-1][:-4]:k for k in glob.glob('/kaggle/input/**/**.csv')}
train = pd.read_csv(data['jigsaw-toxic-comment-train'], usecols=['id', 'comment_text', 'toxic'])
 val = pd.read_csv(data['validation'], usecols=['comment_text', 'toxic'])
 test = pd.read_csv(data['test'], usecols=['id', 'content'])
 test.columns = ['id', 'comment_text']
test['toxic'] = 0.5
sub2 = pd.read_csv('.. /input/ensemble/submission.csv' )<feature_engineering>
df.dropna(inplace = True)
 df.shape
%%time
def f_experience(c, s):
it = {'memory':10,
'influence':0.5,
'inference':0.5,
'interest':0.9,
'sentiment':1e-10,
'harmony':0.5}
exp = {}
for i in range(len(c)) :
words = set([w for w in str(c[i] ).lower().split(' ')])
 for w in words:
try:
exp[w]['influence'] = exp[w]['influence'][1:] + [s[i]]
exp[w]['inference'] += 1
exp[w]['interest'] = exp[w]['interest'][1:] + [(exp[w]['interest'][it['memory']-1] +(s[i] * it['interest'])) /2]
exp[w]['sentiment'] += s[i]
except:
m = [0.for m_ in range(it['memory'])]
exp[w] = {}
exp[w]['influence'] = m[1:] + [s[i]]
exp[w]['inference'] = 1
exp[w]['interest'] = m[1:] + [s[i] * it['interest'] / 2]
exp[w]['sentiment'] = s[i]
for w in exp:
exp[w]['sentiment'] /= exp[w]['inference'] + it['sentiment']
exp[w]['inference'] /= len(c)* it['inference']
return exp
exp = f_experience(train['comment_text'].values, train['toxic'].values )<feature_engineering>
df = reduce_mem_usage(df )
%%time
def features(df):
df['len'] = df['comment_text'].map(len)
 df['wlen'] = df['comment_text'].map(lambda x: len(str(x ).split(' ')))
 df['influence_sum'] = df['comment_text'].map(lambda x: np.sum([np.mean(exp[w]['influence'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['influence_mean'] = df['comment_text'].map(lambda x: np.mean([np.mean(exp[w]['influence'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['inference_sum'] = df['comment_text'].map(lambda x: np.sum([exp[w]['inference'] if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['inference_mean'] = df['comment_text'].map(lambda x: np.mean([exp[w]['inference'] if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['interest_sum'] = df['comment_text'].map(lambda x: np.sum([np.mean(exp[w]['interest'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['interest_mean'] = df['comment_text'].map(lambda x: np.mean([np.mean(exp[w]['interest'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['sentiment_sum'] = df['comment_text'].map(lambda x: np.sum([exp[w]['sentiment'] if w in exp else 0.5 for w in str(x ).lower().split(' ')]))
df['sentiment_mean'] = df['comment_text'].map(lambda x: np.mean([exp[w]['sentiment'] if w in exp else 0.5 for w in str(x ).lower().split(' ')]))
return df
val = features(val)
 test= features(test )<train_on_grid>
cat_feats = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + ["event_name_1", "event_name_2", "event_type_1", "event_type_2"]
useless_cols = ["id", "date", "sales","d", "wm_yr_wk", "weekday"]
train_cols = df.columns[~df.columns.isin(useless_cols)]
X_train = df[train_cols]
y_train = df["sales"]
col = [c for c in val if c not in ['id', 'comment_text', 'toxic']]
x1, x2, y1, y2 = model_selection.train_test_split(val[col], val['toxic'], test_size=0.3, random_state=20)
 model = ensemble.ExtraTreesClassifier(n_estimators=1000, max_depth=10, n_jobs=-1, random_state=20)
 model.fit(x1, y1)
 print(metrics.roc_auc_score(y2, model.predict_proba(x2)[:,1].clip(0.,1.)))
model.fit(val[col], val['toxic'])
 test['toxic'] = model.predict_proba(test[col])[:,1].clip(0.,1.)
sub1 = test[['id', 'toxic']]<save_to_csv>
train_data = lgb.Dataset(X_train, label = y_train, categorical_feature=cat_feats, free_raw_data=False)
 fake_valid_inds = np.random.choice(len(X_train), 1000000)
 fake_valid_data = lgb.Dataset(X_train.iloc[fake_valid_inds], label = y_train.iloc[fake_valid_inds],categorical_feature=cat_feats,
free_raw_data=False )
sub1.rename(columns={'toxic':'toxic1'}, inplace=True)
 sub2.rename(columns={'toxic':'toxic2'}, inplace=True)
 sub3 = pd.merge(sub1, sub2, how='left', on='id')
 sub3['toxic'] =(sub3['toxic1'] * 0.1)+(sub3['toxic2'] * 0.9)
 sub3['toxic'] =(sub3['toxic2'] * 0.51)+(sub3['toxic'] * 0.49)
 sub3[['id', 'toxic']].to_csv('submission.csv', index=False )<prepare_output>
params = {
"objective" : "poisson",
"metric" :"rmse",
"force_row_wise" : True,
"learning_rate" : 0.075,
"sub_row" : 0.75,
"bagging_freq" : 1,
"lambda_l2" : 0.1,
"metric": ["rmse"],
'verbosity': 1,
'num_iterations' : 1500,
}
test = pd.DataFrame(['Howling with Wolf on Lügenpresse'], columns=['comment_text'])
 test['id'] = test.index
test= features(test)
 test['toxic'] = model.predict_proba(test[col])[:,1].clip(0.,1.)
test[['id', 'comment_text', 'toxic']].head()<import_modules>
%%time
m_lgb = lgb.train(params, train_data, valid_sets = [fake_valid_data], verbose_eval=50 )
from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPool2D
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt<define_variables>
df = reduce_mem_usage(df )
np.random.seed(42 )<save_to_csv>
%%time
for i in range(0, 28):
day = fday + timedelta(days=i)
 print(i, day)
 tst = te[(te.date >= day - timedelta(days=max_lags)) &(te.date <= day)].copy()
create_fea(tst)
 tst = tst.loc[tst.date == day , train_cols]
te.loc[te.date == day, "sales"] = 1.02*m_lgb.predict(tst )
<load_from_csv><EOS> 
%%time
te_sub = te.loc[te.date >= fday, ["id", "sales"]].copy()
te_sub.loc[te.date >= fday+ timedelta(days=h), "id"] = te_sub.loc[te.date >= fday+timedelta(days=h),
"id"].str.replace("validation$", "evaluation")
 te_sub["F"] = [f"F{rank}" for rank in te_sub.groupby("id")["id"].cumcount() +1]
te_sub = te_sub.set_index(["id", "F" ] ).unstack() ["sales"][[f"F{i}" for i in range(1,29)]].reset_index()
te_sub.fillna(0., inplace = True)
 te_sub.to_csv("submission.csv",index=False)
 te_sub.shape