Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<train_model>
cal = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 steval = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 price = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv' )
history=model.fit_generator(generator=train_batches, steps_per_epoch = steps_per_epoch, epochs=epochs,
validation_data=val_batches, validation_steps=validation_steps , callbacks=callbacks )<save_to_csv>
id_list = sorted(list(set(steval['id'])))
 d_cols = [col for col in steval.columns if 'd_' in col]
x_1 = steval.loc[steval['id'] == id_list[0]].set_index('id')[d_cols].values[0][:200]
x_2 = steval.loc[steval['id'] == id_list[12]].set_index('id')[d_cols].values[0][300:500]
x_3 = steval.loc[steval['id'] == id_list[36]].set_index('id')[d_cols].values[0][600:800]
x_4 = steval.loc[steval['id'] == id_list[64]].set_index('id')[d_cols].values[0][1000:1200]
x_5 = steval.loc[steval['id'] == id_list[128]].set_index('id')[d_cols].values[0][1300:1500]
x_6 = steval.loc[steval['id'] == id_list[256]].set_index('id')[d_cols].values[0][1600:1800]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines', name="1st sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines', name="2nd sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines', name="3rd sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines', name="4th sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines', name="5th sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines', name="6th sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1200, width=900, title_text="Observing some randomly chosen sales trend :")
 fig.show()
predictions = model.predict_classes(X_test_f, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("mysub5.csv", index=False, header=True )<import_modules>
for i in range(1942,1970):
col = 'd_' + str(i)
 steval[col] = 0
steval[col] = steval[col].astype(np.int16 )
activations, regularizers, Sequential
, utils, callbacks, optimizers 
)
 Flatten, Dense, BatchNormalization
, Activation, Dropout, Conv2D, MaxPool2D
)<load_from_csv>
sales = pd.melt(steval, id_vars=['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'], var_name='d', value_name='sold' ).dropna()
BASEPATH = '.. /input/digit-recognizer/'
def reload(df='train.csv', msg=True, path=BASEPATH):
o = pd.read_csv(BASEPATH + df)
 print(f'{df} loaded!')
 return o
train = reload()
y_train = train.label.copy()
y_train = utils.to_categorical(y_train, 10)
 X_train = train.drop(columns='label')
 def preprocess(df):
df = df / 256.0
df = df.values.reshape(-1, 28, 28, 1)
 return df
X_train = preprocess(X_train)
 test = reload('test.csv')
 X_test = preprocess(test )<init_hyperparams>
sales = pd.merge(sales, cal, on='d', how='left')
 sales = pd.merge(sales, price, on=['store_id','item_id','wm_yr_wk'], how='left' )
l1 = 0
l2 = 0.01
ini_lr = 0.001
val_size = 0.3
batch_size = 30
activation = 'relu'
if activation == 'selu':
initializer = 'lecun_normal'
elif activation in ['relu', 'elu']:
initializer = 'he_normal'
else:
initializer = 'glorot_normal'
sched_lr_val_acc = True
decay_rate = 0.97
X_tr, X_val, y_tr, y_val = train_test_split(
 X_train, y_train, test_size=val_size, random_state=713)
 n_train_val_samples = X_tr.shape[0]
n_train_samples = X_tr.shape[0] + X_val.shape[0]
def generate_tfdata(X, y=None, batch_size=batch_size
, shuffle_repeat=True):

 if y is None:
data = Dataset.from_tensor_slices(( X,))
else:
data = Dataset.from_tensor_slices(( X, y))
if shuffle_repeat:
data = data.shuffle(10000, 713, reshuffle_each_iteration=True ).repeat()
if batch_size:
data = data.batch(batch_size)
 return data.prefetch(1)
 tr_set = generate_tfdata(X_tr, y_tr)
 val_set = generate_tfdata(X_val, y_val, shuffle_repeat=False)
 train_set = generate_tfdata(X_train, y_train)
 test_set = generate_tfdata(X_test, shuffle_repeat=False )<choose_model_class>
