Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<categorify>
%matplotlib inline
x_res=pipeline.fit_transform(x_res)
 test=pipeline.transform(test )<compute_train_metric>
shops = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv')
 items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 catgs = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 sales = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 testd = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv')
 sampl = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sample_submission.csv' )
model = RandomForestClassifier(n_estimators=500, random_state=10, n_jobs = -1)
 cv_score = cross_val_score(model, x_res, y_res, cv = 10, scoring = scorer)
 cv2_score = cross_val_score(model, x_res, y_res, cv = 10, scoring = scorer2 )<choose_model_class>
print(sales.isna().sum() , '
')
 print(testd.isna().sum() )
rf_model = RandomForestClassifier(n_estimators=500, random_state=10, n_jobs = -1 )<train_model>
sales = sales[sales.item_price <= 100000]
sales = sales[sales.item_cnt_day <= 1000]
median = sales[(sales.shop_id == 32)&(sales.item_id == 2973)&(sales.date_block_num == 4)&(sales.item_price > 0)].item_price.median()
sales.loc[sales.item_price < 0, 'item_price'] = median
rf_trained=rf_model.fit(x_res,y_res)
 predicted=rf_model.predict(test )<create_dataframe>
sales.loc[sales.shop_id == 0, 'shop_id'] = 57
testd.loc[testd.shop_id == 0, 'shop_id'] = 57
sales.loc[sales.shop_id == 1, 'shop_id'] = 58
testd.loc[testd.shop_id == 1, 'shop_id'] = 58
sales.loc[sales.shop_id == 10, 'shop_id'] = 11
testd.loc[testd.shop_id == 10, 'shop_id'] = 11
sales.loc[sales.shop_id == 39, 'shop_id'] = 40
testd.loc[testd.shop_id == 39, 'shop_id'] = 40
subm=pd.DataFrame()
subm["Id"]=test_labels
subm["Target"]=predicted<save_to_csv>
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['shop_category'] = shops['shop_name'].str.split(' ' ).map(lambda x:x[1] ).astype(str)
 categories = ['Орджоникидзе,', 'ТЦ', 'ТРК', 'ТРЦ','ул.', 'Магазин', 'ТК', 'склад']
shops.shop_category = shops.shop_category.apply(lambda x: x if(x in categories)else 'etc')
 shops.shop_category.unique()
subm.to_csv("results.csv",index=False )<import_modules>
shops.groupby(['shop_category'] ).sum()
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV as grid_search<choose_model_class>
category = ['ТЦ', 'ТРК', 'ТРЦ', 'ТК']
shops.shop_category = shops.shop_category.apply(lambda x: x if(x in category)else 'etc')
 print('Category Distribution', shops.groupby(['shop_category'] ).sum())
 shops['shop_category_code'] = LabelEncoder().fit_transform(shops['shop_category'] )
xg_model=XGBClassifier(gamma=0.1,max_depth=5,objective='multi:softmax',n_estimators=1200 )<define_search_space>
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
shops['city_code'] = LabelEncoder().fit_transform(shops['city'])
 shops = shops[['shop_id','city_code', 'shop_category_code']]
shops.head()
param_grid={"max_depth":[3,4,5,6],"gamma":[0.001,0.01,0.1,0.2,0,3],"n_estimators":[100,150,200,250,300,350,400,450,500,550,600,650], "objective":['multi:softmax']}<train_model>
print(len(catgs.item_category_name.unique()))
catgs.item_category_name.unique()
xg_model.fit(x_res,y_res)
<create_dataframe>
catgs['type'] = catgs.item_category_name.apply(lambda x: x.split(' ')[0] ).astype(str)
 catgs.loc[(catgs.type == 'Игровые')|(catgs.type == 'Аксессуары'), 'category'] = 'Игры'
catgs.loc[catgs.type == 'PC', 'category'] = 'Музыка'
category = ['Игры', 'Карты', 'Кино', 'Книги','Музыка', 'Подарки', 'Программы', 'Служебные', 'Чистые', 'Аксессуары']
catgs['type'] = catgs.type.apply(lambda x: x if(x in category)else 'etc')
 print(catgs.groupby(['type'] ).sum())
 catgs['type_code'] = LabelEncoder().fit_transform(catgs['type'])
 catgs['split'] = catgs.item_category_name.apply(lambda x: x.split('-'))
catgs['subtype'] = catgs['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 catgs['subtype_code'] = LabelEncoder().fit_transform(catgs['subtype'])
 catgs = catgs[['item_category_id','type_code', 'subtype_code']]
catgs.head()
subm=pd.DataFrame()
subm["Id"]=test_labels
subm["Target"]=xg_model.predict(test )<save_to_csv>
sales['date'] = pd.to_datetime(sales['date'], format='%d.%m.%Y')
 sales['month'] = sales['date'].dt.month
sales['year'] = sales['date'].dt.year
sales = sales.drop(columns=['date'])
 to_append = testd[['shop_id', 'item_id']].copy()
to_append['date_block_num'] = sales['date_block_num'].max() + 1
to_append['year'] = 2015
to_append['month'] = 11
to_append['item_cnt_day'] = 0
to_append['item_price'] = 0
sales = pd.concat([sales, to_append], ignore_index=True, sort=False)
 sales.head()
subm.to_csv("results.csv",index=False )<set_options>
period = sales[['date_block_num', 'year', 'month']].drop_duplicates().reset_index(drop=True)
 period['days'] = period.apply(lambda r: monthrange(r.year, r.month)[1], axis=1)
 sales = sales.drop(columns=['month', 'year'])
 period.head()
%matplotlib inline
warnings.filterwarnings("ignore" )<categorify>
data = pd.merge(grid, shops, on='shop_id')
 data = pd.merge(data, items, on='item_id')
 data = pd.merge(data, catgs, on='item_category_id')
 data = pd.merge(data, period, on='date_block_num')
 data = data[['date_block_num', 'year', 'month', 'days', 'city_code', 'shop_category_code', 'shop_id', 'item_category_id', 'type_code', 'subtype_code', 'item_id']]
for c in ['date_block_num', 'month', 'days', 'city_code', 'shop_category_code', 'shop_id', 'item_category_id', 'type_code', 'subtype_code']:
data[c] = data[c].astype(np.int8)
 data['item_id'] = data['item_id'].astype(np.int16)
 data['year'] = data['year'].astype(np.int16)
 del grid, shops, items, catgs, to_append
data.head()
def encode_data(df):
df['idhogar'] = LabelEncoder().fit_transform(df['idhogar'])
 def feature_importance(forest, X_train):
ranked_list = []
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
print("Feature ranking:")
 for f in range(X_train.shape[1]):
print("%d.feature %d(%f)" %(f + 1, indices[f], importances[indices[f]])+ " - " + X_train.columns[indices[f]])
 ranked_list.append(X_train.columns[indices[f]])
 return ranked_list<feature_engineering>
aux = sales\ 
.groupby(['date_block_num', 'shop_id', 'item_id'], as_index=False)\ 
.agg({'item_cnt_day' : 'sum', 'item_price' : 'mean'})\ 
.rename(columns= {'item_cnt_day' : 'item_cnt_month', 'item_price' : 'item_price_month'})
 aux['item_cnt_month'] = aux['item_cnt_month'].astype(np.float16)
 aux['item_price_month'] = aux['item_price_month'].astype(np.float16)
 month_summary = pd.merge(data, aux, how='left', on=['date_block_num', 'shop_id', 'item_id'])\ 
.fillna(0.0 ).sort_values(by=['shop_id', 'item_id', 'date_block_num'])
 del data, aux
month_summary.head()
def do_features(df):
feats_div = [('children_fraction', 'r4t1', 'r4t3'), 
('working_man_fraction', 'r4h2', 'r4t3'), 
('all_man_fraction', 'r4h3', 'r4t3'), 
('human_density', 'tamviv', 'rooms'), 
('human_bed_density', 'tamviv', 'bedrooms'), 
('rent_per_person', 'v2a1', 'r4t3'), 
('rent_per_room', 'v2a1', 'rooms'), 
('mobile_density', 'qmobilephone', 'r4t3'), 
('tablet_density', 'v18q1', 'r4t3'), 
('mobile_adult_density', 'qmobilephone', 'r4t2'), 
('tablet_adult_density', 'v18q1', 'r4t2'),
]
feats_sub = [('people_not_living', 'tamhog', 'tamviv'), 
('people_weird_stat', 'tamhog', 'r4t3')]
for f_new, f1, f2 in feats_div:
df['fe_' + f_new] =(df[f1] / df[f2] ).astype(np.float32)
 for f_new, f1, f2 in feats_sub:
df['fe_' + f_new] =(df[f1] - df[f2] ).astype(np.float32)
 aggs_num = {'age': ['min', 'max', 'mean'],
'escolari': ['min', 'max', 'mean']
}
aggs_cat = {'dis': ['mean']}
for s_ in ['estadocivil', 'parentesco', 'instlevel']:
for f_ in [f_ for f_ in df.columns if f_.startswith(s_)]:
aggs_cat[f_] = ['mean', 'count']
for name_, df_ in [('18', df.query('age >= 18')) ]:
df_agg = df_.groupby('idhogar' ).agg({**aggs_num, **aggs_cat} ).astype(np.float32)
 df_agg.columns = pd.Index(['agg' + name_ + '_' + e[0] + "_" + e[1].upper() for e in df_agg.columns.tolist() ])
 df = df.join(df_agg, how='left', on='idhogar')
 del df_agg
df.drop(['Id'], axis=1, inplace=True)
 return df<categorify>
month_summary['item_cnt_month'] = month_summary['item_cnt_month'].clip(0,20 )
def convert_OHE2LE(df):
tmp_df = df.copy(deep=True)
 for s_ in ['pared', 'piso', 'techo', 'abastagua', 'sanitario', 'energcocinar', 'elimbasu',
'epared', 'etecho', 'eviv', 'estadocivil', 'parentesco',
'instlevel', 'lugar', 'tipovivi',
'manual_elec']:
if 'manual_' not in s_:
cols_s_ = [f_ for f_ in df.columns if f_.startswith(s_)]
elif 'elec' in s_:
cols_s_ = ['public', 'planpri', 'noelec', 'coopele']
sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print('The OHE in {} is incomplete.A new column will be added before label encoding' 
.format(s_))
col_dummy = s_+'_dummy'
tmp_df[col_dummy] =(tmp_df[cols_s_].sum(axis=1)== 0 ).astype(np.int8)
 cols_s_.append(col_dummy)
 sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print("The category completion did not work")
 tmp_cat = tmp_df[cols_s_].idxmax(axis=1)
 tmp_df[s_ + '_LE'] = LabelEncoder().fit_transform(tmp_cat ).astype(np.int16)
 if 'parentesco1' in cols_s_:
cols_s_.remove('parentesco1')
 tmp_df.drop(cols_s_, axis=1, inplace=True)
 return tmp_df<load_from_csv>
def agg_by(month_summary, group_cols, new_col, target_col = 'item_cnt_month', agg_func = 'mean'):
aux = month_summary\ 
.groupby(group_cols, as_index=False)\ 
.agg({target_col : agg_func})\ 
.rename(columns= {target_col : new_col})
 aux[new_col] = aux[new_col].astype(np.float16)
 return pd.merge(month_summary, aux, how='left', on=group_cols)
 def lag_feature(df, col, lags=[1,2,3,6,12]):
tmp = df[['date_block_num','shop_id','item_id', col]]
for i in lags:
shifted = tmp.copy()
cols = ['date_block_num','shop_id','item_id', '{}_lag_{}'.format(col, i)]
shifted.columns = cols
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left' ).fillna(value={(cols[-1]): 0.0})
 return df
def agg_by_and_lag(month_summary, group_cols, new_col, lags=[1,2,3,6,12], target_col = 'item_cnt_month', agg_func = 'mean'):
tmp = agg_by(month_summary, group_cols, new_col, target_col, agg_func)
 tmp = lag_feature(tmp, new_col, lags)
 return tmp.drop(columns=[new_col] )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 test_ids = test.Id<categorify>
month_summary = agg_by_and_lag(month_summary, ['date_block_num'], 'date_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_id'], 'date_item_avg_item_cnt', [1,2,3,6,12])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code'], 'date_city_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id'], 'date_shop_avg_item_cnt', [1,2,3,6,12])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_category_id'], 'date_cat_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'type_code'], 'date_type_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'subtype_code'], 'date_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code'], 'date_shop_category_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'item_category_id'], 'date_shop_cat_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'type_code'], 'date_shop_type_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'subtype_code'], 'date_shop_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code', 'subtype_code'], 'date_shop_category_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code', 'item_id'], 'date_item_city_avg_item_cnt', [1] )
def process_df(df_):
encode_data(df_)
 return do_features(df_)
 train = process_df(train)
 test = process_df(test )<feature_engineering>
