Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<load_from_csv>
sns.set(style="darkgrid")
 rcParams['figure.figsize'] = 12, 4
prev = pd.read_csv('.. /input/previous_application.csv', nrows = num_rows )<categorify>
items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 shops = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv')
 cats = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 train = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 test = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv' )
prev, cat_cols = one_hot_encoder(prev, nan_as_category= True)
 prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True )<feature_engineering>
train =(
 train
[ 
(train['item_price'] > 0)& 
(train['item_price'] < 300000)& 
(train['item_cnt_day'] < 1000)
 ] 
.reset_index(drop = True)
)
 train.loc[train['item_cnt_day'] < 0, 'item_cnt_day'] = 0
prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']<define_variables>
for i in [(0, 57),(1, 58),(10, 11)]:
train.loc[train['shop_id'] == i[0], 'shop_id'] = i[1]
test.loc[test['shop_id'] == i[0], 'shop_id'] = i[1]
num_aggregations = {
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_APPLICATION': ['min', 'max', 'mean'],
'AMT_CREDIT': ['min', 'max', 'mean'],
'APP_CREDIT_PERC': ['min', 'max', 'mean', 'var'],
'AMT_DOWN_PAYMENT': ['min', 'max', 'mean'],
'AMT_GOODS_PRICE': ['min', 'max', 'mean'],
'HOUR_APPR_PROCESS_START': ['min', 'max', 'mean'],
'RATE_DOWN_PAYMENT': ['min', 'max', 'mean'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['mean', 'sum'],
}<groupby>
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])
 shops.loc[shops['city'] == '!Якутск', 'city'] = 'Якутск'
cat_aggregations = {}
for cat in cat_cols:
cat_aggregations[cat] = ['mean']
prev_agg = prev.groupby('SK_ID_CURR' ).agg({**num_aggregations, **cat_aggregations})
 prev_agg.columns = pd.Index(['PREV_' + e[0] + "_" + e[1].upper() for e in prev_agg.columns.tolist() ] )<merge>
categories = []
for categ in shops['category'].unique() :
if len(shops[shops['category'] == categ])> 4:
categories.append(categ)
 shops['category'] = shops['category'].apply(lambda x: x if x in categories else 'other' )
approved = prev[prev['NAME_CONTRACT_STATUS_Approved'] == 1]
approved_agg = approved.groupby('SK_ID_CURR' ).agg(num_aggregations)
 approved_agg.columns = pd.Index(['APR_' + e[0] + "_" + e[1].upper() for e in approved_agg.columns.tolist() ])
 prev_agg = prev_agg.join(approved_agg, how='left')
 refused = prev[prev['NAME_CONTRACT_STATUS_Refused'] == 1]
refused_agg = refused.groupby('SK_ID_CURR' ).agg(num_aggregations)
 refused_agg.columns = pd.Index(['REF_' + e[0] + "_" + e[1].upper() for e in refused_agg.columns.tolist() ])
 prev_agg = prev_agg.join(refused_agg, how='left')
 del refused, refused_agg, approved, approved_agg, prev
gc.collect() 
<train_model>
shops['shop_category'] = LabelEncoder().fit_transform(shops['category'])
 shops['shop_city'] = LabelEncoder().fit_transform(shops['city'])
 shops = shops[['shop_id', 'shop_category', 'shop_city']]
print("End previous_application................ " )<train_model>
cats['type_code'] =(
 cats['item_category_name'] 
.apply(
 lambda x: x.split(' ')[0] 
)
.astype(str)
)
 cats.loc[ 
(cats['type_code'] == 'Игровые')| 
(cats['type_code'] == 'Аксессуары'),
'category'
] = 'Игры'
print("Start POS_CASH_balance................ " )<load_from_csv>
categories = []
for categ in cats['type_code'].unique() :
if len(cats[cats['type_code'] == categ])> 4:
categories.append(categ)
 cats['type_code'] = cats['type_code'].apply(lambda x: x if x in categories else 'etc' )
