Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<feature_engineering>
import pandas as pd
from pathlib import Path
import numpy as np
sales_aggr_month = sales_train_df[["date", "item_cnt_day"]]
sales_aggr_month["date"] = sales_train_df.date.dt.month
sales_aggr_month = sales_aggr_month.groupby(["date"])["item_cnt_day"]
sales_aggr_month_mean = sales_aggr_month.agg("mean")
 sales_aggr_month_sum = sales_aggr_month.agg("sum" )<groupby>
root = Path(".. /input" )
item_aggr = items_df[["item_id", "item_category_id"]].groupby(["item_category_id"])
 item_aggr_cnt = item_aggr.count()
item_aggr_cnt.head()<groupby>
train = pd.read_csv(root.joinpath("train.csv"))
test = pd.read_csv(root.joinpath("test.csv"))
shop_aggr = sales_train_df[["item_id", "shop_id"]].groupby(["shop_id"])
 shop_aggr_cnt = shop_aggr.count()
shop_aggr_cnt.head()<groupby>
train_id = train.ID_code
test_id = test.ID_code
target = train.target
train.drop(columns=["ID_code", "target"], inplace=True)
 test.drop(columns=["ID_code"], inplace=True )
neg_cnt = sales_train_df[sales_train_df.item_cnt_day < 0]
neg_shop_aggr = neg_cnt[["item_id", "shop_id"]].groupby(["shop_id"])
 neg_shop_aggr_cnt = neg_shop_aggr.count()
neg_shop_aggr_cnt = neg_shop_aggr_cnt.reindex(range(shop_aggr_cnt.shape[0]), fill_value=0)
 neg_shop_aggr_cnt.head()<compute_test_metric>
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score
from catboost import Pool, CatBoostClassifier
print(f"Corr coef between the lines: {np.corrcoef(shop_norm, neg_shop_norm)[0, 1]:.4f}" )<categorify>
model = CatBoostClassifier(loss_function="Logloss",
eval_metric="AUC",
task_type="GPU",
learning_rate=0.01,
iterations=70000,
l2_leaf_reg=50,
random_seed=432013,
od_type="Iter",
depth=5,
early_stopping_rounds=15000,
border_count=64
)
test_df = reduce_mem_usage(test_df)
 items_df = reduce_mem_usage(items_df)
 shops_df = reduce_mem_usage(shops_df)
 item_categories_df = reduce_mem_usage(item_categories_df )<define_variables>
n_split = 15
kf = KFold(n_splits=n_split, random_state=432013, shuffle=True )
previous_count = sales_train_df.shape[0]
sales_train_df = sales_train_df[sales_train_df.item_price < unique_prices[-1]]
sales_train_df = sales_train_df[sales_train_df.item_cnt_day < unique_count[-1]]
new_count = sales_train_df.shape[0]
print(f"Reduced: {previous_count - new_count} values" )<feature_engineering>
y_valid_pred = 0 * target
y_test_pred = 0
sales_train_df.loc[sales_train_df.item_price < 0, "item_price"] = np.median(sales_train_df.loc[(sales_train_df.date_block_num==4)
 &(sales_train_df.shop_id==32)
 &(sales_train_df.item_id==2973),
"item_price"] )<feature_engineering>
for idx,(train_index, valid_index)in enumerate(kf.split(train)) :
y_train, y_valid = target.iloc[train_index], target.iloc[valid_index]
X_train, X_valid = train.iloc[train_index,:], train.iloc[valid_index,:]
_train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("
Fold ", idx)
 fit_model = model.fit(_train,
eval_set=_valid,
use_best_model=True,
verbose=5000,
plot=True 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
print(" auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(test)[:,1]
y_test_pred /= n_split
shops_df.loc[shops_df.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops_df['city'] = shops_df['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops_df.loc[shops_df.city == '!Якутск', 'city'] = 'Якутск'
shops_df['city_code'] = LabelEncoder().fit_transform(shops_df['city'])
 shops_df = shops_df[['shop_id','city_code']]
item_categories_df['split'] = item_categories_df['item_category_name'].str.split('-')
 item_categories_df['type'] = item_categories_df['split'].map(lambda x: x[0].strip())
 item_categories_df['type_code'] = LabelEncoder().fit_transform(item_categories_df['type'])
 item_categories_df['subtype'] = item_categories_df['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 item_categories_df['subtype_code'] = LabelEncoder().fit_transform(item_categories_df['subtype'])
 item_categories_df = item_categories_df[['item_category_id','type_code', 'subtype_code']]
items_df.drop(['item_name'], axis=1, inplace=True )<feature_engineering>
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
submission.to_csv('submission_cb_light_0.8999.csv', index=False )