month_summary = agg_by_and_lag(month_summary, ['date_block_num'], 'date_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_id'], 'date_item_avg_item_price', [1,2,3,6,12], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code'], 'date_city_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id'], 'date_shop_avg_item_price', [1,2,3,6,12], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_category_id'], 'date_cat_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'type_code'], 'date_type_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'subtype_code'], 'date_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code'], 'date_shop_category_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'item_category_id'], 'date_shop_cat_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'type_code'], 'date_shop_type_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'subtype_code'], 'date_shop_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code', 'subtype_code'], 'date_shop_category_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code', 'item_id'], 'date_item_city_avg_item_price', [1], 'item_price_month' )
train['dependency'] = np.sqrt(train['SQBdependency'])
 test['dependency'] = np.sqrt(test['SQBdependency'])
 train.loc[train['edjefa'] == "no", "edjefa"] = 0
train.loc[train['edjefe'] == "no", "edjefe"] = 0
test.loc[test['edjefa'] == "no", "edjefa"] = 0
test.loc[test['edjefe'] == "no", "edjefe"] = 0
train.loc[(train['edjefa'] == "yes")&(train['parentesco1'] == 1), "edjefa"] = train.loc[(train['edjefa'] == "yes")&(train['parentesco1'] == 1), "escolari"]
train.loc[(train['edjefe'] == "yes")&(train['parentesco1'] == 1), "edjefe"] = train.loc[(train['edjefe'] == "yes")&(train['parentesco1'] == 1), "escolari"]
test.loc[(test['edjefa'] == "yes")&(test['parentesco1'] == 1), "edjefa"] = test.loc[(test['edjefa'] == "yes")&(test['parentesco1'] == 1), "escolari"]
test.loc[(test['edjefe'] == "yes")&(test['parentesco1'] == 1), "edjefe"] = test.loc[(test['edjefe'] == "yes")&(test['parentesco1'] == 1), "escolari"]
train.loc[train['edjefa'] == "yes", "edjefa"] = 4
train.loc[train['edjefe'] == "yes", "edjefe"] = 4
test.loc[test['edjefa'] == "yes", "edjefa"] = 4
test.loc[test['edjefe'] == "yes", "edjefe"] = 4
train['edjefe'] = train['edjefe'].astype("int")
 train['edjefa'] = train['edjefa'].astype("int")
 test['edjefe'] = test['edjefe'].astype("int")
 test['edjefa'] = test['edjefa'].astype("int")
 train['edjef'] = np.max(train[['edjefa','edjefe']], axis=1)
 test['edjef'] = np.max(test[['edjefa','edjefe']], axis=1)
 train['v2a1']=train['v2a1'].fillna(0)
 test['v2a1']=test['v2a1'].fillna(0)
 test['v18q1']=test['v18q1'].fillna(0)
 train['v18q1']=train['v18q1'].fillna(0)
 train['rez_esc']=train['rez_esc'].fillna(0)
 test['rez_esc']=test['rez_esc'].fillna(0)
 train.loc[train.meaneduc.isnull() , "meaneduc"] = 0
