Kaggle competition: Predict Future Sales
rf_clf.fit(X, y_train_data['surface'] )<compute_test_metric>
PATH = 'competitive-data-science-predict-future-sales'
items = pd.read_csv('.. /input/' + PATH + '/items.csv')
 shops = pd.read_csv('.. /input/' + PATH + '/shops.csv')
 cats = pd.read_csv('.. /input/' + PATH + '/item_categories.csv')
 train = pd.read_csv('.. /input/' + PATH + '/sales_train.csv')
 test = pd.read_csv('.. /input/' + PATH + '/test.csv' ).set_index('ID')
 print('------------- train info ------------'); print(train.info() , '
')
 print('------------- test info ------------'); print(test.info() , '
')
 print('------------- items info ------------'); print(items.info() , '
')
 print('------------- shops info ------------'); print(shops.info() , '
')
 print('------------- categories info ------------'); print(cats.info() , '
' )
print('Accuracy on train data: ', rf_clf.score(X, y_train_data['surface']))
print('Accuracy on validation data: ', rf_clf.score(X_val, y_val_data['surface']))<predict_on_test>
print(train.isna().sum() , '
')
 print(test.isna().sum() )
pred_train = rf_clf.predict(X)
 pred_val = rf_clf.predict(X_val )<merge>
train = train[train.item_price > 0].reset_index(drop=True)
 train[train.item_cnt_day <= 0].item_cnt_day.unique()
train.loc[train.item_cnt_day < 1, 'item_cnt_day'] = 0
X = y_train.merge(X_train, on='series_id')
 X = X.drop(['group_id', 'run_id', 'run_pos', 'series_id', 'surface'], axis=1)
 X = np.array(X )<choose_model_class>
train.loc[train.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 11, 'shop_id'] = 10
test.loc[test.shop_id == 11, 'shop_id'] = 10
train.loc[train.shop_id == 40, 'shop_id'] = 39
test.loc[test.shop_id == 40, 'shop_id'] = 39
rf_clf = RandomForestClassifier(n_estimators=100, max_depth=11, verbose=1,
class_weight='balanced',
random_state=11, n_jobs=10 )<train_model>
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops['category'] = shops['shop_name'].str.split(' ' ).map(lambda x:x[1] ).astype(str)
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
category = ['Орджоникидзе,', 'ТЦ', 'ТРК', 'ТРЦ','ул.', 'Магазин', 'ТК', 'склад']
shops.category = shops.category.apply(lambda x: x if(x in category)else 'etc')
 shops.category.unique()
rf_clf.fit(X, y_train['surface'] )<predict_on_test>
category = ['ТЦ', 'ТРК', 'ТРЦ', 'ТК']
shops.category = shops.category.apply(lambda x: x if(x in category)else 'etc')
 print('Category Distribution', shops.groupby(['category'] ).sum() )
subm_result = rf_clf.predict(X_t )<prepare_output>
shops['shop_city'] = shops.city
shops['shop_category'] = shops.category
shops['shop_city'] = LabelEncoder().fit_transform(shops['shop_city'])
 shops['shop_category'] = LabelEncoder().fit_transform(shops['shop_category'])
 shops = shops[['shop_id','shop_city', 'shop_category']]
shops.head()
submission = pd.DataFrame(columns = ['series_id', 'surface'])
 submission['series_id'] = X_test.series_id.unique()
submission['surface'] = subm_result<save_to_csv>
print(len(cats.item_category_name.unique()))
cats.item_category_name.unique()
submission.to_csv('submission.csv', index=False )<save_to_csv>
cats['type_code'] = cats.item_category_name.apply(lambda x: x.split(' ')[0] ).astype(str)
 cats.loc[(cats.type_code == 'Игровые')|(cats.type_code == 'Аксессуары'), 'category'] = 'Игры'
cats.loc[cats.type_code == 'PC', 'category'] = 'Музыка'
category = ['Игры', 'Карты', 'Кино', 'Книги','Музыка', 'Подарки', 'Программы', 'Служебные', 'Чистые']
cats['type_code'] = cats.type_code.apply(lambda x: x if(x in category)else 'etc')
 print(cats.groupby(['type_code'] ).sum())
 cats['type_code'] = LabelEncoder().fit_transform(cats['type_code'])
 cats['split'] = cats.item_category_name.apply(lambda x: x.split('-'))
cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id','type_code', 'subtype_code']]
submission.to_csv('submission.csv', index=False )<set_options>
items = pd.read_csv('.. /input/' + PATH + '/items.csv')
 items['name_1'], items['name_2'] = items['item_name'].str.split('[', 1 ).str
items['name_1'], items['name_3'] = items['item_name'].str.split('(', 1 ).str
items['name_2'] = items['name_2'].str.replace('[^A-Za-z0-9А-Яа-я]+', ' ' ).str.lower()
items['name_3'] = items['name_3'].str.replace('[^A-Za-z0-9А-Яа-я]+', ' ' ).str.lower()
items = items.fillna('0')
 result_1 = Counter(' '.join(items['name_2'].values.tolist() ).split(' ')).items()
result_1 = sorted(result_1, key=itemgetter(1))
result_1 = pd.DataFrame(result_1, columns=['feature', 'count'])
 result_1 = result_1[(result_1['feature'].str.len() > 1)&(result_1['count'] > 200)]
result_2 = Counter(' '.join(items['name_3'].values.tolist() ).split(" ")).items()
result_2 = sorted(result_2, key=itemgetter(1))
result_2 = pd.DataFrame(result_2, columns=['feature', 'count'])
 result_2 = result_2[(result_2['feature'].str.len() > 1)&(result_2['count'] > 200)]
result = pd.concat([result_1, result_2])
 result = result.drop_duplicates(subset=['feature'] ).reset_index(drop=True)
 print('Most common aditional features:', result)
 def name_correction(x):
x = x.lower()
x = x.partition('[')[0]
x = x.partition('(')[0]
x = re.sub('[^A-Za-z0-9А-Яа-я]+', ' ', x)
 x = x.replace(' ', ' ')
 x = x.strip()
return x
items['item_name'] = items['item_name'].apply(lambda x: name_correction(x))
items.name_2 = items.name_2.apply(lambda x: x[:-1] if x != '0' else '0' )
%matplotlib inline 
<set_options>
items['type'] = items.name_2.apply(lambda x: x[0:8] if x.split(' ')[0] == 'xbox' else x.split(' ')[0])
 items.loc[(items.type == 'x360')|(items.type == 'xbox360'), 'type'] = 'xbox 360'
items.loc[items.type == '', 'type'] = 'mac'
items.type = items.type.apply(lambda x: x.replace(' ',''))
items.loc[(items.type == 'pc')|(items.type == 'pс')|(items.type == 'рс'), 'type'] = 'pc'
items.loc[(items.type == 'рs3'), 'type'] = 'ps3'
group_sum = items.groupby('type' ).sum()
group_sum.loc[group_sum.item_category_id < 200]
warnings.filterwarnings('ignore')
 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
<load_from_csv>
drop_list = ['5c5', '5c7', '5f4', '6dv', '6jv', '6l6', 'android', 'hm3', 'j72', 'kf6', 'kf7','kg4',
'ps2', 's3v', 's4v'	,'англ', 'русская', 'только', 'цифро']
items.name_2 = items.type.apply(lambda x: 'etc' if x in drop_list else x)
 items = items.drop(['type'], axis=1)
 items.groupby('name_2' ).sum()
train = pd.read_csv('.. /input/career-con-2019/X_train.csv')
 target = pd.read_csv('.. /input/career-con-2019/y_train.csv')
 test = pd.read_csv('.. /input/career-con-2019/X_test.csv')
 sub = pd.read_csv('.. /input/career-con-2019/sample_submission.csv' )<count_unique_values>
items['name_2'] = LabelEncoder().fit_transform(items['name_2'])
 items['name_3'] = LabelEncoder().fit_transform(items['name_3'])
 items.drop(['item_name', 'name_1'], axis=1, inplace=True)
 items.head()
train['series_id'].nunique() , test['series_id'].nunique()<count_unique_values>
ts = time.time()
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train[train.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True)
 time.time() - ts
target['group_id'].nunique()<count_values>
train['revenue'] = train['item_price'] * train['item_cnt_day']
target['surface'].value_counts().reset_index().rename(columns={'index': 'target'} )<normalization>
ts = time.time()
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))
time.time() - ts
<feature_engineering>
test['date_block_num'] = 34
test['date_block_num'] = test['date_block_num'].astype(np.int8)
 test['shop_id'] = test['shop_id'].astype(np.int8)
 test['item_id'] = test['item_id'].astype(np.int16 )
<categorify>
ts = time.time()
matrix = pd.concat([matrix, test], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace=True)
 time.time() - ts
<create_dataframe>
ts = time.time()
matrix = pd.merge(matrix, shops, on=['shop_id'], how='left')
 matrix = pd.merge(matrix, items, on=['item_id'], how='left')
 matrix = pd.merge(matrix, cats, on=['item_category_id'], how='left')
 matrix['shop_city'] = matrix['shop_city'].astype(np.int8)
 matrix['shop_category'] = matrix['shop_category'].astype(np.int8)
 matrix['item_category_id'] = matrix['item_category_id'].astype(np.int8)
 matrix['type_code'] = matrix['type_code'].astype(np.int8)
 matrix['subtype_code'] = matrix['subtype_code'].astype(np.int8)
 time.time() - ts
<create_dataframe>
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
train_df = train.copy()
test_df = test.copy()<feature_engineering>
ts = time.time()
group = matrix.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num'], how='left')
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_avg_item_cnt')
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
def feat_diff(data):
for col in data.columns[3:]:
data[col+'_diff'] = data.groupby(['series_id'])[col].diff().fillna(0)
 return data
def feat_make(data):
data = feat_diff(data)
 X0 = data.loc[:, 'orientation_X':'orientation_W'].values.reshape(-1, 128, 4)
 X1 = data.loc[:, 'angular_velocity_X': 'angular_velocity_Z'].values.reshape(-1, 128, 3)
 X2 = data.loc[:, 'linear_acceleration_X': 'linear_acceleration_Z'].values.reshape(-1, 128, 3)
 X7 = data.loc[:, 'orientation_X_diff':'orientation_W_diff'].values.reshape(-1, 128, 4)
 X8 = data.loc[:, 'angular_velocity_X_diff': 'angular_velocity_Z_diff'].values.reshape(-1, 128, 3)
 X9 = data.loc[:, 'linear_acceleration_X_diff': 'linear_acceleration_Z_diff'].values.reshape(-1, 128, 3)
 return X0, X1, X2, X7, X8, X9<compute_test_metric>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], 'date_item_avg_item_cnt')
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
def mfft(x):
return [ x/math.sqrt(128.0)for x in np.absolute(np.fft.fft(x)) ][1:65]
def feat_fft(X_trn):
ars=[]
for ar in X_trn:
ar= ar.T.tolist()
ms= []
for line in ar:
m= mfft(line)
 ms.append(m)
 ms= np.array(ms ).T.tolist()
ars.append(ms)
 ars= np.array(ars)
 return ars<create_dataframe>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_avg_item_cnt'] = matrix['date_shop_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], 'date_shop_avg_item_cnt')
 matrix.drop(['date_shop_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
<categorify>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_cat_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_category_id'], how='left')
 matrix['date_cat_avg_item_cnt'] = matrix['date_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_cat_avg_item_cnt')
 matrix.drop(['date_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
le = preprocessing.LabelEncoder()
le.fit(target['surface'])
 y_train= le.transform(target['surface'])
 le.classes_<categorify>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_cat_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 matrix['date_shop_cat_avg_item_cnt'] = matrix['date_shop_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_cat_avg_item_cnt')
 matrix.drop(['date_shop_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
le.transform(['carpet', 'concrete', 'fine_concrete', 'hard_tiles',
'hard_tiles_large_space', 'soft_pvc', 'soft_tiles', 'tiled',
'wood'] )<split>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'itme_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_item_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'itme_id'], how='left')
 matrix['date_shop_item_avg_item_cnt'] = matrix['date_shop_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_item_avg_item_cnt')
 matrix.drop(['date_shop_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
X_trn_q, X_trn_a, X_trn_l, X_trn_q_d, X_trn_a_d, X_trn_l_d = feat_make(train_df)
 X_tst_q, X_tst_a, X_tst_l, X_tst_q_d, X_tst_a_d, X_tst_l_d = feat_make(test_df )<feature_engineering>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix['date_shop_subtype_avg_item_cnt'] = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_subtype_avg_item_cnt')
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
X_trn_q_f = feat_fft(X_trn_q)
 X_trn_a_f = feat_fft(X_trn_a)
 X_trn_l_f = feat_fft(X_trn_l)
 X_trn_q_d_f = feat_fft(X_trn_q_d)
 X_trn_a_d_f = feat_fft(X_trn_a_d)
 X_trn_l_d_f = feat_fft(X_trn_l_d)
 X_tst_q_f = feat_fft(X_tst_q)
 X_tst_a_f = feat_fft(X_tst_a)
 X_tst_l_f = feat_fft(X_tst_l)
 X_tst_q_d_f = feat_fft(X_tst_q_d)
 X_tst_a_d_f = feat_fft(X_tst_a_d)
 X_tst_l_d_f = feat_fft(X_tst_l_d)
<sort_values>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_city'], how='left')
 matrix['date_city_avg_item_cnt'] = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_city_avg_item_cnt')
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
def run_set(X_trn):
for i in run_id:
X_run = target[:][target['run_id']==i]
idx= X_run.index
X= X_trn[idx].tolist()
X=[item for sublist in X for item in sublist]
end=int(len(X)/2048)*2048
X=X[:end]
X=np.array(X ).reshape(-1, 2048, X_trn.shape[-1])
 return X
def test_run_set(X_trn):
X_=[]
for i in test_run_id:
X_run = test_target[:][test_target['run_id']==i]
idx= X_run.index
X= X_trn[idx].tolist()
X=[item for sublist in X for item in sublist]
end=int(len(X)/2048)*2048
X=X[:end]
X=np.array(X ).reshape(-1, 2048, X_trn.shape[-1])
 return X<categorify>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'shop_city'], how='left')
 matrix['date_item_city_avg_item_cnt'] = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_item_city_avg_item_cnt')
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
X_trn_ = [X_trn_q, X_trn_a, X_trn_l, X_trn_q_d, X_trn_a_d, X_trn_l_d,
X_trn_a_f, X_trn_l_f, X_trn_a_d_f, X_trn_l_d_f] 
<define_variables>
X_train = [run_set(x)for x in X_trn_]<define_variables>
ts = time.time()
group = matrix.groupby(['date_block_num','shop_id' ,'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id' ,'item_id'], how='left')
 matrix['date_shop_item_avg_item_cnt'] = matrix['date_shop_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_item_avg_item_cnt')
 matrix.drop(['date_shop_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
X_test= [X_tst_q, X_tst_a, X_tst_l, X_tst_q_d, X_tst_a_d, X_tst_l_d,
X_tst_a_f, X_tst_l_f, X_tst_a_d_f, X_tst_l_d_f] 
<choose_model_class>
ts = time.time()
group = train.groupby(['item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['item_id'], how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group = train.groupby(['date_block_num','item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1,2,3]
matrix = lag_feature(matrix, lags, 'date_item_avg_item_price')
 for i in lags:
matrix['delta_price_lag_'+str(i)] = \ 
(matrix['date_item_avg_item_price_lag_'+str(i)] - matrix['item_avg_item_price'])/ matrix['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
matrix['delta_price_lag'] = matrix.apply(select_trend, axis=1)
 matrix['delta_price_lag'] = matrix['delta_price_lag'].astype(np.float16)
 matrix['delta_price_lag'].fillna(0, inplace=True)
 fetures_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
fetures_to_drop += ['date_item_avg_item_price_lag_'+str(i)]
fetures_to_drop += ['delta_price_lag_'+str(i)]
matrix.drop(fetures_to_drop, axis=1, inplace=True)
 time.time() - ts
class Head:
def build(n_steps, n_features):
inputA = Input(shape=(n_steps, n_features))
x = Conv1D(filters=100, kernel_size=10, activation='relu' )(inputA)
 x = Conv1D(filters=100, kernel_size=10, activation='relu' )(x)
 x = MaxPooling1D(pool_size=3 )(x)
 x = Conv1D(filters=160, kernel_size=10, activation='relu' )(x)
 x = Conv1D(filters=160, kernel_size=10, activation='relu' )(x)
 x = AveragePooling1D(pool_size=3 )(x)
 x = Dropout(0.5 )(x)
 x = Flatten()(x)
 x = Model(inputs= inputA, outputs= x)
 return x
def build_f(n_steps, n_features):
inputA = Input(shape=(n_steps, n_features))
x = Conv1D(filters=100, kernel_size=5, activation='relu' )(inputA)
 x = Conv1D(filters=100, kernel_size=5, activation='relu' )(x)
 x = MaxPooling1D(pool_size=3 )(x)
 x = Conv1D(filters=160, kernel_size=5, activation='relu' )(x)
 x = Conv1D(filters=160, kernel_size=5, activation='relu' )(x)
 x = AveragePooling1D(pool_size=3 )(x)
 x = Dropout(0.5 )(x)
 x = Flatten()(x)
 x = Model(inputs= inputA, outputs= x)
 return x<categorify>
ts = time.time()
group = train.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['shop_id'], how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'delta_revenue')
 matrix.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True)
 time.time() - ts
def sub_model(X_trn, y_trn):
flatA1 = Head.build(2048, 4)
 flatA2 = Head.build(2048, 3)
 flatA3 = Head.build(2048, 3)
 flatB1 = Head.build(2048, 4)
 flatB2 = Head.build(2048, 3)
 flatB3 = Head.build(2048, 3)
 fftA2 = Head.build_f(2048, 3)
 fftA3 = Head.build_f(2048, 3)
 fftB2 = Head.build_f(2048, 3)
 fftB3 = Head.build_f(2048, 3)
 x= concatenate([
flatA1.output, flatA2.output, flatA3.output,
flatB1.output, flatB2.output, flatB3.output,
fftA2.output, fftA3.output,
fftB2.output, fftB3.output,
])
 x = Dense(500, activation='relu' )(x)
 x = Dense(100, activation='relu' )(x)
 x = Dense(n_surfaces, activation='softmax' )(x)
 model = Model(inputs=[
flatA1.input, flatA2.input, flatA3.input,
flatB1.input, flatB2.input, flatB3.input,
fftA2.input, fftA3.input,
fftB2.input, fftB3.input,
],
outputs= x)
 plot_model(model, show_shapes=True, to_file='multichannel.png')
 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.fit(x= X_trn, y= y_trn, epochs= epochs, batch_size= batch_size, verbose= verbose)
 return model<load_pretrained>
matrix['month'] = matrix['date_block_num'] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )
def load_all_models(n_models):
all_models = []
for i in range(n_models):
filename = 'model_' + str(i)+ '.h5'
model = load_model(filename)
 all_models.append(model)
 print('>loaded %s' % filename)
 return all_models<load_pretrained>
def define_stacked_model(members):
for i in range(len(members)) :
model = members[i]
for layer in model.layers:
layer.trainable = False
layer.name = 'ensemble_' + str(i)+ '_' + layer.name
ensemble_visible = [model.input for model in members]
ensemble_visible = [item for sublist in ensemble_visible for item in sublist]
ensemble_outputs = [model.output for model in members]
merge = concatenate(ensemble_outputs)
 hidden = Dense(45, activation='relu' )(merge)
 output = Dense(9, activation='softmax' )(hidden)
 model = Model(inputs=ensemble_visible, outputs=output)
 plot_model(model, show_shapes=True, to_file='model_graph.png')
 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<choose_model_class>
verbose, epochs, batch_size = 2, 15, 32
n_surfaces = 9
n_models= 2
folds = StratifiedShuffleSplit(n_splits= 2, test_size= 0.5, random_state=0)
 np.set_printoptions(precision=2)
 class_names= le.classes_<categorify>
ts = time.time()
matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')
 time.time() - ts
X_trn= X_train
y_trn= y_train
for fold_,(trn, val)in enumerate(folds.split(np.zeros(len(y_trn)) , y_trn)) :
print("fold {}".format(fold_))
y_oh = to_categorical(y_trn)
 xtrn= [x[trn] for x in X_trn]
for i in range(n_models):
model = sub_model(xtrn, y_oh[trn])
 filename = 'model_' + str(i)+ '.h5'
model.save(filename)
 print('>Saved %s' % filename)
 members = load_all_models(n_models)
 print('Loaded %d models' % len(members))
stacked_model = define_stacked_model(members)
 X_t = [x[trn] for x in X_trn]
X_t = [X_t for _ in range(n_models)]
X_t = [item for sublist in X_t for item in sublist]
X_v = [x[val] for x in X_trn]
X_v = [X_v for _ in range(n_models)]
X_v = [item for sublist in X_v for item in sublist]
stacked_model.fit(X_t, y_oh[trn], validation_data=(X_v, y_oh[val]), epochs=epochs, verbose=verbose)
 plt.plot(stacked_model.history.history['acc'])
 plt.plot(stacked_model.history.history['val_acc'])
 plt.title('model accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(['train', 'val'], loc='upper left')
 plt.show()
plt.plot(stacked_model.history.history['loss'])
 plt.plot(stacked_model.history.history['val_loss'])
 plt.title('model loss')
 plt.ylabel('loss')
 plt.xlabel('epoch')
 plt.legend(['train', 'val'], loc='upper left')
 plt.show()
y_pred_ = stacked_model.predict(X_v, verbose=verbose)
 y_pred = y_pred_.argmax(axis=-1)
 acc = accuracy_score(y_trn[val], y_pred)
 print('Stacked Test Accuracy: %.3f' % acc)
 plot_confusion_matrix(y_trn[val], y_pred, classes= class_names,
title='Confusion matrix, without normalization')
 plot_confusion_matrix(y_trn[val], y_pred, classes= class_names,
normalize=True, title='Normalized confusion matrix')
<choose_model_class>
ts = time.time()
matrix = matrix[matrix.date_block_num > 3]
time.time() - ts
n_folds = 2
for f in range(n_folds):
print('fold',f)
 y_oh = to_categorical(y_train)
 for i in range(n_models):
model = sub_model(X_train, y_oh)
 filename = 'model_' + str(i)+ '.h5'
model.save(filename)
 print('>Saved %s' % filename)
 members = load_all_models(n_models)
 print('Loaded %d models' % len(members))
stacked_model = define_stacked_model(members)
 X_t = X_train
X_t = [X_t for _ in range(n_models)]
X_t = [item for sublist in X_t for item in sublist]
stacked_model.fit(X_t, y_oh, epochs=5, verbose=verbose)
 X_tst = X_test
X_tst = [X_tst for _ in range(n_models)]
X_tst = [item for sublist in X_tst for item in sublist]
y_test_ = stacked_model.predict(X_tst, verbose=verbose)
 plt.plot(model.history.history['acc'])
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.plot(model.history.history['loss'])
 if f==0:
y_test = np.zeros_like(y_test_)
 y_test += y_test_
y_test = y_test/ n_folds<categorify>
ts = time.time()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
matrix = fill_na(matrix)
 time.time() - ts
y_test = y_test.argmax(axis=-1 )<categorify>
matrix.to_pickle('.. /working/data.pkl')
 del matrix
gc.collect() ;
y_run=[]
for i in run_id:
X_run = target[:][target['run_id']==i]
y = le.transform(X_run['surface'])[0]
y_run.append(y )<categorify>
rcParams['figure.figsize'] = 12, 4
for run_id, i in enumerate(y_test):
if test_target['run_id']==run_id:
test_target['surface']= le.inverse_transform(i )<save_to_csv>
data = pd.read_pickle('.. /working/data.pkl')
 test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 print(len(data.columns))
data.columns
subm = pd.DataFrame()
subm['series_id'] = test_target['series_id']
subm['surface']= test_target['surface']
subm = subm.sort_values('series_id')
 subm.to_csv("submission.csv", index= False )<install_modules>
data = data[[
'date_block_num', 'shop_id', 'item_id', 'item_cnt_month', 'shop_city',
'shop_category', 'item_category_id', 'name_2', 'name_3', 'type_code',
'subtype_code', 'item_cnt_month_lag_1', 'item_cnt_month_lag_2',
'item_cnt_month_lag_3', 'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1', 'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3', 'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2', 'date_shop_avg_item_cnt_lag_3',
'date_cat_avg_item_cnt_lag_1', 'date_shop_cat_avg_item_cnt_lag_1',
'date_shop_subtype_avg_item_cnt_lag_1', 'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'date_shop_item_avg_item_cnt_lag_1',
'delta_price_lag',
'delta_revenue_lag_1', 'month', 'days',
'item_shop_first_sale',
'item_first_sale'
]]
len(data.columns )
<set_options>
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
warnings.filterwarnings('ignore')
<define_variables>
del data
gc.collect() ;
xtrainPath = '.. /input/X_train.csv'
xtestPath = '.. /input/X_test.csv'
samplePath = '.. /input/sample_submission.csv'
ytrainPath = '.. /input/y_train.csv'<load_from_csv>
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
tree_method='gpu_hist',
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 20)
 time.time() - ts
<compute_test_metric><EOS> 
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))