Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<feature_engineering>
train_df['speed'] = train_df['distance']/train_df['trip_duration']*3.6<load_from_csv>
pd.set_option('display.max_colwidth', None)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 4000 )
train_new_1 = pd.read_csv(".. /input/new-york-city-taxi-with-osrm/fastest_routes_train_part_1.csv")
 train_new_2 = pd.read_csv(".. /input/new-york-city-taxi-with-osrm/fastest_routes_train_part_2.csv")
 train_test = pd.read_csv(".. /input/new-york-city-taxi-with-osrm/fastest_routes_test.csv")
 train_new = pd.concat([train_new_1, train_new_2], axis=0)
<merge>
seed = 2020
random.seed(seed)
 np.random.seed(seed )
train_all = train_df.merge(train_new, on='id', how='inner' )<data_type_conversions>
sns.set()
DEFAULT_FIG_WIDTH = 20
sns.set_context("paper", font_scale=1.2 )
train_d = pd.to_datetime(train_all['pickup_datetime'])
 train_all['month'] = train_d.dt.month.astype('category' ).cat.codes
train_all['hour'] = train_d.dt.hour.astype('category' ).cat.codes
train_all['wday'] = train_d.dt.weekday.astype('category' ).cat.codes<prepare_x_and_y>
print('Python : ' + sys.version.split('
')[0])
 print('Numpy : ' + np.__version__)
 print('Pandas : ' + pd.__version__)
SELECTED_COLUMNS = ['vendor_id', 'passenger_count', 'distance', 'pickup_latitude','pickup_longitude','dropoff_latitude', 'dropoff_longitude','hour','month','wday','total_distance','total_travel_time']
X = train_all[SELECTED_COLUMNS]
X.head(15 )<prepare_x_and_y>
print('Prophet : ' + fbprophet.__version__ )
y = np.log1p(train_all['trip_duration'] )<choose_model_class>
HOME = "./"
DATA_HOME = "/kaggle/input/m5-forecasting-accuracy/"
TRAIN_DATA_HOME = DATA_HOME
CALENDAR = DATA_HOME + "calendar.csv"
SALES = DATA_HOME + "sales_train_validation.csv"
PRICES = DATA_HOME + "sell_prices.csv"
MODELS_DIR = "models"
if not os.path.exists(MODELS_DIR):
os.makedirs(MODELS_DIR)
 NUM_SERIES = 30490
NUM_TRAINING = 1913
NUM_TEST = NUM_TRAINING + 2 * 28
MAX_LEVEL = 9
rf = RandomForestRegressor(n_estimators = 100, random_state = 42 )<train_model>
series_ids = np.empty(NUM_SERIES, dtype=object)
 item_ids = np.empty(NUM_SERIES, dtype=object)
 dept_ids = np.empty(NUM_SERIES, dtype=object)
 cat_ids = np.empty(NUM_SERIES, dtype=object)
 store_ids = np.empty(NUM_SERIES, dtype=object)
 state_ids = np.empty(NUM_SERIES, dtype=object)
 qties = np.zeros(( NUM_TRAINING, NUM_SERIES), dtype=float)
 sell_prices = np.zeros(( NUM_TEST, NUM_SERIES), dtype=float )
rf.fit(X, y);<load_from_csv>
id_idx = {}
with open(SALES, "r", newline='')as f:
is_header = True
i = 0
for row in csv.reader(f):
if is_header:
is_header = False
continue
series_id, item_id, dept_id, cat_id, store_id, state_id = row[0:6]
series_id = f"{item_id}_{store_id}"
qty = np.array(row[6:], dtype=float)
 series_ids[i] = series_id
item_ids[i] = item_id
dept_ids[i] = dept_id
cat_ids[i] = cat_id
store_ids[i] = store_id
state_ids[i] = state_id
qties[:, i] = qty
id_idx[series_id] = i
i += 1
test_df = pd.read_csv('.. /input/nyc-taxi-trip-duration/test.csv')
 test_all = test_df.merge(train_test, on='id', how='inner')
 test_df.dtypes
test_df.head(10 )<data_type_conversions>
wm_yr_wk_idx = defaultdict(list)
 with open(CALENDAR, "r", newline='')as f:
for row in csv.DictReader(f):
d = int(row['d'][2:])
 wm_yr_wk_idx[row['wm_yr_wk']].append(d )
test_df['vendor_id'] = test_df['vendor_id'].astype('category' ).cat.codes 
<feature_engineering>
with open(PRICES, "r", newline='')as f:
is_header = True
for row in csv.reader(f):
if is_header:
is_header = False
continue
store_id, item_id, wm_yr_wk, sell_price = row
series_id = f"{item_id}_{store_id}"
series_idx = id_idx[series_id]
for d in wm_yr_wk_idx[wm_yr_wk]:
sell_prices[d - 1, series_idx] = float(sell_price )
test_df['distance'] = test_df.apply(lambda x : calcul_distance(x), axis = 1 )<data_type_conversions>
qty_ts = pd.DataFrame(qties,
index=range(1, NUM_TRAINING + 1),
columns=[state_ids, store_ids,
cat_ids, dept_ids, item_ids])
 qty_ts.index.names = ['d']
qty_ts.columns.names = ['state_id', 'store_id',
'cat_id', 'dept_id', 'item_id']
price_ts = pd.DataFrame(sell_prices,
index=range(1, NUM_TEST + 1),
columns=[state_ids, store_ids,
cat_ids, dept_ids, item_ids])
 price_ts.index.names = ['d']
price_ts.columns.names = ['state_id', 'store_id',
'cat_id', 'dept_id', 'item_id']
test_df_da = pd.to_datetime(test_df['pickup_datetime'])
 test_df['month'] = test_df_da.dt.month.astype('category' ).cat.codes
test_df['hour'] = test_df_da.dt.hour.astype('category' ).cat.codes
test_df['wday'] = test_df_da.dt.weekday.astype('category' ).cat.codes<merge>
LEVELS = {
1: [],
2: ['state_id'],
3: ['store_id'],
4: ['cat_id'],
5: ['dept_id'],
6: ['state_id', 'cat_id'],
7: ['state_id', 'dept_id'],
8: ['store_id', 'cat_id'],
9: ['store_id', 'dept_id'],
10: ['item_id'],
11: ['state_id', 'item_id'],
12: ['item_id', 'store_id']
}
COARSER = {
'state_id': [],
'store_id': ['state_id'],
'cat_id': [],
'dept_id': ['cat_id'],
'item_id': ['cat_id', 'dept_id']
}
test_all = test_df.merge(train_test, on='id', how='inner' )<data_type_conversions>
def aggregate_all_levels(df):
levels = []
for i in range(1, max(LEVELS.keys())+ 1):
level = aggregate_groupings(df, i, *LEVELS[i])
 levels.append(level)
 return pd.concat(levels, axis=1)
 def aggregate_groupings(df, level_id, grouping_a=None, grouping_b=None):

 if grouping_a is None and grouping_b is None:
new_df = df.sum(axis=1 ).to_frame()
elif grouping_b is None:
new_df = df.groupby(COARSER[grouping_a] + [grouping_a], axis=1 ).sum()
else:
assert grouping_a is not None
new_df = df.groupby(COARSER[grouping_a] + COARSER[grouping_b] +
[grouping_a, grouping_b], axis=1 ).sum()
new_df.columns = _restore_columns(df.columns, new_df.columns, level_id,
grouping_a, grouping_b)
 return new_df
test_d_da = pd.to_datetime(test_all['pickup_datetime'])
 test_all['month'] = test_d_da.dt.month.astype('category' ).cat.codes
test_all['hour'] = test_d_da.dt.hour.astype('category' ).cat.codes
test_all['wday'] = test_d_da.dt.weekday.astype('category' ).cat.codes<save_to_csv>
def _restore_columns(original_index, new_index, level_id, grouping_a, grouping_b):
original_df = original_index.to_frame()
new_df = new_index.to_frame()
for column in original_df.columns:
if column not in new_df.columns:
new_df[column] = None
new_df['level'] = level_id
if grouping_a is None and grouping_b is None:
new_df['id'] = 'Total_X'
elif grouping_b is None:
new_df['id'] = new_df[grouping_a] + '_X'
else:
assert grouping_a is not None
new_df['id'] = new_df[grouping_a] + '_' + new_df[grouping_b]
new_index = pd.MultiIndex.from_frame(new_df)
 if grouping_a is None and grouping_b is None:
new_index = new_index.droplevel(0)
 new_levels = ['level'] + original_index.names + ['id']
return new_index.reorder_levels(new_levels )
predictions = np.exp(rf.predict(X_test)) -np.ones(len(X_test))
X_test.shape
pred = pd.DataFrame(predictions, index=test_df['id'])
 pred.columns = ['trip_duration']
pred.to_csv("dat1.csv")
 pd.read_csv('dat1.csv' ).head()<compute_train_metric>
df_train = agg_pd.T.reset_index()
df_train = df_train.set_index("id")
 rename_dict = {}
for c in df_train.columns:
if c not in ['level', 'state_id', 'store_id', 'cat_id', 'dept_id', 'item_id']:
rename_dict[c] = "d_%s" % c
df_train.rename(columns=rename_dict, inplace=True)
 day_cols = pd.Series([c for c in df_train.columns if c not in ['level', 'state_id', 'store_id', 'cat_id', 'dept_id', 'item_id']])
 print(df_train.shape)
 df_train.head()
scores = -cross_val_score(rf, X, y, cv=2, scoring = 'neg_mean_squared_error' )<install_modules>
df_calendar = pd.read_csv(CALENDAR)
 df_calendar.index = df_calendar['d'].values
df_calendar['ds'] = pd.to_datetime(df_calendar['date'])
 df_calendar['quarter'] = df_calendar['ds'].dt.quarter
df_calendar.head()
!pip install knockknock
!pip install -q efficientnet<import_modules>
events1 = pd.Series(df_calendar['event_name_1'].values, index=df_calendar['ds'].values ).dropna()
events2 = pd.Series(df_calendar['event_name_2'].values, index=df_calendar['ds'].values ).dropna()
holidays = pd.DataFrame(pd.concat([events1, events2], axis=0))
holidays['ds'] = holidays.index.values
holidays.rename({0: 'holiday'}, axis=1, inplace=True)
 holidays.reset_index(drop=True, inplace=True)
 del events1, events2
holidays.head()
user_secrets = UserSecretsClient()
token = user_secrets.get_secret("token")
 chat_id = user_secrets.get_secret("chat_id")
 warnings.filterwarnings('ignore' )<train_on_grid>
def clean_data(df_train, day_cols, indx):
t = df_train.loc[indx].copy()
t.loc[day_cols[(( t.loc[day_cols]>0 ).cumsum() ==0 ).values]] = np.nan
q1 = t.loc[day_cols].quantile(0.25)
 q3 = t.loc[day_cols].quantile(0.75)
 iqr = q3-q1
qm =(q3+1.5*iqr)
 t.loc[day_cols][t.loc[day_cols]>qm] = qm
return t
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()<define_search_space>
future_preds = 28
day_fit_cols = day_cols
SEED = 42
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
SIZE = [800,800]
LR = 0.0008
WEIGHT_DECAY = 0
EPOCHS = 40
WARMUP = 15
TTA = 4<define_variables>
ignore_dates = ["2011-12-25", "2012-12-25", "2013-12-25", "2014-12-25", "2015-12-25",
"2011-12-24", "2012-12-24", "2013-12-24", "2014-12-24", "2015-12-24",
"2011-12-31", "2012-12-31", "2013-12-31", "2014-12-31", "2015-12-31",
"2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01"]
ignore_days = []
def seed_everything(SEED):
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 seed_everything(SEED )<load_from_csv>
def make_prediction(indx, model_columns = 'yhat', ret_columns = 'yhat', full_predict = True):
global df_train, holidays, df_calendar
changepoints=list()
uncertainty_samples=False
changepoint_prior_scale=0.1
changepoint_range=0.9
n_changepoints=50
holidays_prior_scale=10
yearly_seasonality=10
weekly_seasonality=3
daily_seasonality=False
monthly_fourier_order=8
quarter_fourier_order=6
seasonality_prior_scale=10
seasonality_mode = 'multiplicative'
target = df_train.loc[indx, day_fit_cols]
snap_state_id = str(df_train.loc[indx, 'state_id'])
 cols = ['ds', 'month', 'wday', 'quarter']
if snap_state_id in ["CA", "TX", "WI"]:
cols = cols + ['snap_'+snap_state_id]
df = df_calendar.iloc[:target.shape[0]+future_preds][cols].copy()
df['y'] = target
df['ft_lag28'] = df['y'].shift(28)
 df["ft_lag28"].fillna(method ='bfill', inplace = True)
 df['ft_lag35'] = df['y'].shift(35)
 df["ft_lag35"].fillna(method ='bfill', inplace = True)
 df['ft_lag42'] = df['y'].shift(42)
 df["ft_lag42"].fillna(method ='bfill', inplace = True)
 df['ft_lag28_roll28_std'] = df['y'].shift(28 ).rolling(28 ).std()
df["ft_lag28_roll28_std"].fillna(method ='bfill', inplace = True)
 df['ft_lag28_roll28_max'] = df['y'].shift(28 ).rolling(28 ).max()
df["ft_lag28_roll28_max"].fillna(method ='bfill', inplace = True)
 df['ft_lag28_roll28_min'] = df['y'].shift(28 ).rolling(28 ).min()
df["ft_lag28_roll28_min"].fillna(method ='bfill', inplace = True)
 GROWTH = 'linear'
m = Prophet(growth=GROWTH, uncertainty_samples=uncertainty_samples, changepoint_prior_scale=changepoint_prior_scale, changepoint_range=changepoint_range,
n_changepoints = n_changepoints,
holidays_prior_scale=holidays_prior_scale, yearly_seasonality=yearly_seasonality,
daily_seasonality=daily_seasonality, weekly_seasonality=weekly_seasonality,
holidays=holidays, seasonality_mode=seasonality_mode, seasonality_prior_scale=seasonality_prior_scale)
 m.add_country_holidays(country_name='US')
 if not monthly_fourier_order is None:
m.add_seasonality(name='monthly', period=365.25/12, fourier_order=monthly_fourier_order)
 if not quarter_fourier_order is None:
m.add_seasonality(name='quarterly', period=365.25/4, fourier_order=quarter_fourier_order)
 for reg in df.columns:
if reg!='ds' and reg!='y':
m.add_regressor(reg)
 target_first_valid_index = target.first_valid_index()
if GROWTH == "logistic":
df["cap"] = df["y"].max()
df["floor"] = df["y"].min()
m.fit(df.loc[target.loc[target_first_valid_index:].index])
 if 'y' not in ret_columns:
df.drop(['y'], axis=1, inplace=True)
 res = None
if full_predict == True:
forecast = m.predict(df.loc[target_first_valid_index:])
 forecast["yhat"] = forecast["yhat"].astype(np.float32)
 res = forecast[model_columns]
res.index = df.loc[target_first_valid_index:].index.values
res = df.merge(res, left_index=True, right_index=True, how="left")[ret_columns]
else:
forecast = m.predict(df.iloc[-future_preds:])
 forecast["yhat"] = forecast["yhat"].astype(np.float32)
 res = forecast[model_columns]
res.index = df.iloc[-future_preds:].index.values
return(indx, res )
DIR_INPUT = '/kaggle/input/plant-pathology-2020-fgvc7'
train_df = pd.read_csv(DIR_INPUT + '/train.csv')
 test_df = pd.read_csv(DIR_INPUT + '/test.csv')
 cols = list(train_df.columns[1:] )<split>
train_indxs = ["Total_X"]
train_indxs = train_indxs + ["CA_X", "TX_X", "WI_X"]
train_indxs = train_indxs + ['CA_1_X', 'CA_2_X', 'CA_3_X','CA_4_X', 'TX_1_X', 'TX_2_X', 'TX_3_X', 'WI_1_X', 'WI_2_X', 'WI_3_X']
train_indxs = train_indxs + ['HOBBIES_X', 'FOODS_X', 'HOUSEHOLD_X']
train_indxs = train_indxs + ['HOBBIES_1_X', 'HOBBIES_2_X', 'FOODS_1_X', 'FOODS_2_X', 'FOODS_3_X', 'HOUSEHOLD_1_X', 'HOUSEHOLD_2_X']
m_score = 0
for train_ind in train_indxs: 
(_, pred)= make_prediction(train_ind, model_columns = ['yhat'], ret_columns = ['yhat', 'ds', 'y'])
 fig, ax = plt.subplots(figsize=(DEFAULT_FIG_WIDTH, 4))
d = pred.set_index("ds" ).plot(kind="line", y=["y", "yhat"], ax=ax, linestyle='-', linewidth=0.8)
 score = np.log1p(mean_squared_error(pred["y"][:-28], pred["yhat"][:-28]))
m_score = m_score +(score / len(train_indxs))
plt.title("%s LRMSE=%.3f" %(train_ind, score))
plt.show()
print("Average LRMSE=%.3f" % m_score )
train,valid = train_test_split(train_df,test_size = 0.2,random_state = SEED )<categorify>
print('Predicting...', flush=True)
 start_time = time.time()
train_indxs = df_train.index
res = []
for train_indx in tqdm(train_indxs):
r = make_prediction(train_indx)
 res.append(r)
 end_time = time.time()
print('Exec speed=%.2f' %(( end_time-start_time)/train_indxs.shape[0]))
train_dataset =(tf.data.Dataset 
.from_tensor_slices(preprocess(train)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.shuffle(SEED)
.batch(BATCH_SIZE,drop_remainder=True)
.repeat() 
.prefetch(AUTO))
valid_dataset =(tf.data.Dataset 
.from_tensor_slices(preprocess(valid)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO))
test_dataset =(tf.data.Dataset 
.from_tensor_slices(preprocess(test_df,test=True)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))<choose_model_class>
tmp = pd.DataFrame()
for result in res:
uid = result[0]
ret = result[1].rename(uid)
 tmp = pd.concat([tmp, ret], axis=1)
 fbp_pd = tmp.T
fbp_pd.index.name = "id"
fbp_pd.head(10 )
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB7(input_shape=(SIZE[0], SIZE[1], 3),weights='imagenet',pooling='avg',include_top=False),
Dense(4, activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] )<normalization>
fbp_pd.to_pickle("model_fit.pkl.gz", compression="gzip" )
def get_cosine_schedule_with_warmup(lr,num_warmup_steps, num_training_steps, num_cycles=0.5):

 def lrfn(epoch):
if epoch < num_warmup_steps:
return float(epoch)/ float(max(1, num_warmup_steps)) * lr
progress = float(epoch - num_warmup_steps)/ float(max(1, num_training_steps - num_warmup_steps))
return max(0.0, 0.5 *(1.0 + math.cos(math.pi * float(num_cycles)* 2.0 * progress)))* lr
return tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 lr_schedule= get_cosine_schedule_with_warmup(lr=LR,num_warmup_steps=WARMUP,num_training_steps=EPOCHS )<train_model>
yhat_df = pd.merge(yhat_df, df_train.reset_index() [['id', 'level', 'state_id', 'store_id', 'cat_id', 'dept_id', 'item_id']], on=["id"], how="left")
 print(yhat_df.shape)
 yhat_df.head()
STEPS_PER_EPOCH = train.shape[0] // BATCH_SIZE
@telegram_sender(token=token, chat_id=int(chat_id))
def train() :
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset)
 string = 'Train acc:{:.4f} Train loss:{:.4f},Val acc:{:.4f} Val loss:{:.4f}'.format(\
model.history.history['categorical_accuracy'][-1],model.history.history['loss'][-1],\
model.history.history['val_categorical_accuracy'][-1],model.history.history['val_loss'][-1])
 return string<train_model>
level_coef_dict = {
12: ["id"],
10: ["item_id"],
5: ["dept_id"],
4: ["cat_id"],
3: ["store_id"],
2: ["state_id"],
1: ["all"],
11: ["state_id", "item_id"],
7: ["state_id", "dept_id"],
9: ["store_id","dept_id"],
6: ["state_id", "cat_id"],
8: ["store_id","cat_id"],
}
train()<save_to_csv>
def predict_topdown(df, df_level12, items):
df_level_forecast = pd.DataFrame()
for idx, row in df.iterrows() :
item1 = items[0]
if item1 is not "all":
item1_id = row[item1]
if len(items)== 2:
item2 = items[1]
item2_id = row[items[1]]
if item1 is not "all":
if len(items)== 2:
df_item = df_level12.loc[(df_level12[item1] == item1_id)&(df_level12[item2] == item2_id)][['id']]
else:
df_item = df_level12.loc[df_level12[item1] == item1_id][['id']]
else:
df_item = df_level12[['id']]
if item1 is not "all":
if len(items)== 2:
df_item['val'] = df_level12[(df_level12[item1] == item1_id)&(df_level12[item2] == item2_id)].iloc[:, np.r_[0,-28:0]].sum(axis = 1)
 else:
df_item['val'] = df_level12[df_level12[item1] == item1_id].iloc[:, np.r_[0,-28:0]].sum(axis = 1)
 else:
df_item['val'] = df_level12.iloc[:, np.r_[0,-28:0]].sum(axis = 1)
 for i in range(1,29):
col = "d_%d" %(1913 + i)
 p_col = "F%d" % i
df_item[p_col] =(df_item['val'] * float(row[col])/ df_item['val'].sum())
 df_level_forecast = pd.concat([df_level_forecast, df_item])
 return df_level_forecast.drop(columns=["val"] )
test_pred = model.predict(test_dataset, verbose=1)
 submission_df = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = test_pred
submission_df.to_csv('submission.csv', index=False)
 pd.Series(np.argmax(submission_df[cols].values,axis=1)).value_counts()<save_to_csv>
for key, value in level_coef_dict.items() :
if(key <= MAX_LEVEL)and(key >= 1):
predictions = yhat_df[yhat_df["level"] == key]
df_levelx_forecast = predict_topdown(predictions, df_sale, value)
 print("Top-Down prediction for level %s, %s, items=%s into %s" %(key, value, predictions.shape[0], df_levelx_forecast.shape[0]))
df_levelx_forecast.to_pickle(MODELS_DIR + "/level_%d.pkl.gz" % key, compression="gzip")
 print(df_levelx_forecast.shape)
 df_levelx_forecast.head()
test_pred_tta = np.zeros(( len(test_df),4))
for i in range(TTA):
test_dataset_tta =(tf.data.Dataset 
.from_tensor_slices(preprocess(test_df,test=True)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_pred_tta += model.predict(test_dataset_tta, verbose=1)
 submission_df = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = test_pred_tta/TTA
submission_df.to_csv('submission_tta.csv', index=False)
 pd.Series(np.argmax(submission_df[cols].values,axis=1)).value_counts()<install_modules>
SAMPLE_SUBMISSION = TRAIN_DATA_HOME + "sample_submission.csv"
sub_ids = pd.read_csv(SAMPLE_SUBMISSION)[['id']]
sub_ids = sub_ids[sub_ids.id.str.endswith("validation")]
!pip install knockknock
!pip install efficientnet-pytorch<import_modules>
for key, value in level_coef_dict.items() :
if(key <= MAX_LEVEL)and(key >= 1):
predictions = yhat_df[yhat_df["level"] == key]
df_levelx_forecast = predict_topdown(predictions, df_sale, value)
 print("Top-Down prediction for level %s, %s, items=%s into %s" %(key, value, predictions.shape[0], df_levelx_forecast.shape[0]))
df_levelx_forecast["id"] = df_levelx_forecast["id"].astype(str)+ "_validation"
part1 = pd.merge(sub_ids, df_levelx_forecast, on="id", how="left")
 part2 = part1.copy()
part2["id"] = part2["id"].str.replace("validation$", "evaluation")
 sub = pd.concat([part1, part2])
 MODE_LEVEL_PATH = MODELS_DIR + "/level_%d" % key
if not os.path.exists(MODE_LEVEL_PATH):
os.makedirs(MODE_LEVEL_PATH)
 sub.to_csv(MODE_LEVEL_PATH + "/submission.csv.gz", compression="gzip", index=False )