Kaggle competition: M5 Forecasting - Accuracy
x=x.reshape(-1,28,28,1)
 test=test.values.reshape(-1,28,28,1)
 x=x/255
test=test/255
y=to_categorical(y )<init_hyperparams>
df_cal = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 df_eval = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 df_price = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
 df_sample_output = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sample_submission.csv' )
train_datagen=ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
fill_mode='nearest'
)<choose_model_class>
holiday = ['NewYear', 'OrthodoxChristmas', 'MartinLutherKingDay', 'SuperBowl', 'PresidentsDay', 'StPatricksDay', 'Easter', 'Cinco De Mayo', 'IndependenceDay', 'EidAlAdha', 'Thanksgiving', 'Christmas']
weekend = ['Saturday', 'Sunday']
df_cal['is_holiday_1'] = df_cal['event_name_1'].apply(lambda x : 1 if x in holiday else 0)
 df_cal['is_holiday_2'] = df_cal['event_name_1'].apply(lambda x : 1 if x in holiday else 0)
 df_cal['is_holiday'] = df_cal[['is_holiday_1','is_holiday_2']].max(axis=1)
 df_cal['is_weekend'] = df_cal['weekday'].apply(lambda x : 1 if x in weekend else 0 )
ensem=10
model=[0]*ensem
for i in range(ensem):
model[i]=Sequential()
model[i].add(Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu',input_shape=(28,28,1)))
 model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(2,2))
model[i].add(Dropout(0.2))
model[i].add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(2,2))
model[i].add(Dropout(0.2))
model[i].add(GlobalAveragePooling2D())
 model[i].add(Dense(128,activation='relu'))
model[i].add(Dropout(0.2))
model[i].add(Dense(10,activation='softmax'))
model[i].compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam' )<train_model>
df_cal = df_cal.drop(['weekday', 'wday', 'month', 'year', 'event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'], axis='columns' )
callback=tf.keras.callbacks.EarlyStopping(monitor='accuracy',min_delta=0,patience=5,mode='auto',restore_best_weights=True,verbose=0)
 lrs=tf.keras.callbacks.LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose=0)
 history=[0]*ensem
for i in range(ensem):
train_x,valid_x,train_y,valid_y=train_test_split(x,y,test_size=0.2)
 history[i]=model[i].fit_generator(train_datagen.flow(train_x,train_y,batch_size=128),
epochs=100,
steps_per_epoch=train_x.shape[0]//128,
verbose=0,
validation_data=(valid_x,valid_y),
validation_steps=valid_x.shape[0]//128,
callbacks=[callback,lrs])
 print('Model {}: Epochs=100, Train_Accuracy:{}, Val_Accuracy:{}'.format(i+1,max(history[i].history['accuracy']),max(history[i].history['val_accuracy'])) )<save_to_csv>
del_col = []
for x in range(1851):
del_col.append('d_' + str(x+1))
prediction=np.zeros(( test.shape[0],10))
for i in range(ensem):
prediction=prediction+model[i].predict(test)
 prediction=np.argmax(prediction,axis = 1)
 prediction=pd.Series(prediction,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name="ImageId"),prediction],axis=1)
 submission.to_csv("digit_recognizer.csv",index=False )<import_modules>