d_id = dict(zip(sales.id.cat.codes, sales.id))
d_item_id = dict(zip(sales.item_id.cat.codes, sales.item_id))
d_dept_id = dict(zip(sales.dept_id.cat.codes, sales.dept_id))
d_cat_id = dict(zip(sales.cat_id.cat.codes, sales.cat_id))
d_store_id = dict(zip(sales.store_id.cat.codes, sales.store_id))
d_state_id = dict(zip(sales.state_id.cat.codes, sales.state_id))
def get_regularizer(l1=l1, l2=l2):
return regularizers.l1_l2(l1=l1, l2=l2)
 def get_optimizer(lr=ini_lr, beta_1=0.9, beta_2=0.999
, decay_rate=decay_rate
, n_samples=n_train_samples, batch_size=batch_size):
if sched_lr_val_acc == False:
lr = optimizers.schedules.ExponentialDecay(
 lr
, decay_steps = n_samples // batch_size // 2
, decay_rate = decay_rate 
)
 return optimizers.Adam(lr, beta_1=beta_1, beta_2=beta_2)
 def get_callbacks(es_params=['val_loss', 1e-3, 15]
, lr_params=['val_accuracy', 0.001, 5]):
cb = []
cb.append(callbacks.EarlyStopping(
 monitor=es_params[0], min_delta=es_params[1], patience=es_params[2]
, verbose=1, restore_best_weights=True 
))
if sched_lr_val_acc:
cb.append(callbacks.ReduceLROnPlateau(
 monitor=lr_params[0], factor=0.5, patience=lr_params[2]
, min_delta=lr_params[1], min_lr=ini_lr*0.1**5 
))
cb.append(callbacks.TerminateOnNaN())
 return cb
DefaultConv2D = partial(Conv2D
, kernel_size=3
, strides=1
, activation=activation
, kernel_initializer=initializer
, padding='SAME')
 DefaultMaxPool2D = partial(MaxPool2D
, pool_size=2
, strides=2)
 DefaultDense = partial(Dense
, activation=activation
, kernel_initializer=initializer
, kernel_regularizer=get_regularizer())
 def get_model(compiling=True, n_samples=n_train_samples, batch_size=batch_size
, activation=activation, initializer=initializer, p=0.5):
model = Sequential(
 [DefaultConv2D(filters=32, kernel_size=5, input_shape=(28,28,1))
, BatchNormalization()
, DefaultMaxPool2D()
, DefaultConv2D(filters=64)
 , BatchNormalization()
, DefaultConv2D(filters=64)
 , BatchNormalization()
, DefaultMaxPool2D()
, DefaultConv2D(filters=128)
 , BatchNormalization()
, DefaultConv2D(filters=128)
 , BatchNormalization()
, DefaultMaxPool2D()
, Flatten()
, DefaultDense(units=64)
 , Dropout(p)
 , DefaultDense(units=32)
 , Dropout(p)
 , Dense(units=10, activation='softmax')
 ] 
)
 optimizer = get_optimizer(n_samples=n_samples, batch_size=batch_size)
 if compiling:
opt = get_optimizer()
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
 return model<train_model>
sales.d = sales['d'].apply(lambda x: x.split('_')[1] ).astype(np.int16)
 cols = sales.dtypes.index.tolist()
types = sales.dtypes.values.tolist()
for i,type in enumerate(types):
if type.name == 'category':
sales[cols[i]] = sales[cols[i]].cat.codes
verbose = 1
model = get_model(n_samples=n_train_val_samples)
 print(model.summary())
 epochs = 100
cb = get_callbacks()
history = model.fit(tr_set, epochs = epochs
, steps_per_epoch = n_train_val_samples // batch_size
, validation_data = val_set
, callbacks = cb
, verbose = verbose )<save_to_csv>
sales.drop('date',axis=1,inplace=True )
y_pred = model.predict(test_set)
 y_pred = np.argmax(y_pred, axis=1)
 submission = reload('sample_submission.csv')
 submission['Label'] = y_pred
