Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<define_variables>
!cp.. /input/rapids/rapids.0.13.0 /opt/conda/envs/rapids.tar.gz
!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz > /dev/null
sys.path = ["/opt/conda/envs/rapids/lib/python3.6/site-packages"] + sys.path
sys.path = ["/opt/conda/envs/rapids/lib/python3.6"] + sys.path
sys.path = ["/opt/conda/envs/rapids/lib"] + sys.path
!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/
print('rapids ai installed' )
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 24
BATCH_SIZE = 128
WEIGHT_DECAY = {'ALL_TARGETS': 1e-5, 'SCORED_ONLY': 3e-6}
MAX_LR = {'ALL_TARGETS': 1e-2, 'SCORED_ONLY': 3e-3}
DIV_FACTOR = {'ALL_TARGETS': 1e3, 'SCORED_ONLY': 1e2}
PCT_START = 0.1<choose_model_class>
pd.plotting.register_matplotlib_converters()
pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
max_width = 12
warnings.filterwarnings('ignore')
 print('all libraries imported' )
model = Model(num_features, num_all_targets)
<define_search_model>
path = '.. /input/competitive-data-science-predict-future-sales/'
f_categories = pd.read_csv(path + 'item_categories.csv')
 f_items = pd.read_csv(path + 'items.csv')
 f_transactions = pd.read_csv(path + 'sales_train.csv')
 f_sample = pd.read_csv(path + 'sample_submission.csv')
 f_shops = pd.read_csv(path + 'shops.csv')
 f_test = pd.read_csv(path + 'test.csv' )
def make_cv_folds(train, SEEDS, NFOLDS, DRUG_THRESH):
vc = train.drug_id.value_counts()
vc1 = vc.loc[vc <= DRUG_THRESH].index.sort_values()
vc2 = vc.loc[vc > DRUG_THRESH].index.sort_values()
for seed_id in range(SEEDS):
kfold_col = 'kfold_{}'.format(seed_id)
 dct1 = {}
dct2 = {}
skf = MultilabelStratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=seed_id)
 tmp = train.groupby('drug_id')[target_cols].mean().loc[vc1]
for fold,(idxT, idxV)in enumerate(skf.split(tmp, tmp[target_cols])) :
dd = {k: fold for k in tmp.index[idxV].values}
dct1.update(dd)
 skf = MultilabelStratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=seed_id)
 tmp = train.loc[train.drug_id.isin(vc2)].reset_index(drop=True)
 for fold,(idxT, idxV)in enumerate(skf.split(tmp, tmp[target_cols])) :