pos = pd.read_csv('.. /input/POS_CASH_balance.csv', nrows = num_rows )<categorify>
cats['type_code'] = LabelEncoder().fit_transform(cats['type_code'])
 cats['split'] =(
 cats['item_category_name'] 
.apply(lambda x: x.split('-')) 
)
 cats['subtype'] =(
 cats['split'] 
.apply(
 lambda x: x[1].strip() if len(x)>= 2 else x[0].strip() 
)
)
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id', 'subtype_code', 'type_code']]
pos, cat_cols = one_hot_encoder(pos, nan_as_category= True)
 aggregations = {
'MONTHS_BALANCE': ['max', 'mean', 'size'],
'SK_DPD': ['max', 'mean'],
'SK_DPD_DEF': ['max', 'mean']
}<groupby>
def name_correction(x):
x = x.lower()
x = x.partition('[')[0]
x = x.partition('(')[0]
x = re.sub('\W+', ' ', x)
 x = x.replace(' ', ' ')
 x = x.strip()
return x
for cat in cat_cols:
aggregations[cat] = ['mean']
pos_agg = pos.groupby('SK_ID_CURR' ).agg(aggregations)
 pos_agg.columns = pd.Index(['POS_' + e[0] + "_" + e[1].upper() for e in pos_agg.columns.tolist() ])
 pos_agg['POS_COUNT'] = pos.groupby('SK_ID_CURR' ).size()
del pos
gc.collect()<train_model>
items['name1'], items['name2'] = items['item_name'].str.split('[', 1 ).str
items['name1'], items['name3'] = items['item_name'].str.split('(', 1 ).str
items['name2'] = items['name2'].str.replace('\W+', ' ' ).str.lower()
items['name3'] = items['name3'].str.replace('\W+', ' ' ).str.lower()
items = items.fillna('0')
 items['item_name'] = items['item_name'].apply(lambda x: name_correction(x))
items['name2'] = items['name2'].apply(lambda x: x[:-1] if x != '0' else '0' )
print("Start POS_CASH_balance................ " )<load_from_csv>
items['type'] =(
 items['name2'] 
.apply(
 lambda x: x[0:8] if x.split(' ')[0] == 'xbox' else x.split(' ')[0] 
)
)
 items.loc[ 
(items['type'] == 'x360')| 
(items['type'] == 'xbox360')| 
(items['type'] == 'xbox 360'),
'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'] == 'pс'),
'type'
] = 'pс'
items.loc[items['type'] == 'рs3' , 'type'] = 'рs3'
ins = pd.read_csv('.. /input/installments_payments.csv', nrows = num_rows)
 ins, cat_cols = one_hot_encoder(ins, nan_as_category= True )<feature_engineering>
group_sum =(
 items 
.groupby('type')
.agg({'item_id': 'count'})
.reset_index() 
)
 drop_cols = []
for categ in group_sum['type'].unique() :
if group_sum.loc[(group_sum['type'] == categ), 'item_id'].values[0] <= 39:
drop_cols.append(categ)
 items['name2'] =(
 items['name2'] 
.apply(
 lambda x: 'other' if x in drop_cols else x 
)
)
 items = items.drop(['type'], axis=1 )
ins['PAYMENT_PERC'] = ins['AMT_PAYMENT'] / ins['AMT_INSTALMENT']
ins['PAYMENT_DIFF'] = ins['AMT_INSTALMENT'] - ins['AMT_PAYMENT']
ins['DPD'] = ins['DAYS_ENTRY_PAYMENT'] - ins['DAYS_INSTALMENT']
ins['DBD'] = ins['DAYS_INSTALMENT'] - ins['DAYS_ENTRY_PAYMENT']
ins['DPD'] = ins['DPD'].apply(lambda x: x if x > 0 else 0)
 ins['DBD'] = ins['DBD'].apply(lambda x: x if x > 0 else 0 )<groupby>
items['name2'] = LabelEncoder().fit_transform(items['name2'])
 items['name3'] = LabelEncoder().fit_transform(items['name3'])
 items.drop(['item_name', 'name1'], axis=1, inplace=True)
aggregations = {
'NUM_INSTALMENT_VERSION': ['nunique'],
'DPD': ['max', 'mean', 'sum'],
'DBD': ['max', 'mean', 'sum'],
'PAYMENT_PERC': ['max', 'mean', 'sum', 'var'],
'PAYMENT_DIFF': ['max', 'mean', 'sum', 'var'],
'AMT_INSTALMENT': ['max', 'mean', 'sum'],
'AMT_PAYMENT': ['min', 'max', 'mean', 'sum'],
'DAYS_ENTRY_PAYMENT': ['max', 'mean', 'sum']
}
for cat in cat_cols:
aggregations[cat] = ['mean']
ins_agg = ins.groupby('SK_ID_CURR' ).agg(aggregations)
 ins_agg.columns = pd.Index(['INS_' + e[0] + "_" + e[1].upper() for e in ins_agg.columns.tolist() ])
 ins_agg['INS_COUNT'] = ins.groupby('SK_ID_CURR' ).size()