submission.to_csv('submission.csv', index=False )<load_from_csv>
lags = [1,2,4,8,16,32]
for lag in lags:
sales['sold_lag_'+str(lag)] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'],as_index=False)['sold'].shift(lag ).astype(np.float16 )
train, test = pd.read_csv('.. /input/digit-recognizer/train.csv'), \
pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head()<prepare_x_and_y>
sales['item_sold_avg'] = sales.groupby('item_id')['sold'].transform('mean' ).astype(np.float16)
 sales['state_sold_avg'] = sales.groupby('state_id')['sold'].transform('mean' ).astype(np.float16)
 sales['store_sold_avg'] = sales.groupby('store_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_sold_avg'] = sales.groupby('cat_id')['sold'].transform('mean' ).astype(np.float16)
 sales['dept_sold_avg'] = sales.groupby('dept_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_dept_sold_avg'] = sales.groupby(['cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_item_sold_avg'] = sales.groupby(['store_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['cat_item_sold_avg'] = sales.groupby(['cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_item_sold_avg'] = sales.groupby(['dept_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_store_sold_avg'] = sales.groupby(['dept_id','store_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_dept_sold_avg'] = sales.groupby(['store_id','cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_item_sold_avg'] = sales.groupby(['store_id','cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
y_train = train['label']
x_train, x_test = train.iloc[:,1:], test<categorify>
id_list = sorted(list(set(sales['id'])))
 sold_avg_cols = [col for col in sales.columns if '_sold_avg' in col]
x_1 = sales.loc[sales['id'] == id_list[0]].set_index('id')[sold_avg_cols].values[0][:]
x_2 = sales.loc[sales['id'] == id_list[12]].set_index('id')[sold_avg_cols].values[0][:]
x_3 = sales.loc[sales['id'] == id_list[36]].set_index('id')[sold_avg_cols].values[0][:]
x_4 = sales.loc[sales['id'] == id_list[64]].set_index('id')[sold_avg_cols].values[0][:]
x_5 = sales.loc[sales['id'] == id_list[128]].set_index('id')[sold_avg_cols].values[0][:]
x_6 = sales.loc[sales['id'] == id_list[256]].set_index('id')[sold_avg_cols].values[0][:]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines+markers', name="First sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines+markers', name="Second sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines+markers', name="Third sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines+markers', name="Fourth sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines+markers', name="Fifth sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines+markers', name="Sixth sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1500, width=900, title_text="Observing some randomly chosen '_sold_avg' trends after applying lag:")
 fig.show()
x_train, x_test = x_train / 255., x_test / 255.
 x_train, x_test = x_train.values.reshape(-1,28,28,1),\
x_test.values.reshape(-1,28,28,1 )<categorify>
sales['rolling_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.rolling(window=6 ).mean() ).astype(np.float16 )
y_train = to_categorical(y_train, num_classes=10 )<choose_model_class>
sales['expanding_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.expanding(2 ).mean() ).astype(np.float16 )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=32, kernel_size=(5,5), \
activation='relu', input_shape=(28,28,1)) ,
tf.keras.layers.MaxPooling2D(pool_size=(2,2)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), \
activation='relu', input_shape=(28,28,1)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), \
activation='relu', input_shape=(28,28,1)) ,
tf.keras.layers.Dropout (.4),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)) ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout (.4),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()<train_model>
gc.collect()
num_epochs = 50
history = model.fit(x_train, y_train,
epochs=num_epochs,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor='loss', patience=6),
tf.keras.callbacks.ReduceLROnPlateau(monitor='loss', patience=4)
 ],