dd = {k: fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 train[kfold_col] = train.drug_id.map(dct1)
 train.loc[train[kfold_col].isna() , kfold_col] = train.loc[train[kfold_col].isna() , 'sig_id'].map(dct2)
 train[kfold_col] = train[kfold_col].astype('int8')
 return train
SEEDS = 7
NFOLDS = 7
DRUG_THRESH = 18
train = make_cv_folds(train, SEEDS, NFOLDS, DRUG_THRESH)
 train.head()<define_variables>
f_transactions.date = pd.to_datetime(f_transactions.date, format = '%d.%m.%Y')
 f_sales = f_transactions\ 
.join(f_items.set_index('item_id'), on='item_id')\ 
.join(f_categories.set_index('item_category_id'), on='item_category_id')\ 
.join(f_shops.set_index('shop_id'), on='shop_id')
 f_sales.head(2 ).T
model_path = '.. /input/pytorch-transfer-learning-with-k-folds-by-drug-ids/'<split>
f_test\ 
.join(f_sample.set_index('ID'), on='ID' ).head()
def run_inference(fold_id, seed_id):
test_ = process_data(test)
 model = Model(num_features, num_targets)
 model.load_state_dict(torch.load(f"{model_path}/SCORED_ONLY_SEED_{seed_id}_FOLD{fold_id}_.pth"))
model.to(DEVICE)
 x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 predictions = np.zeros(( len(test_), num_targets))
predictions = inference_fn(model, testloader, DEVICE)
 return predictions<define_variables>
test = f_test.copy()
SEED = [0, 1, 2, 3, 4, 5, 6]
fold =[0,1,2,3,4,5,6]
predictions = np.zeros(( len(test), len(target_cols)))
 time_begin = time()
for fold_id in fold:
for seed_id in SEED:
predictions_ = run_inference(fold_id, seed_id)
 predictions += predictions_ / len(SEED)
 time_diff = time() - time_begin
test[target_cols] = predictions<feature_engineering>
f_transactions.date = pd.to_datetime(f_transactions.date, format = '%d.%m.%Y')
 transactions = f_transactions.copy()
transactions = transactions.loc[~transactions.index.isin([1573253,1573252,1618930,1571406,2909818,1829149,1088966])]
transactions['year'] = transactions.date.dt.year
transactions['month'] = transactions.date.dt.month
transactions['day'] = transactions.date.dt.day
transactions['day_of_year'] = transactions.date.dt.dayofyear
transactions['week_day'] = transactions.date.dt.weekday
transactions['activity_cnt'] = transactions.item_cnt_day.abs()
transactions['activity_rev'] = transactions.activity_cnt*transactions.item_price
transactions['revenue'] = transactions.item_cnt_day*transactions.item_price
test[target_cols]=test[target_cols]/7 
<merge>
items = f_items.copy()
items.at[20949,'item_category_id'] = 84
items.at[5441,'item_category_id'] = 3
items = items\ 
.join(transactions.groupby('item_id' ).agg(
 sale_start=('date','min'),
sale_end=('date','max'),
price_mean=('item_price','mean'),
item_total_revenue=('revenue','sum'), 
))
sub2 = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0 )<install_modules>
categories = f_categories.copy()
categories = categories.append({'item_category_name': 'Фирменный пакет','item_category_id': 84,}, ignore_index=True)
 categories['cat_global'] = np.select(
 [categories.item_category_id.isin(range(0,8)) ,
categories.item_category_id.isin([8,80]),
categories.item_category_id==9,
categories.item_category_id.isin(range(10,18)) ,
categories.item_category_id.isin(range(18,32)) ,
categories.item_category_id.isin([32,33,34,35,36,37,79]),
categories.item_category_id.isin(range(37,42)) ,
categories.item_category_id.isin(range(42,55)) ,
categories.item_category_id.isin(range(55,61)) ,
categories.item_category_id.isin(range(61,73)) ,
categories.item_category_id.isin(range(73,79)) ,
categories.item_category_id.isin([81,82]),
categories.item_category_id==83,
categories.item_category_id==84],
['accessories','tickets','delivery','consoles','games',
'payment_cards','movies','books','music','gifts','programs',
'discs','batteries','plastic_bags'])
 categories['cat_platform'] = np.select(
 [categories.item_category_name.str.contains('PS2'),
categories.item_category_name.str.contains('PS3'),
categories.item_category_name.str.contains('PS4'),
categories.item_category_name.str.contains('PSP'),
categories.item_category_name.str.contains('PSVita'),
categories.item_category_name.str.contains('XBOX 360'),
categories.item_category_name.str.contains('XBOX ONE'),
categories.item_category_name.str.contains('PC'),
categories.item_category_name.str.contains('MAC'),
categories.item_category_name.str.contains('Android')],
['PS2','PS3','PS4','PSP','PSVita','XBOX_360','XBOX_ONE','PC','MAC','Android'],
default='other')
 categories['cat_digital'] = categories.item_category_name\ 
.str.contains('Цифра')*1
categories['cat_in_test'] = categories.item_category_id\ 
.isin(test.set_index('item_id' ).join(items.item_category_id ).item_category_id)*1
categories['cat_to_drop'] = categories.item_category_id\ 
.isin([ 1,8,10,13,17,18,39,46,48,50,51,52,53,66,68,80,81,82])*1
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet<set_options>
shops = f_shops.copy()
shops = shops.loc[~shops.shop_id.isin([0,1,11])]
shops['shop_in_test'] = shops.shop_id.isin(test.shop_id)*1
shops['shop_to_drop'] = shops.shop_id.isin([8,9,20,23,32,33,40])*1
split = shops['shop_name'].\
str.replace('Н.Новгород','ННовгород' ).\
str.replace('Сергиев Посад','СергиевПосад' ).\
str.replace('\W+',' ' ).\
str.strip().\
str.split(' ', 1 ).\
str
shops['shop_city'] = split[0]
shops['shop_name_part'] = split[1]
sold_items_cnt_per_work_day = transactions.groupby(['shop_id'] ).item_cnt_day.sum() /transactions.groupby(['shop_id'] ).date.nunique()
shops = shops.join(( sold_items_cnt_per_work_day/sold_items_cnt_per_work_day.mean() ).to_frame('shop_size'))
warnings.filterwarnings("ignore")
 sys.path.append(".. /input/rank-gauss")
 os.environ["CUDA_LAUNCH_BLOCKING"] = '1'