df_eval = df_eval.drop(del_col, axis='columns' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import tensorflow as tf
from kerastuner import HyperModel
from kerastuner.tuners import RandomSearch
from tensorflow.python.keras.callbacks import ModelCheckpoint
import seaborn as sns
from pylab import rcParams
from sklearn.metrics import confusion_matrix<set_options>
df_eval = pd.merge(df_eval, df_cal, how='left', on='d' )
rcParams['figure.figsize'] = 15, 15
NUM_CLASSES = 10
INPUT_SHAPE =(28, 28, 1 )<load_from_csv>
df_eval = pd.merge(df_eval, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'])
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 X = train.iloc[:, 1:]
y = train.iloc[:, 0]<normalization>
df_eval_test = df_eval.query('d == "d_1852"' )
X /= 256
test /= 256
X = X.values.reshape(( -1,)+ INPUT_SHAPE)
 test = test.values.reshape(( -1,)+ INPUT_SHAPE )<choose_model_class>
df_eval_test = df_eval_test[['id', 'store_id', 'item_id', 'dept_id', 'cat_id', 'state_id', 'd', 'qty', 'sell_price']]
class CNNHyperModel(HyperModel):
def __init__(self, input_shape, num_classes):
self.input_shape = input_shape
self.num_classes = num_classes
def build(self, hp):
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(8, 3, padding='same', dilation_rate=(3, 3), \
activation='relu', input_shape=self.input_shape))
model.add(tf.keras.layers.Conv2D(16, 3, padding='same', \
activation='relu'))
max_or_avg =hp.Int(
 'max_or_avg',
min_value=1,
max_value=2,
step=1,
default=1)
 three = hp.Fixed('three', 3)
 model.add(tf.keras.layers.AvgPool2D(pool_size=max_or_avg, name=f'first_avg_pool_{max_or_avg}'))
model.add(tf.keras.layers.MaxPool2D(pool_size=three - max_or_avg))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Conv2D(32, 3, padding='same', dilation_rate=(3, 3), \
activation='relu'))
model.add(tf.keras.layers.Conv2D(64, 3, padding='same', \
activation='relu'))
model.add(tf.keras.layers.AvgPool2D(pool_size=max_or_avg, name=f'second_avg_pool_{max_or_avg}'))
model.add(tf.keras.layers.MaxPool2D(pool_size=three - max_or_avg))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Flatten())
 drop_rate = hp.Float('dropout',
min_value=0.3,
max_value=0.7,
default=0.3,
step=0.2, 
)
 model.add(tf.keras.layers.Dropout(rate=drop_rate,
name=f'drop{drop_rate}'))
units=hp.Int('units',
min_value=28,
max_value=4*28,
step=28,
default=28 
)
 model.add(tf.keras.layers.Dense(
 units=units,
name=f'Dense{units}',
activation='relu' 
))
model.add(tf.keras.layers.Dense(self.num_classes))
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
 optimizer = tf.keras.optimizers.Adam()
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
 return model<choose_model_class>
df_eval_test['qty'] = df_eval_test['d'].apply(lambda x: int(x.replace(x, '0')) )
hypermodel = CNNHyperModel(input_shape=INPUT_SHAPE, num_classes=NUM_CLASSES)
 tuner = RandomSearch(
 hypermodel,
objective='val_accuracy',
max_trials=24,
executions_per_trial=1,
directory='./random_search',
project_name='MNIST' 
)
 tuner.search_space_summary()<split>