train.loc[train.SQBmeaned.isnull() , "SQBmeaned"] = 0
test.loc[test.meaneduc.isnull() , "meaneduc"] = 0
test.loc[test.SQBmeaned.isnull() , "SQBmeaned"] = 0
train.loc[(train.v14a == 1)&(train.sanitario1 == 1)&(train.abastaguano == 0), "v14a"] = 0
train.loc[(train.v14a == 1)&(train.sanitario1 == 1)&(train.abastaguano == 0), "sanitario1"] = 0
test.loc[(test.v14a == 1)&(test.sanitario1 == 1)&(test.abastaguano == 0), "v14a"] = 0
test.loc[(test.v14a == 1)&(test.sanitario1 == 1)&(test.abastaguano == 0), "sanitario1"] = 0<concatenate>
month_summary['item_shop_first_sale'] = month_summary['date_block_num'] - month_summary.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 month_summary['item_first_sale'] = month_summary['date_block_num'] - month_summary.groupby('item_id')['date_block_num'].transform('min' )
def train_test_apply_func(train_, test_, func_):
test_['Target'] = 0
xx = pd.concat([train_, test_])
 xx_func = func_(xx)
 train_ = xx_func.iloc[:train_.shape[0], :]
test_ = xx_func.iloc[train_.shape[0]:, :].drop('Target', axis=1)
 del xx, xx_func
