Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<load_from_csv>
%matplotlib inline
pd.set_option('display.max_rows', 600)
 pd.set_option('display.max_columns', 50)
 !pip install bayesian-optimization
def downcast_dtypes(df):

 float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype == "int64"]
df[float_cols] = df[float_cols].astype(np.float32)
 df[int_cols] = df[int_cols].astype(np.int32)
 return df
def clip40(x):
if x>40:
return 40
elif x<0:
return 0
else:
return x
def clip20(x):
if x>20:
return 20
elif x<0:
return 0
else:
return x
def remove_from_list(master_list , to_remove):
for elm in to_remove:
if elm in master_list: master_list.remove(elm)
 return master_list
def over_underfit_plot(model, X, y):
gc.collect()
plt.figure()
plt.xlabel("Training examples")
 plt.ylabel("Score")
 train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=5, n_jobs=-1)
 train_scores_mean = np.mean(train_scores, axis=1)
 test_scores_mean = np.mean(test_scores, axis=1)
 del train_scores,test_scores
plt.grid()
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
 plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
 plt.legend(loc="best")
 plt.yticks(sorted(set(np.append(train_scores_mean, test_scores_mean))))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
multiprocessing.cpu_count()
submit = pd.read_csv('.. /input/imet-2019-fgvc6/sample_submission.csv')
 model.load_weights('.. /working/Resnet50_focal.h5')
 predicted = []<categorify>
DATA_FOLDER = '/kaggle/input/competitive-data-science-predict-future-sales/'
sales = pd.read_csv(os.path.join(DATA_FOLDER, 'sales_train.csv'))
items = pd.read_csv(os.path.join(DATA_FOLDER, 'items.csv'))
item_categories = pd.read_csv(os.path.join(DATA_FOLDER, 'item_categories.csv'))
test = pd.read_csv(os.path.join(DATA_FOLDER, 'test.csv'))
shops = pd.read_csv(os.path.join(DATA_FOLDER, 'shops.csv'))
submissions = pd.read_csv(os.path.join(DATA_FOLDER, 'sample_submission.csv'))
test_block = sales['date_block_num'].iloc[-1] + 1
test['date_block_num'] = test_block
BATCH = 512
fullValGen = data_generator.create_valid(
 train_dataset_info[valid_indexes], BATCH,(SIZE,SIZE,3))
n_val = round(train_dataset_info.shape[0]*0.15)//BATCH
print(n_val)
 lastFullValPred = np.empty(( 0, NUM_CLASSES))
lastFullValLabels = np.empty(( 0, NUM_CLASSES))
for i in tqdm(range(n_val+1)) :
im, lbl = next(fullValGen)
 scores = model.predict(im)
 lastFullValPred = np.append(lastFullValPred, scores, axis=0)
 lastFullValLabels = np.append(lastFullValLabels, lbl, axis=0)
 print(lastFullValPred.shape, lastFullValLabels.shape )<compute_test_metric>
sales['time'] = pd.to_datetime(sales['date'], format='%d.%m.%Y')
 sales['year'] = sales.time.dt.year
sales['month'] = sales.time.dt.month
sales['day'] = sales.time.dt.day
def my_f2(y_true, y_pred):
assert y_true.shape[0] == y_pred.shape[0]
tp = np.sum(( y_true == 1)&(y_pred == 1), axis=1)
 tn = np.sum(( y_true == 0)&(y_pred == 0), axis=1)
 fp = np.sum(( y_true == 0)&(y_pred == 1), axis=1)
 fn = np.sum(( y_true == 1)&(y_pred == 0), axis=1)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f2 =(1+beta_f2**2)*p*r /(p*beta_f2**2 + r + 1e-15)
 return np.mean(f2)
 def find_best_fixed_threshold(preds, targs, do_plot=True):
score = []
thrs = np.arange(0, 0.5, 0.01)
 for thr in tqdm(thrs):
score.append(my_f2(targs,(preds > thr ).astype(int)))
 score = np.array(score)
 pm = score.argmax()
best_thr, best_score = thrs[pm], score[pm].item()
print(f'thr={best_thr:.3f}', f'F2={best_score:.3f}')
 if do_plot:
plt.plot(thrs, score)
 plt.vlines(x=best_thr, ymin=score.min() , ymax=score.max())
 plt.text(best_thr+0.03, best_score-0.01, f'$F_{2}=${best_score:.3f}', fontsize=14);
plt.show()
return best_thr, best_score<compute_train_metric>
print('Number of unique train values in column "shop_id" of the dataframe : ',sales['shop_id'].nunique())
 print('Number of unique train values in column "item_id" of the dataframe : ',sales['item_id'].nunique())
 print('Number of unique test values in column "shop_id" of the dataframe : ',test['shop_id'].nunique())
 print('Number of unique test values in column "item_id" of the dataframe : ',test['item_id'].nunique())
best_thr, best_score = find_best_fixed_threshold(lastFullValPred, lastFullValLabels, do_plot=True )<import_modules>
item_categories['split'] = item_categories['item_category_name'].str.split('-')
 item_categories['type'] = item_categories['split'].map(lambda x: x[0].strip())
 item_categories['type_code'] = LabelEncoder().fit_transform(item_categories['type'])
 item_categories['subtype'] = item_categories['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 item_categories['subtype_code'] = LabelEncoder().fit_transform(item_categories['subtype'])
 item_categories = item_categories[['item_category_id','type_code', 'subtype_code' , 'item_category_name']]
fastai.__version__<define_variables>
zero_df = sales[sales['item_cnt_day']<0]
print('number of rows with negative item_cnt_day %f' %(zero_df['item_cnt_day'].count()))
zero_df = sales[sales['item_price']<0]
print('number of rows with negative item_price %f' %(zero_df['item_price'].count()))
path = Path('.. /input/imet-2019-fgvc6/' )<load_pretrained>
print(' setting -ve item_cnt_day to zero ')
 sales['item_cnt_day'] = sales['item_cnt_day'].apply(lambda x: x if x >=0 else 0)
 print(' removing -ve item_price items ')
 sales['item_price'] = sales['item_price'].apply(lambda x: x if x >=0 else 0 )
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/densenet201/densenet201.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'densenet201.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url<load_from_csv>
sales = sales[sales.item_price<100000]
sales = sales[sales.item_cnt_day<=1500]
train_df = pd.read_csv(path/'train.csv')
 train_df.head()<load_from_csv>
sales['revenue'] = sales['item_cnt_day']*sales['item_price']
labels_df = pd.read_csv(path/'labels.csv')
 labels_df.head()<load_from_csv>
sales = pd.merge(sales, items , how='left', on='item_id')
 sales = pd.merge(sales, item_categories , how='left', on='item_category_id' )
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.head()<categorify>
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, test = [ImageList.from_df(df, path=path, cols='id', folder=folder, suffix='.png')
 for df, folder in zip([train_df, test_df], ['train', 'test'])]
data =(train.split_by_rand_pct(0.2, seed=42)
.label_from_df(cols='attribute_ids', label_delim=' ')
.add_test(test)
.transform(get_transforms() , size=128)
.databunch(path=Path('.'), bs=64 ).normalize() )<define_variables>
lagged_submissions = pd.merge(submissions , test , on='ID' ).drop(columns=['item_cnt_month'])
 lagged_submissions = pd.merge(lagged_submissions, all_data[all_data['date_block_num']==33] , how="left" ,on=['shop_id' , 'item_id'])
 lagged_submissions = lagged_submissions.filter(['ID','target_lag_1'])
 values = {'target_lag_1': 0}
lagged_submissions = lagged_submissions.fillna(value=values)
 lagged_submissions['item_cnt_month'] = lagged_submissions['target_lag_1'].clip(0,20)
 lagged_submissions.filter(['ID' , 'item_cnt_month'] ).to_csv('sample_submissions.csv' , index=False )
data.show_batch()<compute_test_metric>
feature_cnt = 5
tfidf = feature_extraction.text.TfidfVectorizer(max_features=feature_cnt)
 items['item_name_len'] = items['item_name'].map(len)
 items['item_name_wc'] = items['item_name'].map(lambda x: len(str(x ).split(' ')))
 txtFeatures = pd.DataFrame(tfidf.fit_transform(items['item_name'] ).toarray())
 cols = txtFeatures.columns
for i in range(feature_cnt):
items['item_name_tfidf_' + str(i)] = txtFeatures[cols[i]]
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val =(-logit ).clamp(min=0)
 loss = logit - logit * target + max_val + \ 
(( -max_val ).exp() +(-logit - max_val ).exp() ).log()
invprobs = F.logsigmoid(-logit *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
 return loss.mean()<choose_model_class>
feature_cnt = 5
tfidf = feature_extraction.text.TfidfVectorizer(max_features=feature_cnt)
 item_categories['item_cat_name_len'] = item_categories['item_category_name'].map(len)
 item_categories['item_cat_name_wc'] = item_categories['item_category_name'].map(lambda x: len(str(x ).split(' ')))
 txtFeatures = pd.DataFrame(tfidf.fit_transform(item_categories['item_category_name'] ).toarray())
 cols = txtFeatures.columns
for i in range(feature_cnt):
item_categories['item_cat_name_tfidf_' + str(i)] = txtFeatures[cols[i]]
learn = cnn_learner(data, base_arch=models.densenet201, loss_func=FocalLoss() , metrics=fbeta )<find_best_params>
all_data = pd.merge(all_data, items[['item_id','item_category_id' , 'item_name_tfidf_0' , 'item_name_tfidf_1',
'item_name_tfidf_2','item_name_tfidf_3','item_name_tfidf_4']], how='left', on=['item_id','item_category_id'])
 all_data = pd.merge(all_data, item_categories[['item_category_id' , 'item_cat_name_tfidf_0' , 'item_cat_name_tfidf_1',
'item_cat_name_tfidf_2','item_cat_name_tfidf_3','item_cat_name_tfidf_4' ]], how='left', on=['item_category_id'] )
learn.lr_find()
learn.recorder.plot()<train_model>
data = [[0, 8], [1,2] , [2,2], [3,0] , [4,6] , [5,3] , [6,1] , [7,2] , [8,2] , [9,1] , [10,2] , [11,3] , [12,9] ,
[13,3] , [14,4] , [15, 1] , [16,8] , [17,7] , [18,2] , [19,0] , [20,2] , [21,2] , [22, 4] , [23,1] , [24,10],
[25,3] , [26,3] , [27,1] , [28,5] , [29,5] , [30,2] , [31,0] , [32 ,2] , [33,1], [34,3]]
holidays = pd.DataFrame(data, columns = ['date_block_num', 'num_hols'])
 holidays['holiday_season'] = holidays['date_block_num'].apply(lambda x: 1 if x in([0,11,12,23,24])else 0 )
lr = 3e-2
learn.fit_one_cycle(3, slice(lr))<train_model>
all_data = pd.merge(all_data, holidays, how='left', on='date_block_num')
 all_data = downcast_dtypes(all_data )
lr = 1e-3
learn.fit_one_cycle(21, slice(lr/10, lr))<predict_on_test>
cor_target = abs(corrmatt["target"])
 relevant_features = cor_target[cor_target>0.5]
relevant_features
valid_preds = learn.get_preds(DatasetType.Valid)
 best_thr = find_best_fixed_threshold(*valid_preds )<predict_on_test>
all_data.to_pickle('feature_data.pkl')
 del all_data, pp_df, sales
test_preds = learn.get_preds(DatasetType.Test)
 test_df.attribute_ids = join_preds(test_preds, best_thr)
 test_df.head()<save_to_csv>
all_data = pd.read_pickle('feature_data.pkl' )
test_df.to_csv('submission.csv', index=False )<set_options>
all_data = all_data[all_data['date_block_num'] >= 12]
shift_range= [1, 2, 3, 4, 5, 6,9, 12]
fit_cols = [col for col in all_data.columns if col[-1] in [str(item)for item in shift_range]]
for col in all_data.columns:
if "mean" in col:
fit_cols.append(col)
 for col in all_data.columns:
if "tfidf" in col:
fit_cols.append(col)
 to_drop_cols = list(set(list(all_data.columns)) -(set(fit_cols)|set(index_cols)))+ ['date_block_num' , 'shop_id' , 'item_id' ]
gc.collect() ;
h2o.init(max_mem_size = "10G" )<load_from_csv>
dates = all_data['date_block_num']
last_block = dates.max()
print('Test `date_block_num` is %d' % last_block )
print("
Data Load Stage")
 training = pd.read_csv('.. /input/train.csv', index_col = "item_id", parse_dates = ["activation_date"])
 traindex = training.index
testing = pd.read_csv('.. /input/test.csv', index_col = "item_id", parse_dates = ["activation_date"])
 testdex = testing.index
y = training.deal_probability.copy()
print('Train shape: {} Rows, {} Columns'.format(*training.shape))
print('Test shape: {} Rows, {} Columns'.format(*testing.shape))<feature_engineering>
dates = all_data['date_block_num']
print('Test `date_block_num` is %d' % test_block)
 dates_train = dates[dates < test_block-1]
dates_val = dates[dates == test_block-1]
dates_test = dates[dates == test_block]
X_train = all_data.loc[dates < test_block-1].drop(to_drop_cols, axis=1)
 X_test = all_data.loc[dates == test_block].drop(to_drop_cols, axis=1)
 X_val = all_data.loc[dates == test_block-1].drop(to_drop_cols, axis=1)
 y_train = all_data.loc[dates < test_block-1, 'target'].values.clip(0,40)
 y_test = all_data.loc[dates == test_block, 'target'].values.clip(0,40)
 y_val = all_data.loc[dates == test_block-1, 'target'].values.clip(0,40)
 target_range=(0,20)
 X_cv_train = all_data.loc[(( dates>28)&(dates<32)) ].drop(to_drop_cols, axis=1)
 y_cv_train = all_data.loc[(( dates>28)&(dates<32)) , 'target'].values.clip(0,40)
df = pd.concat([training,testing],axis=0)
 del training, testing
gc.collect()
print('
All Data shape: {} Rows, {} Columns'.format(*df.shape))
print("Feature Engineering")
 df["price"] = np.log(df["price"]+0.001)
 df["price"].fillna(-999,inplace=True)
 df["image_top_1"].fillna(-999,inplace=True)
 print("
Create Time Variables")
 df["Weekday"] = df['activation_date'].dt.weekday
df["Weekd of Year"] = df['activation_date'].dt.week
df["Day of Month"] = df['activation_date'].dt.day
df.drop(["activation_date","image"],axis=1,inplace=True)
 print("
Encode Variables")
 categorical = ["user_id","region","city","parent_category_name","category_name","item_seq_number","user_type","image_top_1"]
messy_categorical = ["param_1","param_2","param_3","title","description"]
print("Encoding :",categorical + messy_categorical)
 lbl = preprocessing.LabelEncoder()
for col in categorical + messy_categorical:
df[col] = lbl.fit_transform(df[col].astype(str))
X = df.loc[traindex,:].copy()
print("Training Set shape",X.shape)
 test = df.loc[testdex,:].copy()
print("Submission Set Shape: {} Rows, {} Columns".format(*test.shape))
del df
gc.collect() 
<drop_column>
X_train.isna().sum().sum()
test.drop("deal_probability",axis=1, inplace=True )<split>
X_test.isna().sum().sum()
<create_dataframe>
print(' Training a plain linear regression model ')
 gc.collect()
target_range=(0,20)
 lr = LinearRegression()
lr.fit(X_train.values, y_train )
htrain = h2o.H2OFrame(X)
 htest = h2o.H2OFrame(test )<predict_on_test>
pred_lr_val = lr.predict(X_val)
 print('Val RMSE for lr is %f' % sqrt(mean_squared_error(y_val, pred_lr_val.clip(0,20))))
print('R^2 score for lr is %f' %(r2_score(y_val, pred_lr_val.clip(0,20))))
x =htrain.columns
y ='deal_probability'
x.remove(y)
 aml = H2OAutoML(max_runtime_secs = 18000)
 aml.train(x=x, y =y, training_frame=htrain)
 print('Generate predictions...')
 htrain.drop(['deal_probability'])
<compute_test_metric>
over_underfit_plot(lr, X_train, y_train )
<set_options>
pred_lr = lr.predict(X_test.values ).clip(*target_range)
 print(' pred_lr min %f , pred_lr max %f ' ,(pred_lr.min() ,pred_lr.max()))
aml.leader<predict_on_test>
scaler = StandardScaler().fit(X_train)
 X_train_std = scaler.transform(X_train)
 X_valid_std = scaler.transform(X_val)
 model_lr_scaled = LinearRegression(fit_intercept=True, normalize=True)
 model_lr_scaled.fit(X_train_std, y_train)
 print(np.sqrt(mean_squared_error(y_val, model_lr_scaled.predict(X_valid_std))))
model_lr_scaled.predict(X_valid_std ).min()
over_underfit_plot(model_lr_scaled, X_train_std, y_train )
preds = aml.leader.predict(htest)
 preds = preds.as_data_frame()<save_to_csv>
print('Val RMSE for lr is %f' % sqrt(mean_squared_error(y_val, model_lr_scaled.predict(X_val))))
print('R^2 score for lr is %f' %(r2_score(y_val, model_lr_scaled.predict(X_val))))
lgsub = pd.DataFrame(preds.predict.values,columns=["deal_probability"],index=testdex)
 lgsub['deal_probability'].clip(0.0, 1.0, inplace=True)
 lgsub.to_csv("lgsub.csv",index=True,header=True)
 print("Notebook Runtime: %0.2f Minutes"%(( time.time() - notebookstart)/60))<set_options>
print('Val RMSE for lr is %f' % sqrt(mean_squared_error(y_val, model_lr_scaled.predict(X_val ).clip(0,20))))
print('R^2 score for lr is %f' %(r2_score(y_val, model_lr_scaled.predict(X_val ).clip(0,20))))
%matplotlib inline
pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999
plt.rcParams['figure.figsize'] =(30, 15)
 plt.rcParams['font.size'] = 25<load_from_csv>
pred_lr_scaled = model_lr_scaled.predict(X_test.values ).clip(*target_range )
train = pd.read_csv(".. /input/avito-demand-prediction/train.csv", parse_dates=["activation_date"])
 test = pd.read_csv(".. /input/avito-demand-prediction/test.csv", parse_dates=["activation_date"])
 data = pd.concat([train, test])
 del train, test<load_from_csv>
pd.read_csv?<count_values>
opt_hyp_params = {
'verbose': 1,
'seed': 0,
'max_depth': 8,
'learning_rate': 0.1,
'reg_lambda': 2.0,
'reg_alpha': 2.0,
'subsample': 0.7,
'num_leaves': 20,
'feature_fraction': 0.8,
'drop_rate': 0.2,
'bagging_fraction': 0.75,
'min_split_gain': 0.1,
'min_child_weight': 19
}
int(opt_hyp_params['num_leaves'] )
data.dtypes.value_counts()<define_variables>
evals_result = {}
lgb_params = {
'metric': 'rmse',
'nthread':1,
'num_leaves' : 50,
'learning_rate' : 0.01,
'max_depth':7,
'bagging_fraction':0.6,
'feature_fraction': 0.8,
'min_child_samples':20,
'min_child_weight': 0.001,
'reg_alpha':0.5,
'reg_lambda':0.001,
'min_data_in_leaf': 2**7,
'bagging_seed': 2**7,
'bagging_freq':1,
'verbose':0 ,
'subsample': 0.7
}
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_valid = lgb.Dataset(X_val, y_val)
 lgb_cv_train = lgb.Dataset(X_cv_train, y_cv_train)
 cat_features = [ 'si_mean' , 'ii_mean' , 'ic_mean' , 'tc_mean' , 'stc_mean' ]
parent_category_name_map = {"Личные вещи" : "Personal belongings",
"Для дома и дачи" : "For the home and garden",
"Бытовая электроника" : "Consumer electronics",
"Недвижимость" : "Real estate",
"Хобби и отдых" : "Hobbies & leisure",
"Транспорт" : "Transport",
"Услуги" : "Services",
"Животные" : "Animals",
"Для бизнеса" : "For business"}
region_map = {"Свердловская область" : "Sverdlovsk oblast",
"Самарская область" : "Samara oblast",
"Ростовская область" : "Rostov oblast",
"Татарстан" : "Tatarstan",
"Волгоградская область" : "Volgograd oblast",
"Нижегородская область" : "Nizhny Novgorod oblast",
"Пермский край" : "Perm Krai",
"Оренбургская область" : "Orenburg oblast",
"Ханты-Мансийский АО" : "Khanty-Mansi Autonomous Okrug",
"Тюменская область" : "Tyumen oblast",
"Башкортостан" : "Bashkortostan",
"Краснодарский край" : "Krasnodar Krai",
"Новосибирская область" : "Novosibirsk oblast",
"Омская область" : "Omsk oblast",
"Белгородская область" : "Belgorod oblast",
"Челябинская область" : "Chelyabinsk oblast",
"Воронежская область" : "Voronezh oblast",
"Кемеровская область" : "Kemerovo oblast",
"Саратовская область" : "Saratov oblast",
"Владимирская область" : "Vladimir oblast",
"Калининградская область" : "Kaliningrad oblast",
"Красноярский край" : "Krasnoyarsk Krai",
"Ярославская область" : "Yaroslavl oblast",
"Удмуртия" : "Udmurtia",
"Алтайский край" : "Altai Krai",
"Иркутская область" : "Irkutsk oblast",
"Ставропольский край" : "Stavropol Krai",
"Тульская область" : "Tula oblast"}
category_map = {"Одежда, обувь, аксессуары":"Clothing, shoes, accessories",
"Детская одежда и обувь":"Children's clothing and shoes",
"Товары для детей и игрушки":"Children's products and toys",
"Квартиры":"Apartments",
"Телефоны":"Phones",
"Мебель и интерьер":"Furniture and interior",
"Предложение услуг":"Offer services",
"Автомобили":"Cars",
"Ремонт и строительство":"Repair and construction",
"Бытовая техника":"Appliances",
"Товары для компьютера":"Products for computer",
"Дома, дачи, коттеджи":"Houses, villas, cottages",
"Красота и здоровье":"Health and beauty",
"Аудио и видео":"Audio and video",
"Спорт и отдых":"Sports and recreation",
"Коллекционирование":"Collecting",
"Оборудование для бизнеса":"Equipment for business",
"Земельные участки":"Land",
"Часы и украшения":"Watches and jewelry",
"Книги и журналы":"Books and magazines",
"Собаки":"Dogs",
"Игры, приставки и программы":"Games, consoles and software",
"Другие животные":"Other animals",
"Велосипеды":"Bikes",
"Ноутбуки":"Laptops",
"Кошки":"Cats",
"Грузовики и спецтехника":"Trucks and buses",
"Посуда и товары для кухни":"Tableware and goods for kitchen",
"Растения":"Plants",
"Планшеты и электронные книги":"Tablets and e-books",
"Товары для животных":"Pet products",
"Комнаты":"Room",
"Фототехника":"Photo",
"Коммерческая недвижимость":"Commercial property",
"Гаражи и машиноместа":"Garages and Parking spaces",
"Музыкальные инструменты":"Musical instruments",
"Оргтехника и расходники":"Office equipment and consumables",
"Птицы":"Birds",
"Продукты питания":"Food",
"Мотоциклы и мототехника":"Motorcycles and bikes",
"Настольные компьютеры":"Desktop computers",
"Аквариум":"Aquarium",
"Охота и рыбалка":"Hunting and fishing",
"Билеты и путешествия":"Tickets and travel",
"Водный транспорт":"Water transport",
"Готовый бизнес":"Ready business",
"Недвижимость за рубежом":"Property abroad"}<feature_engineering>
%%time
gc.collect()
evals_result = {}
model_lgb =lgb.train(lgb_params, lgb_train, num_boost_round=500,
early_stopping_rounds=200, categorical_feature=cat_features,
evals_result=evals_result,verbose_eval=50,valid_sets=[lgb_train, lgb_valid] )
data['region_en'] = data['region'].apply(lambda x : region_map[x])
 data['parent_category_name_en'] = data['parent_category_name'].apply(lambda x : parent_category_name_map[x])
 data['category_name_en'] = data['category_name'].apply(lambda x : category_map[x] )<count_missing_values>
pred_lgb = model_lgb.predict(X_test.values ).clip(*target_range)
 pickle.dump(model_lgb,open("firstlevel_lgb_model" , "wb"))
data.isnull().sum() / data.shape[0] * 100<count_values>
pred_lgb_val = model_lgb.predict(X_val)
 print('Val RMSE for lgb is %f' % sqrt(mean_squared_error(y_val, pred_lgb_val.clip(*target_range))))
print('Val RMSE for lgb is %f' % sqrt(mean_squared_error(y_val, pred_lgb_val)) )
pd.cut(data["deal_probability"].dropna() , 10 ).value_counts().sort_index()<concatenate>
X_test_level2 = np.c_[pred_lr, pred_lgb]
pd.concat([data.region_en.value_counts().rename("abs"), data.region_en.value_counts(True ).rename("rel")], axis=1 )<filter>
dates_train_level2 = dates_train[dates_train.isin([27,28, 29, 30, 31, 32])]
y_train_level2 = y_train[dates_train.isin(dates_train_level2)]
train = data[data.deal_probability.notnull() ]<concatenate>
print(X_train_level2.mean(axis=0))
reg_label = pd.crosstab(train.region_en, pd.cut(train.deal_probability, 10)).apply(lambda x: x/x.sum() , axis=1)
 reg_label<count_values>
alphas_to_try = np.linspace(0, 1, 1001)
 r2_scores = np.array([r2_score(y_train_level2, np.dot(X_train_level2, [alpha, 1 - alpha])) for alpha in alphas_to_try])
 best_alpha = alphas_to_try[r2_scores.argmax() ]
r2_train_simple_mix = r2_scores.max()
print('Best alpha: %f; Corresponding r2 score on train: %f' %(best_alpha, r2_train_simple_mix))
train.category_name_en.value_counts()<groupby>
test_preds = best_alpha * pred_lr +(1 - best_alpha)* pred_lgb
test['item_cnt_month'] = test_preds.clip(*target_range)
 test[['ID', 'item_cnt_month']].to_csv('submission_averaging.csv', index=False )
data.groupby("parent_category_name_en" ).category_name_en.apply(set )<count_values>
test_preds_lr_scaled = best_alpha * pred_lr_scaled +(1 - best_alpha)* pred_lgb
test['item_cnt_month'] = test_preds_lr_scaled.clip(*target_range)
 test[['ID', 'item_cnt_month']].to_csv('submission_averaging_lr_scaled.csv', index=False )
data.parent_category_name_en.value_counts(True )<count_unique_values>
metamodel = LinearRegression()
metamodel.fit(X_train_level2, y_train_level2)
 pickle.dump(metamodel, open("metamodel", 'wb'))
data.apply(lambda x: x.unique().shape[0] )<groupby>
train_preds = metamodel.predict(X_train_level2 ).clip(*target_range)
 r2_train_stacking = r2_score(y_train_level2.clip(*target_range), train_preds)
 test_preds = metamodel.predict(np.vstack(( pred_lr, pred_lgb)).T)
 test['item_cnt_month'] = test_preds.clip(*target_range)
 test[['ID', 'item_cnt_month']].to_csv('submission_stacking.csv', index=False)
 print('Train R-squared for stacking is %f' % r2_train_stacking )
train.groupby(pd.qcut(train.item_seq_number, 15)).deal_probability.mean()<count_values>
rmse_train = np.sqrt(mean_squared_error(y_train_level2, train_preds))
print('RMSE Train: %f' % rmse_train)
train.user_type.value_counts()<feature_engineering>
print(' mean train preds : %f ' ,(train_preds.mean()))
<count_values><EOS> 
print(' mean test preds : %f ' ,(test_preds.mean()))