tmp_df = df_eval_test
X_train_val, X_test, y_train_val, y_test = train_test_split(X, y, test_size=1/7 )<compute_test_metric>
for x in range(28):
df_eval_test = df_eval_test.append(tmp_df )
search_results = tuner.search(X_train_val, y_train_val,
epochs=10, validation_split=1/6,
batch_size=100 )<train_model>
df_eval_test = df_eval_test.reset_index(drop=True )
best_model = tuner.get_best_models(num_models=1)[0]
best_model.summary()
print(best_model.evaluate(X_test, y_test))<choose_model_class>
lst_d = []
i = 0
lst_index = df_eval_test.index
for x in lst_index:
lst_d.append('d_' + str(((lst_index[i])// 30490)+ 1942))
i = i + 1
def schedule(epoch, lr):
return 10**(-(epoch//10)-3)
 lr_schedule = tf.keras.callbacks.LearningRateScheduler(schedule, verbose=1)
 cb_checkpointer_val = ModelCheckpoint(filepath = '.. /working/best_val.hdf5',
monitor = 'val_accuracy',
save_best_only = True,
mode = 'auto' )<split>
df_eval_test['d'] = lst_d
X_train, X_val, y_train, y_val = train_test_split(X_train_val,
y_train_val,
test_size=1/6 )<train_model>
df_eval_test = pd.merge(df_eval_test, df_cal, how='left', on='d' )
fit_history = best_model.fit(X_train, y_train, epochs=40, batch_size=100,
validation_data=(X_val, y_val),
callbacks = [lr_schedule, cb_checkpointer_val] )<train_model>
df_eval_test = pd.merge(df_eval_test, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'] )
best_model.load_weights('./best_val.hdf5')
 best_model.evaluate(X_test, y_test )<choose_model_class>
del tmp_df
gc.collect()
cb_checkpointer = ModelCheckpoint(filepath = '.. /working/best.hdf5',
monitor = 'accuracy',
save_best_only = True,
mode = 'auto' )<train_model>
df_eval = pd.get_dummies(data=df_eval, columns=['dept_id', 'cat_id', 'store_id', 'state_id'])
 df_eval_test = pd.get_dummies(data=df_eval_test, columns=['dept_id', 'cat_id', 'store_id', 'state_id'] )
final_fit_history = best_model.fit(X, y, epochs=40, batch_size=100,
callbacks = [lr_schedule, cb_checkpointer] )<load_pretrained>
df_eval_test = df_eval_test.drop(['sell_price_x', 'snap_CA', 'snap_TX', 'snap_WI'], axis='columns')
 df_eval_test = df_eval_test.rename(columns={'sell_price_y': 'sell_price'})
 df_eval = df_eval.drop(['snap_CA', 'snap_TX', 'snap_WI'], axis='columns' )
best_model.load_weights('./best.hdf5' )<compute_test_metric>
target_col = 'qty'
exclude_cols = ['id', 'item_id', 'd', 'date', 'wm_yr_wk']
feature_cols = [col for col in df_eval.columns if col not in exclude_cols]
y = np.array(df_eval[target_col])
 X = np.array(df_eval[feature_cols])
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42 )
best_model.evaluate(X, y )<save_to_csv>
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test)

params = {
'n_jobs': -1,
'boosting_type': 'gbdt',
"objective" : "regression",
"metric" : "rmse",
"num_leaves" : 64,
"learning_rate" : 0.005,
"bagging_fraction" : 0.7,
"feature_fraction" : 0.5,
"bagging_frequency" : 6,
"bagging_seed" : 42,
"verbosity" : 1,
"seed": 42,
}
evals_result = {}
model = lgb.train(params, lgb_train, 5000,
valid_sets=[lgb_eval],
early_stopping_rounds=1500,
verbose_eval=50,
evals_result=evals_result )
pred = pd.DataFrame({'ImageId' : np.arange(test.shape[0])+ 1,
'Label': best_model.predict(test ).argmax(axis=-1)})
 pred.to_csv('pred.csv', index=False)
 pred<install_modules>
pred = model.predict(df_eval_test[feature_cols] )
pip install livelossplot<set_options>
df_eval_test['pred_qty'] = pred
%matplotlib inline 
<load_from_csv>
predictions = df_eval_test[['id', 'date', 'pred_qty']]
predictions = pd.pivot(predictions, index = 'id', columns = 'date', values = 'pred_qty' ).reset_index()
predictions
train_set = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_set = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 img_col = 28
img_row = 28<prepare_x_and_y>
predictions = predictions.drop(predictions.columns[1], axis=1 )
X_train_df = train_set.drop(['label'],axis = 1)
 y_train_df = train_set['label']
X_test_df = test_set
X_tr = np.asarray(X_train_df)/255
y_tr = np.asarray(y_train_df)
 X_te = np.asarray(X_test_df)/255
print(type(X_tr))
print(X_tr.shape)
 print(X_te.shape)
<count_values>
predictions.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]
train_set['label'].value_counts().sort_index()<prepare_x_and_y>
x = 2744099 + 1 - 853720
df_val = df_eval[x:]
X_train_f = X_tr.reshape(42000,img_col,img_row,1)
 X_test_f= X_te.reshape(28000,img_col,img_row,1)
 y_train_f = to_categorical(y_tr)
 y_train_f.shape[1]<choose_model_class>
predictions_v = df_val[['id', 'date', 'qty']]
predictions_v = pd.pivot(predictions_v, index = 'id', columns = 'date', values = 'qty' ).reset_index()
model = Sequential()
model.add(Conv2D(64,(4,4),padding = 'valid', input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(2,2),padding = 'same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(2,2),padding = 'same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.1))
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.1))
model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(64))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(64))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(10 , activation = 'softmax'))<choose_model_class>
predictions_v['id'] = predictions['id'].apply(lambda x: x.replace('evaluation', 'validation'))
init_lr = 1e-2
decay_steps = 150
alpha = 1e-5
beta = 1e-8
num_periods=4
lin_cos_dec1 = tf.keras.experimental.LinearCosineDecay(init_lr,
decay_steps,
num_periods=num_periods, alpha=alpha,
beta=beta, name='LinCosDec 1' )<choose_model_class>
predictions_v.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]
opt = Adam(lr=0.00005)
 model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
 model.summary()<split>
predictions_concat = pd.concat([predictions, predictions_v], axis=0 )
<choose_model_class><EOS> 
predictions_concat.to_csv('submission.csv', index=False )