return train_, test_<categorify>
month_summary = pd.read_pickle('month_summary.pkl' )
train, test = train_test_apply_func(train, test, convert_OHE2LE )<categorify>
def generate_subsample(month_summary, target='item_cnt_month'):
X_test = month_summary[month_summary['date_block_num'] == 34]
X_test = X_test.drop(columns=[target])
 X_val = month_summary[month_summary['date_block_num'] == 33]
y_val = X_val[target]
X_val = X_val.drop(columns=[target])
 X_train = month_summary[(month_summary['date_block_num'] >= 12)&(month_summary['date_block_num'] < 33)]
y_train = X_train[target]
X_train = X_train.drop(columns=[target])
 return X_train, y_train, X_val, y_val, X_test
cols_2_ohe = ['eviv_LE', 'etecho_LE', 'epared_LE', 'elimbasu_LE',
'energcocinar_LE', 'sanitario_LE', 'manual_elec_LE',
'pared_LE']
cols_nums = ['age', 'meaneduc', 'dependency',
'hogar_nin', 'hogar_adul', 'hogar_mayor', 'hogar_total',
'bedrooms', 'overcrowding']
def convert_geo2aggs(df_):
tmp_df = pd.concat([df_[(['lugar_LE', 'idhogar']+cols_nums)],
pd.get_dummies(df_[cols_2_ohe],
columns=cols_2_ohe)],axis=1)
 geo_agg = tmp_df.groupby(['lugar_LE','idhogar'] ).mean().groupby('lugar_LE' ).mean().astype(np.float32)
 geo_agg.columns = pd.Index(['geo_' + e for e in geo_agg.columns.tolist() ])
 del tmp_df