c_ = Fore.CYAN
m_ = Fore.MAGENTA
r_ = Fore.RED
b_ = Fore.BLUE
y_ = Fore.YELLOW
g_ = Fore.GREEN 
<load_from_csv>
sales = transactions\ 
.join(items.set_index('item_id'), on='item_id')\ 
.join(categories.set_index('item_category_id'), on='item_category_id')\ 
.join(shops.set_index('shop_id'), on='shop_id')\ 
.loc[lambda x:(x.cat_to_drop==0)&(x.shop_to_drop==0)]
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 df = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<create_dataframe>
_ = sales.groupby('date' ).shop_id.nunique()
sales = sales\ 
.set_index('date')\ 
.join(_.to_frame('shops_opened')) \ 
.reset_index() \ 
.assign(shop_compensation = lambda x:(x.shop_size*x.shops_opened)/(_.mean())) \ 
.assign(item_cnt_day_sw = lambda x: x.item_cnt_day/x.shop_compensation )
train_features2=train_features.copy()
test_features2=test_features.copy()<define_variables>
sales['days_from_sale_start'] =(sales.date-sales.sale_start ).dt.days
_ = sales.loc[(sales.sale_start>=datetime.datetime(2013, 6, 1))
&(~sales.cat_global.isin(['delivery']))
&(sales.days_from_sale_start<=60)]
_ = pd.pivot_table(_,
index=['cat_global','item_id'],
columns='days_from_sale_start',
values='item_cnt_day_sw',
aggfunc='sum' ).fillna(0 ).reset_index() \ 
.drop('item_id',axis=1)
 new_item_effect_cat =(_.groupby('cat_global' ).mean() ).div(( _.groupby('cat_global' ).mean() ).mean(axis=1),axis=0 ).T
new_item_effect_cat = new_item_effect_cat.div(new_item_effect_cat.tail(20 ).mean())
 new_item_effect_glob =(_.mean() /(_.mean().mean())).T
new_item_effect_glob = new_item_effect_glob.div(new_item_effect_glob.tail(20 ).mean())
 sales = sales\ 
.set_index(['cat_global','days_from_sale_start'])\ 
.join(new_item_effect_cat.unstack().to_frame('new_item_effect_cat'), how='left')\ 
.fillna(value={'new_item_effect_cat':1})\ 
.reset_index() \ 
.assign(item_cnt_day_sw=lambda x: x.item_cnt_day_sw/x.new_item_effect_cat )
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<feature_engineering>
sales['item_cnt_day_deseasoned'] = sales.item_cnt_day\ 
.div(sales.week_day_effect_cat)\ 
.div(sales.seasonality_annual_cat )
qt = QuantileTransformer(n_quantiles=100,random_state=42,output_distribution='normal')
 train_features[GENES+CELLS] = qt.fit_transform(train_features[GENES+CELLS])
 test_features[GENES+CELLS] = qt.transform(test_features[GENES+CELLS] )<define_variables>
sales = sales.loc[(sales.shop_in_test==1)]
n_comp_GENES=80
n_comp_CELLS=10<concatenate>
del f_sales,transactions,f_transactions
g_pca = PCA(n_components=n_comp_GENES, random_state=42)
 train_features = pd.concat(( train_features, pd.DataFrame(g_pca.fit_transform(train_features[GENES]), columns=[f'pca_G-{i}' for i in range(n_comp_GENES)])) , axis=1)
 test_features = pd.concat(( test_features, pd.DataFrame(g_pca.transform(test_features[GENES]), columns=[f'pca_G-{i}' for i in range(n_comp_GENES)])) , axis=1)
 c_pca = PCA(n_components=n_comp_CELLS, random_state=42)
 train_features = pd.concat(( train_features, pd.DataFrame(c_pca.fit_transform(train_features[CELLS]), columns=[f'pca_C-{i}' for i in range(n_comp_CELLS)])) , axis=1)
 test_features = pd.concat(( test_features, pd.DataFrame(c_pca.transform(test_features[CELLS]), columns=[f'pca_C-{i}' for i in range(n_comp_CELLS)])) , axis=1 )<concatenate>