del ins
gc.collect()<train_model>
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 = np.int16 
)
)
 matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix = matrix.astype({
'date_block_num': np.int8,
'shop_id': np.int8,
'item_id': np.int16
})
 matrix.sort_values(cols, inplace=True )
print("End POS_CASH_balance................ " )<train_model>
train['revenue'] = train['item_cnt_day'] * train['item_price']
print("Start credit_card_balance................ " )<load_from_csv>
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)
.astype(np.float16)
 )
cc = pd.read_csv('.. /input/credit_card_balance.csv', nrows = num_rows )<categorify>
test['date_block_num'] = 34
test =(
 test 
.astype({
'date_block_num': np.int8,
'shop_id': np.int8,
'item_id': np.int16
})
 )
cc, cat_cols = one_hot_encoder(cc, nan_as_category= True)
 cc.drop(columns = ['SK_ID_PREV'], inplace = True)
 cc_agg = cc.groupby('SK_ID_CURR' ).agg(['min', 'max', 'mean', 'sum', 'var'])
 cc_agg.columns = pd.Index(['CC_' + e[0] + "_" + e[1].upper() for e in cc_agg.columns.tolist() ])
 cc_agg['CC_COUNT'] = cc.groupby('SK_ID_CURR' ).size()
del cc
gc.collect()<train_model>
matrix = pd.concat(
 [matrix, test.drop(['ID'], axis=1)],
ignore_index=True, sort=False, keys=cols 
)
 matrix.fillna(0, inplace=True )
print("End credit_card_balance................ " )<merge>
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 =(
 matrix 
.astype({
'shop_city': np.int8,
'shop_category': np.int8,
'item_category_id': np.int8,
'subtype_code': np.int8,
'name2': np.int8,
'name3': np.int16,
'type_code': np.int8
})
 )
with timer("Process bureau and bureau_balance"):
print("Bureau df shape:", bureau_agg.shape)
 df = df.join(bureau_agg, how='left',on='SK_ID_CURR')
 gc.collect()
with timer("Process previous_applications"):
print("Previous applications df shape:", prev_agg.shape)
 df = df.join(prev_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process POS-CASH balance"):
print("Pos-cash balance df shape:", pos_agg.shape)
 df = df.join(pos_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process installments payments"):
print("Installments payments df shape:", ins_agg.shape)
 df = df.join(ins_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process credit card balance"):
print("Credit card balance df shape:", cc_agg.shape)
 df = df.join(cc_agg, how='left', on='SK_ID_CURR')
 gc.collect()
del bureau_agg,prev_agg,pos_agg,ins_agg,cc_agg
gc.collect()<train_model>
def lag_feature(df, lags, cols):
for col in cols:
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'] = shifted['date_block_num'] + i
df = pd.merge(df, shifted, on=['date_block_num', 'shop_id', 'item_id'], how='left')
 return df
print("Done.;.............. ")
<split>
matrix = lag_feature(matrix, [1, 2, 3], ['item_cnt_month'] )
train_df = df[df['TARGET'].notnull() ]
test_df = df[df['TARGET'].isnull() ]<data_type_conversions>
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 )
train_df = train_df.drop(['index'],axis=1)
 test_df = test_df.drop(['index','TARGET'],axis=1)
 train_df = train_df.fillna(0)
 test_df = test_df.fillna(0 )<split>
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 )
label = u'TARGET'
a = list(train_df.columns)
 a.remove(label)
 labels = train_df[label]
data_only = train_df[list(a)]
col_name = data_only.columns
X_train, X_test, y_train, y_test = train_test_split(data_only, labels, test_size=0.1,random_state = 42 )<train_model>
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 )
clf_xgBoost = xgb.XGBClassifier(
 learning_rate =0.01, n_estimators=1000, max_depth=4, min_child_weight=4, subsample=0.8, colsample_bytree=0.8, objective= 'binary:logistic',
nthread=4, scale_pos_weight=2, seed=27)
 clf_xgBoost.fit(data_only,labels )<predict_on_test>
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, 2, 3], ['date_shop_item_avg_item_cnt'])
 matrix.drop(['date_shop_item_avg_item_cnt'], axis=1, inplace=True )