validation_split=0.2,
verbose=0 )<compute_test_metric>
sales['daily_avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id','d'])['sold'].transform('mean' ).astype(np.float16)
 sales['avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['selling_trend'] =(sales['daily_avg_sold'] - sales['avg_sold'] ).astype(np.float16)
 sales.drop(['daily_avg_sold','avg_sold'],axis=1,inplace=True )
scores = model.evaluate(x_train, y_train, batch_size=32)
 print(f'Loss: {scores[0]} Accuracy: {scores[1]}' )<predict_on_test>
sales = sales[sales['d']>=32]
y_predicted = model.predict(x_test)
 y_test_labels = np.argmax(y_predicted, axis=1 )<define_variables>
gc.collect()
x_test_ids = [(i + 1)for i in range(x_test.shape[0])]<save_to_csv>
sales.to_pickle('salesdata.pkl')
 del sales
results = pd.DataFrame({
'ImageId': x_test_ids,
'Label': y_test_labels
})
 results.to_csv('submission.csv', index=False )<install_modules>
gc.collect()
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git<set_options>
data = pd.read_pickle('salesdata.pkl')
 validation = data[(data['d']>=1914)&(data['d']<1942)][['id','d','sold']]
test = data[data['d']>=1942][['id','d','sold']]
eval_prediction = test['sold']
validation_prediction = validation['sold']
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 0
seed_everything(seed)
 warnings.filterwarnings('ignore' )<init_hyperparams>
gc.collect()
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<define_variables>
gc.collect()
BATCH_SIZE = 16 * REPLICAS
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
TTA_STEPS = 8<define_search_space>
X_train, y_train = df[df['d']<1914].drop('sold',axis=1), df[df['d']<1914]['sold']
X_valid, y_valid = df[(df['d']>=1914)&(df['d']<1942)].drop('sold',axis=1), df[(df['d']>=1914)&(df['d']<1942)]['sold']
X_test = df[df['d']>=1942].drop('sold',axis=1 )
def data_augment(image, label):
p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if p_spatial >.75:
image = tf.image.transpose(image)
 if p_rotate >.75:
image = tf.image.rot90(image, k=3)
 elif p_rotate >.5:
image = tf.image.rot90(image, k=2)
 elif p_rotate >.25:
image = tf.image.rot90(image, k=1)
 if p_pixel_1 >=.4:
image = tf.image.random_saturation(image, lower=.7, upper=1.3)
 if p_pixel_2 >=.4:
image = tf.image.random_contrast(image, lower=.8, upper=1.2)
 if p_pixel_3 >=.4:
image = tf.image.random_brightness(image, max_delta=.1)
 if p_crop >.7:
if p_crop >.9:
image = tf.image.central_crop(image, central_fraction=.7)
 elif p_crop >.8:
image = tf.image.central_crop(image, central_fraction=.8)
 else:
image = tf.image.central_crop(image, central_fraction=.9)
 elif p_crop >.4:
crop_size = tf.random.uniform([], int(HEIGHT*.8), HEIGHT, dtype=tf.int32)
 image = tf.image.random_crop(image, size=[crop_size, crop_size, CHANNELS])
 return image, label<categorify>
gc.collect()
def get_name(file_path):

 parts = tf.strings.split(file_path, os.path.sep)
 name = parts[-1]
return name
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
return image
def center_crop(image):
image = tf.reshape(image, [600, 800, CHANNELS])
 h, w = image.shape[0], image.shape[1]
if h > w:
image = tf.image.crop_to_bounding_box(image,(h - w)// 2, 0, w, w)
 else:
image = tf.image.crop_to_bounding_box(image, 0,(w - h)// 2, h, h)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 return image
def resize_image(image, label):

 image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
 return image, label
def process_path(file_path):

 name = get_name(file_path)
 img = tf.io.read_file(file_path)
 img = decode_image(img)
 return img, name
def get_dataset(files_path, shuffled=False, tta=False, extension='jpg'):

 dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)
 dataset = dataset.map(process_path, num_parallel_calls=AUTO)
 if tta:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.map(resize_image, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )<train_model>
valgrid = {'n_estimators':hp.quniform('n_estimators', 900, 1200, 100),
'learning_rate':hp.quniform('learning_rate', 0.1, 0.4, 0.1),
'max_depth':hp.quniform('max_depth', 4,8,1),
'num_leaves':hp.quniform('num_leaves', 25,75,25),
'subsample':hp.quniform('subsample', 0.5, 0.9, 0.1),
'colsample_bytree':hp.quniform('colsample_bytree', 0.5, 0.9, 0.1),
'min_child_weight':hp.quniform('min_child_weight', 200, 500, 100)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'num_leaves': int(params['num_leaves']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'min_child_weight': params['min_child_weight']}
lgb_a = lgb(**params)
 score = cross_val_score(lgb_a, X_train, y_train, cv=2, n_jobs=-1 ).mean()
return score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(123), algo=tpe.suggest )
model_path_list = glob.glob('/kaggle/input/cassava-leaf-disease-training-with-tpu-v2-pods/*.h5')
 model_path_list.sort()
print('Models to predict:')
 print(*model_path_list, sep='
' )<choose_model_class>
gc.collect()
def model_fn(input_shape, N_CLASSES):
inputs = L.Input(shape=input_shape, name='input_image')
 base_model = efn.EfficientNetB4(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 x = L.Dropout (.5 )(base_model.output)
 output = L.Dense(N_CLASSES, activation='softmax', name='output' )(x)
 model = Model(inputs=inputs, outputs=output)
 return model
with strategy.scope() :
model = model_fn(( None, None, CHANNELS), N_CLASSES)
 model.summary()<predict_on_test>
model = lightgbm.LGBMRegressor(
 n_estimators = int(bestP['n_estimators']),
learning_rate = bestP['learning_rate'],
subsample = bestP['subsample'],
colsample_bytree = bestP['colsample_bytree'],
max_depth = int(bestP['max_depth']),
num_leaves = int(bestP['num_leaves']),
min_child_weight = int(bestP['min_child_weight']))
print('Prediction for Store: {}**'.format(d_store_id[store]))
model.fit(X_train, y_train, eval_set=[(X_train,y_train),(X_valid,y_valid)], eval_metric='rmse', verbose=20, early_stopping_rounds=20)
 validation_prediction[X_valid.index] = model.predict(X_valid)
 eval_prediction[X_test.index] = model.predict(X_test)
 filename = 'model'+str(d_store_id[store])+'.pkl'
files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, N_CLASSES))
for model_path in model_path_list:
print(model_path)
 K.clear_session()
model.load_weights(model_path)
 if TTA_STEPS > 0:
test_ds = get_dataset(files_path, tta=True ).repeat()
ct_steps = TTA_STEPS *(( test_size/BATCH_SIZE)+ 1)
 preds = model.predict(test_ds, steps=ct_steps, verbose=1)[:(test_size * TTA_STEPS)]
preds = np.mean(preds.reshape(test_size, TTA_STEPS, N_CLASSES, order='F'), axis=1)
 test_preds += preds / len(model_path_list)
 else:
test_ds = get_dataset(files_path, tta=False)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)/ len(model_path_list)
 test_preds = np.argmax(test_preds, axis=-1)
 test_names_ds = get_dataset(files_path)
 image_names = [img_name.numpy().decode('utf-8')for img, img_name in iter(test_names_ds.unbatch())]<save_to_csv>
joblib.dump(model, filename)
 del model, X_train, y_train, X_valid, y_valid
gc.collect()
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )<import_modules>
validation = steval[['id']+['d_' + str(i)for i in range(1914,1942)]]
validation['id']=pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv' ).id
validation.columns=['id'] + ['F' + str(i + 1)for i in range(28)]
test['sold'] = eval_prediction
evaluation = test[['id','d','sold']]
evaluation = pd.pivot(evaluation, index='id', columns='d', values='sold' ).reset_index()
evaluation.columns=['id'] + ['F' + str(i + 1)for i in range(28)]
evaluation.id = evaluation.id.map(d_id )
import numpy as np
import os
import pandas as pd
from fastai.vision.all import *<define_variables>
gc.collect()
set_seed(42 )<load_from_csv>
submit = pd.concat([validation,evaluation] ).reset_index(drop=True)
 submit.head()
<drop_column><EOS> 
submit.to_csv('submission.csv',index=False)
 print("Submission successful" )