def get_features_normal(grid,L):
date_length_max =(np.datetime64(L[-1][0],'D')-np.datetime64('2013-01-01','D'))
X=[0]*len(L)
 y=[0]*len(L)
 for block in range(len(L)) :
date_start = L[block][0]
col_drop = ['shop_in_test',
'item_category_name',
'cat_to_drop',
'shop_name',
'shop_name_part',
'cat_in_test',
'shop_to_drop'
]
data = sales.loc[(sales.date<date_start)&(sales.date>=date_start-date_length_max),
~sales.columns.isin(col_drop)]
X_temp = grid.loc[grid.date_block==block]\ 
.join(items.loc[:,items.columns.isin(['item_id','item_category_id'])].set_index('item_id'), on='item_id')\ 
.join(categories.set_index('item_category_id'), on='item_category_id')\ 
.join(shops.set_index('shop_id'), on='shop_id')\ 
.loc[:,lambda x : ~x.columns.isin(col_drop)]
lt = len(X_temp.columns)
 X_temp = X_temp\ 
.join(data.groupby('item_id')\ 
.agg(days_first_item_sold_glob=('date','min'),
days_last_item_sold_glob=('date','max')) ,
on='item_id')\ 
.join(data.groupby(['item_id','shop_id'])\ 
.agg(days_first_item_sold_shop=('date','min'),
days_last_item_sold_shop=('date','max')) ,
on=['item_id','shop_id'])\
X_temp.iloc[:,lt:] =(( date_start-X_temp.iloc[:,lt:])/np.timedelta64(1, 'D')).fillna(-1)
 X_temp = X_temp\ 
.join(data.groupby('item_id')\ 
.agg(item_price_mean=('item_price','mean')) ,
on='item_id')\ 
.join(data.groupby('item_category_id')\ 
.agg(cat_price_mean=('item_price','mean')) ,
on='item_category_id')\
X_temp = X_temp.assign(new_item = lambda x: x.item_price_mean.isna() *1)
 X_temp.loc[X_temp.cat_price_mean.isna() ,['cat_price_mean']] = data.item_price.mean()
X_temp.loc[X_temp.item_price_mean.isna() ,['item_price_mean']] = X_temp['cat_price_mean']
lt = len(X_temp.columns)
 for i in ['item_category_id']:
X_temp = X_temp.join(sales.groupby(['shop_id',i] ).item_cnt_day.sum().to_frame('s1')\ 
.join(sales.groupby('shop_id' ).item_cnt_day.sum().to_frame('s2')) \ 
.join(sales.groupby(i ).item_cnt_day.sum().to_frame('g1')) \ 
.assign(g2=sales.item_cnt_day.sum())\ 
.assign(**{i+'_shop_to_cat_glob': lambda x:(x.s1/x.s2)/(x.g1/x.g2)})\ 
.drop(['s1','s2','g1','g2'],axis=1),
on=['shop_id',i])
 X_temp.iloc[:,-1].fillna(0,inplace=True)
 X_temp = X_temp.join(sales.loc[sales.date.isin(L[block])].groupby(['item_id','shop_id'])\ 
.agg(target=('item_cnt_day','sum')) ,
on=['item_id','shop_id'])
 j0 = 0