pred = clf_xgBoost.predict_proba(test_df)
 test_df['TARGET'] = pred[:, 0]<save_to_csv>
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 )
test_df[['SK_ID_CURR', 'TARGET']].to_csv('submission_clf_xgBoost.csv', index= False )<set_options>
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 )
plt.style.use('fivethirtyeight')
 %matplotlib inline
init_notebook_mode(connected=True)
 print(os.listdir(".. /input"))
PATH = ".. /input" 
<load_from_csv>
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 )
data = pd.read_csv(PATH+"/application_train.csv")
 test = pd.read_csv(PATH+"/application_test.csv")
 bureau = pd.read_csv(PATH+"/bureau.csv")
 bureau_balance = pd.read_csv(PATH+"/bureau_balance.csv")
 credit_card_balance = pd.read_csv(PATH+"/credit_card_balance.csv")
 installments_payments = pd.read_csv(PATH+"/installments_payments.csv")
 previous_application = pd.read_csv(PATH+"/previous_application.csv")
 POS_CASH_balance = pd.read_csv(PATH+"/POS_CASH_balance.csv" )<load_from_csv>
group =(
 train 
.groupby('item_id')
.agg({
'item_price': 'mean'
})
)
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = matrix.merge(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 = matrix.merge(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_trends(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_trends, axis=1)
 matrix['delta_price_lag_'] = matrix['delta_price_lag_'].astype(np.float16)
 matrix['delta_price_lag_'].fillna(0, inplace=True)
 features_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
features_to_drop.append('date_item_avg_item_price_lag_' + str(i))
features_to_drop.append('delta_price_lag_' + str(i))
matrix.drop(features_to_drop, axis=1, inplace=True )
data = pd.read_csv(PATH+"/application_train.csv", nrows=10000)
 test = pd.read_csv(PATH+"/application_test.csv", nrows=10000)
 bureau = pd.read_csv(PATH+"/bureau.csv", nrows=10000)
 bureau_balance = pd.read_csv(PATH+"/bureau_balance.csv", nrows=10000)
 credit_card_balance = pd.read_csv(PATH+"/credit_card_balance.csv", nrows=10000)
 installments_payments = pd.read_csv(PATH+"/installments_payments.csv", nrows=10000)
 previous_application = pd.read_csv(PATH+"/previous_application.csv", nrows=10000)
 POS_CASH_balance = pd.read_csv(PATH+"/POS_CASH_balance.csv", nrows=10000 )<feature_engineering>
group =(
 train 
.groupby(['date_block_num', 'shop_id'])
.agg({
'revenue': 'sum'
})
)
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = matrix.merge(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_block_num': 'mean'
})
)
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = matrix.merge(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.float32)
 matrix = lag_feature(matrix, [1], ['delta_revenue'])
 matrix['delta_revenue_lag_1'] = matrix['delta_revenue_lag_1'].astype(np.float32)
 matrix.drop(
 ['date_shop_revenue', 'shop_avg_revenue', 'delta_revenue'],
axis=1, inplace=True
)
data['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 data['CODE_GENDER'].replace({'XNA': 'F'}, inplace=True)
 data['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 data['YEARS_BUILD_CREDIT'] = data['AMT_CREDIT']/data['YEARS_BUILD_AVG']
data['Annuity_Income'] = data['AMT_ANNUITY']/data['AMT_INCOME_TOTAL']
data['Income_Cred'] = data['AMT_CREDIT']/data['AMT_INCOME_TOTAL']
data['EMP_AGE'] = data['DAYS_EMPLOYED']/data['DAYS_BIRTH']
data['Income_PP'] = data['AMT_INCOME_TOTAL']/data['CNT_FAM_MEMBERS']
data['CHILDREN_RATIO'] =(1 + data['CNT_CHILDREN'])/ data['CNT_FAM_MEMBERS']
data['PAYMENTS'] = data['AMT_ANNUITY']/ data['AMT_CREDIT']
data['NEW_CREDIT_TO_GOODS_RATIO'] = data['AMT_CREDIT'] / data['AMT_GOODS_PRICE']
data['GOODS_INCOME'] = data['AMT_GOODS_PRICE']/data['AMT_INCOME_TOTAL']
data['Ext_source_mult'] = data['EXT_SOURCE_1'] * data['EXT_SOURCE_2'] * data['EXT_SOURCE_3']
data['Ext_SOURCE_MEAN'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis = 1)
 data['Ext_SOURCE_SD'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis = 1)
 columns = ['Annuity_Income', 'Income_Cred', 'EMP_AGE', 'Income_PP']
test['CODE_GENDER'].replace({'XNA': 'F'}, inplace=True)
 test['YEARS_BUILD_CREDIT'] = test['AMT_CREDIT']/test['YEARS_BUILD_AVG']
test['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 test['Annuity_Income'] = test['AMT_ANNUITY']/test['AMT_INCOME_TOTAL']
test['Income_Cred'] = test['AMT_CREDIT']/test['AMT_INCOME_TOTAL']
test['EMP_AGE'] = test['DAYS_EMPLOYED']/test['DAYS_BIRTH']
test['Income_PP'] = test['AMT_INCOME_TOTAL']/test['CNT_FAM_MEMBERS']
test['CHILDREN_RATIO'] =(1 + test['CNT_CHILDREN'])/ test['CNT_FAM_MEMBERS']
test['PAYMENTS'] = test['AMT_ANNUITY']/ test['AMT_CREDIT']
test['NEW_CREDIT_TO_GOODS_RATIO'] = test['AMT_CREDIT'] / test['AMT_GOODS_PRICE']
test['GOODS_INCOME'] = test['AMT_GOODS_PRICE']/test['AMT_INCOME_TOTAL']
test['Ext_source_mult'] = test['EXT_SOURCE_1'] * test['EXT_SOURCE_2'] * test['EXT_SOURCE_3']
test['Ext_SOURCE_MEAN'] = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis = 1)
 test['Ext_SOURCE_SD'] = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis = 1 )<merge>
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 )
bureau_new = bureau
group = bureau_new[['SK_ID_CURR', 'DAYS_CREDIT']].groupby('SK_ID_CURR')['DAYS_CREDIT'].count().reset_index().rename(index=str, columns={'DAYS_CREDIT': 'BUREAU_LOAN_COUNT'})
 bureau_new = bureau_new.merge(group, how = 'left', on = 'SK_ID_CURR')
 bureau_new.head()