return df_.join(geo_agg, how='left', on='lugar_LE')
 train, test = train_test_apply_func(train, test, convert_geo2aggs )<categorify>
X_train, y_train, X_val, y_val, X_test = generate_subsample(month_summary.drop(columns=['item_price_month']), 'item_cnt_month')
 del month_summary
train['num_over_18'] = 0
train['num_over_18'] = train[train.age >= 18].groupby('idhogar' ).transform("count")
 train['num_over_18'] = train.groupby("idhogar")["num_over_18"].transform("max")
 train['num_over_18'] = train['num_over_18'].fillna(0)
 test['num_over_18'] = 0
test['num_over_18'] = test[test.age >= 18].groupby('idhogar' ).transform("count")
 test['num_over_18'] = test.groupby("idhogar")["num_over_18"].transform("max")
 test['num_over_18'] = test['num_over_18'].fillna(0)
 def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['r4t3_to_tamhog'] = df['r4t3']/df['tamhog']
df['r4t3_to_rooms'] = df['r4t3']/df['rooms']
df['v2a1_to_r4t3'] = df['v2a1']/df['r4t3']
df['v2a1_to_r4t3'] = df['v2a1']/(df['r4t3'] - df['r4t1'])
 df['hhsize_to_rooms'] = df['hhsize']/df['rooms']
df['rent_to_hhsize'] = df['v2a1']/df['hhsize']
df['rent_to_over_18'] = df['v2a1']/df['num_over_18']
df.loc[df.num_over_18 == 0, "rent_to_over_18"] = df[df.num_over_18 == 0].v2a1
extract_features(train)
 extract_features(test )<drop_column>
def train_gbmodel(X_train, y_train, X_val, y_val):
RAND_SEED = 42
lgb_params = {'num_leaves': 2**8, 'max_depth': 19, 'max_bin': 107,
'bagging_freq': 1, 'bagging_fraction': 0.7135681370918421,
'feature_fraction': 0.49446461478601994, 'min_data_in_leaf': 2**8,
'learning_rate': 0.015980721586917768, 'num_threads': 2,
'min_sum_hessian_in_leaf': 6,
'random_state' : RAND_SEED,
'bagging_seed' : RAND_SEED,
'boost_from_average' : 'true',
'boost' : 'gbdt',
'metric' : 'rmse',
'verbose' : 1}
lgb_train = lgb.Dataset(X_train, label=y_train)
 lgb_val = lgb.Dataset(X_val, label=y_val)
 return lgb.train(lgb_params, lgb_train,
num_boost_round=300,
valid_sets=[lgb_train, lgb_val],
early_stopping_rounds=20 )
<split><EOS> 
y_pred = gbm_model.predict(X_test ).clip(0, 20)
 result = pd.merge(testd, X_test.assign(item_cnt_month=y_pred), how='left', on=['shop_id', 'item_id'])[['ID', 'item_cnt_month']]
result.to_csv('submission.csv', index=False )