for j in [30,120]:
ll = len(X_temp.columns)
 dd = data.loc[(data.date>=date_start-np.timedelta64(j, 'D')) & 
(data.date<date_start-np.timedelta64(j0, 'D')) ]
X_temp = X_temp\ 
.join(dd.groupby('item_id')\ 
.agg(item_glob_sales=('item_cnt_day','sum')) ,on='item_id')\ 
.join(dd.groupby(['shop_id','item_id'])\ 
.agg(item_shop_sales=('item_cnt_day','sum')) ,on=['shop_id','item_id'])\ 
.join(dd.groupby('item_category_id')\ 
.agg(cat_glob_sales=('item_cnt_day','sum')) ,on='item_category_id')\ 
.join(dd.groupby(['shop_id','item_category_id'])\ 
.agg(cat_shop_sales=('item_cnt_day','sum')) ,on=['shop_id','item_category_id'])\ 
.join(dd.groupby('item_id')\ 
.agg(item_glob_days=('date','nunique')) ,on='item_id')\ 
.join(dd.groupby(['item_id','shop_id'])\ 
.agg(item_shop_days=('date','nunique')) ,on=['item_id','shop_id'])\
X_temp.rename(columns=dict(zip(X_temp.iloc[:,ll:].columns,
X_temp.iloc[:,ll:].columns+'_'+str(j0)+'-'+str(j))),
inplace=True)
 j0 = j
X_temp.iloc[:,lt:] = X_temp.iloc[:,lt:].fillna(0)
 X_temp.loc[:,['shop_id','item_category_id']] = X_temp.loc[:,['shop_id','item_category_id']].astype('str')
 X[block] = X_temp.loc[:,~X_temp.columns.isin(['target','cat_digital','shop_city'])]
y[block] = X_temp.target.fillna(0 ).clip(0,20)
 X = pd.concat(X)
 print('features - done')
 return X,y
c_n = [f for f in list(train_features.columns)if f not in ['sig_id', 'cp_type', 'cp_time', 'cp_dose']]
mask =(train_features[c_n].var() >= 0.85 ).values
tmp = train_features[c_n].loc[:, mask]
train_features = pd.concat([train_features[['sig_id', 'cp_type', 'cp_time', 'cp_dose']], tmp], axis=1)
 tmp = test_features[c_n].loc[:, mask]
test_features = pd.concat([test_features[['sig_id', 'cp_type', 'cp_time', 'cp_dose']], tmp], axis=1 )<merge>
def data_for_cb(X,y,tb,bn,cat_features):
tune_set_cb = Pool(data=X.loc[X.date_block.isin(range(tb+1,bn)) ],
label=pd.concat(y[tb+1:]),
cat_features=cat_features)
 eval_set_cb = Pool(data=X.loc[X.date_block.isin([1])],
label=y[1],
cat_features=cat_features)
 train_set_cb = Pool(data=X.loc[X.date_block.isin(range(1,bn-tb)) ],
label=pd.concat(y[1:-tb]),
cat_features=cat_features)
 test_set_cb = Pool(data=X.loc[X.date_block.isin([0])],
label=y[0],
cat_features=cat_features)
 print('cb data - done')
 return tune_set_cb, eval_set_cb, train_set_cb, test_set_cb
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]<drop_column>
def encode_X(X,cat_features):
X = pd.concat([X.loc[:,~X.columns.isin(cat_features)],
pd.get_dummies(X.loc[:,cat_features])], axis=1)
 _ = X.date_block.values
X = pd.DataFrame(data=StandardScaler().fit_transform(X), columns=X.columns)
 X.iloc[:,23:] = X.iloc[:,23:]*0.1
X.date_block = _
return X
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<drop_column>
def data_for_rf_knn(X,y,tb,bn):
X_tune_enc = X.loc[X.date_block.isin(range(tb+1,bn)) ].astype(np.float32)
 y_tune_enc = pd.concat(y[tb+1:] ).astype(np.float32)
 X_eval_enc = X.loc[X.date_block.isin([1])].astype(np.float32)
 y_eval_enc = y[1].astype(np.float32)
 X_train_enc = X.loc[X.date_block.isin(range(1,bn-tb)) ].astype(np.float32)
 y_train_enc = pd.concat(y[1:-tb] ).astype(np.float32)
 X_test_enc = X.loc[X.date_block.isin([0])].astype(np.float32)
 y_test_enc = y[0].astype(np.float32)
 print('rf and knn data - done')
 return X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist() 