del group<merge>
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')
 )
group = bureau_new[['SK_ID_CURR', 'CREDIT_TYPE']].groupby('SK_ID_CURR')['CREDIT_TYPE'].nunique().reset_index().rename(index=str, columns = {'CREDIT_TYPE': 'LOAN_TYPES_PER_CUST'})
 bureau_new = bureau_new.merge(group,on = ['SK_ID_CURR'], how = 'left')
 bureau_new.head()
del group<feature_engineering>
matrix = matrix[matrix['date_block_num'] >= 4]
matrix.head().T
bureau_new["AVERAGE_LOAN_TYPE"] = bureau_new['BUREAU_LOAN_COUNT']/bureau_new['LOAN_TYPES_PER_CUST']<merge>
data = matrix.copy()
replace = {'Active': 1, 'Closed':0, 'Sold': 1, 'Bad debt': 1}
bureau_new['CREDIT_ACTIVE'] = bureau_new['CREDIT_ACTIVE'].replace(replace)
 gp = bureau_new.groupby('SK_ID_CURR')['CREDIT_ACTIVE'].mean().reset_index().rename(index=str, columns={'CREDIT_ACTIVE': 'ACTIVE_LOANS_PERCENTAGE'})
 bureau_new = bureau_new.merge(gp, on = 'SK_ID_CURR', how = 'left')
 bureau_new.head()
del gp<merge>
X_train = data[data.date_block_num <= 32].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num <= 32]['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 )
def repl(x):
if x < 0:
y = 0
else:
y= 1
return y
bureau_new['CREDIT_ENDDATE_BINARY'] = bureau_new['DAYS_CREDIT_ENDDATE'].apply(lambda x: repl(x))
grp = bureau_new.groupby('SK_ID_CURR')['CREDIT_ENDDATE_BINARY'].mean().reset_index().rename(index=str, columns={'CREDIT_ENDDATE_BINARY': 'CREDIT_ENDDATE_PERCENTAGE'})
 bureau_new = bureau_new.merge(grp, on = 'SK_ID_CURR', how = 'left')
 del grp<define_variables>
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
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
)
<categorify><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 )