Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<groupby>
root = Path(".. /input" )
movies_df = train_df.loc[train_df['item_category']=='Кино',
:].groupby('item_id', as_index=False ).sum() [['item_id', 'item_cnt_day']]<merge>
train = pd.read_csv(root.joinpath("train.csv"))
test = pd.read_csv(root.joinpath("test.csv"))
top20_movies = pd.merge(movies_df, item_df ).sort_values(by='item_cnt_day', ascending=False ).head(20 )<rename_columns>
train_df = train.copy()
test_df = test.copy()
train_df.drop(columns=["ID_code", "target"], inplace=True)
 test_df.drop(columns=["ID_code"], inplace=True)
 target = train.target
top20_movies = top20_movies.rename(columns={'item_cnt_day': 'item_cnt_sum'} )<set_options>
def augment_train(df_train, y_train):
t0 = df_train[y_train == 0].copy()
t1 = df_train[y_train == 1].copy()
i = 0
N = 3
for I in range(2):
for col in df_train.columns:
i = i + 1000
np.random.seed(i)
 np.random.shuffle(t0[col].values)
 np.random.shuffle(t1[col].values)
 df_train = pd.concat([df_train, t0.copy() ])
 df_train = pd.concat([df_train, t1.copy() ])
 y_train = pd.concat([y_train, pd.Series([0] * t0.shape[0]), pd.Series([1] * t1.shape[0])])
 return df_train, y_train
del top20_movies
gc.collect()<sort_values>
model = CatBoostClassifier(subsample=0.36,
custom_loss='Logloss',
random_strength = 0,
max_depth=3,
eval_metric="AUC",
learning_rate=0.02,
iterations=60000,
bootstrap_type='Bernoulli',
l2_leaf_reg=0.3,
task_type="GPU",
random_seed=432013,
od_type="Iter",
border_count=128
)
item_sum = item_sum.sort_values(by='revenue_sum' )<set_options>
def run_cat(model, trt, tst, tar,n_splits=5, plot=False):
kf = KFold(n_splits=n_splits, random_state=432013, shuffle=True)
 oof = np.zeros(len(trt))
feature_importance_df = pd.DataFrame()
y_valid_pred = 0 * tar
y_test_pred = 0
for n_fold,(train_index, valid_index)in enumerate(kf.split(trt, tar)) :
y_train, y_valid = tar.iloc[train_index], tar.iloc[valid_index]
X_train, X_valid = trt.iloc[train_index,:], trt.iloc[valid_index,:]
X_train, y_train = augment_train(X_train, y_train)
 _train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("Fold ", n_fold)
 fit_model = model.fit(_train,
verbose_eval=1000,
early_stopping_rounds=1000,
eval_set=[_valid],
use_best_model=True,
plot=False, 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
oof[valid_index] = pred
print("auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(tst)[:,1]
y_test_pred /= n_splits
print("average auc:", roc_auc_score(tar, oof))
return y_test_pred, oof
del item_sum, item_mean
gc.collect()<groupby>
y_test_pred, oof = run_cat(model,train_df, test_df, target )
games_df = train_df.loc[train_df['item_category']=='Игры',
:].groupby('item_id', as_index=False ).sum() [['item_id', 'revenue', 'item_cnt_day']]<merge>
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
pd.Series(oof ).to_csv("Cat_oof.csv", index = False)
 submission.to_csv('submission_cb_light_0.8999.csv', index=False )