<filter>
cb = CatBoostRegressor(task_type='GPU',
iterations = 1500,
boosting_type = 'Ordered',
depth = 4)
 rf = RandomForestRegressor(min_samples_leaf=15,
n_jobs=-1,
random_state=0,
n_estimators=60)
 knn = KNeighborsRegressor(n_neighbors=30)
 lr = LinearRegression(n_jobs=-1 )
target=target[target_cols] 
<categorify>
def run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc):
eval_df = pd.DataFrame(np.zeros(( len(y_eval_enc),5)) ,columns=['rf','cb','knn','lr','y'])
 test_df = pd.DataFrame(np.zeros(( len(y_test_enc),5)) ,columns=['rf','cb','knn','lr','y'])
 print(' ')
 print('tuning...')
 rf.fit(X_tune_enc,y_tune_enc.values)
 print('rf - done')
 cb.fit(tune_set_cb, silent=True)
 print('cb - done')
 knn.fit(cudf.DataFrame.from_pandas(X_tune_enc), y_tune_enc.values)
 print('knn - done')
 eval_df.cb = cb.predict(eval_set_cb ).clip(0,20)
 eval_df.rf = rf.predict(X_eval_enc ).clip(0,20)
 eval_df.knn = knn.predict(cudf.DataFrame.from_pandas(X_eval_enc)).to_pandas().clip(0,20)
 eval_df.y = y_eval_enc.values
print('rf','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.rf,squared=False)))
 print('cb','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.cb,squared=False)))
 print('knn','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.knn,squared=False)))
 lr.fit(eval_df.iloc[:,:-2],eval_df.y)
 print('lr - done')
 eval_df.lr = lr.predict(eval_df.iloc[:,:-2] ).clip(0,20)
 print('lr','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.lr,squared=False)))
 print('model weights',
'rf',
'{:.1%}'.format(lr.coef_[0]),
'cb',
'{:.1%}'.format(lr.coef_[1]),
'knn',
'{:.1%}'.format(lr.coef_[2]))
print(' ')
 print('training...')
 rf.fit(X_train_enc,y_train_enc.values)
 print('rf - done')
 cb.fit(train_set_cb, silent=True)
 print('cb - done')
 knn.fit(cudf.DataFrame.from_pandas(X_train_enc), y_train_enc.values)
 print('knn - done')
 test_df.rf = rf.predict(X_test_enc ).clip(0,20)
 test_df.cb = cb.predict(test_set_cb ).clip(0,20)
 test_df.knn = knn.predict(cudf.DataFrame.from_pandas(X_test_enc)).to_pandas().clip(0,20)
 test_df.lr = lr.predict(test_df.iloc[:,:-2] ).clip(0,20)
 print('all done')
 return test_df.lr
train = pd.get_dummies(train, columns=['cp_time','cp_dose'])
 test_ = pd.get_dummies(test, columns=['cp_time','cp_dose'] )<define_variables>
print('start monthly models...')
 print(' ')
 bn = 4
tb = 1
cat_features = ['cat_global','cat_platform','shop_id','item_category_id']
L = get_monthly_list(bn)
 grid = get_grid(sales,L)
 X,y = get_features_normal(grid,L)
 tune_set_cb, eval_set_cb, train_set_cb, test_set_cb = data_for_cb(X,y,tb,bn,cat_features)
 X = encode_X(X,cat_features)
 X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc = data_for_rf_knn(X,y,tb,bn)
 del X,y
pred_monthly = run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc )
feature_cols = [c for c in train.columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['sig_id']]<drop_column>
print(' ')
 print('start 30d models...')
 print(' ')
 bn = 7
tb = 2
cat_features = ['cat_global','cat_platform','shop_id','item_category_id']
L = get_30d_list(bn)
 grid = get_grid(sales,L)
 X,y = get_features_normal(grid,L)
 tune_set_cb, eval_set_cb, train_set_cb, test_set_cb = data_for_cb(X,y,tb,bn,cat_features)
 X = encode_X(X,cat_features)
 X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc = data_for_rf_knn(X,y,tb,bn)
 del X,y
pred_30d = run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc )
train = train[feature_cols]
test = test_[feature_cols]<prepare_x_and_y>
f_sample.item_cnt_month =(pred_monthly*0.5+pred_30d*0.5 ).clip(0,20)
 f_sample.to_csv(r'submission.csv',index=False )