Kaggle competition: Santander Customer Transaction Prediction
ones = dt_train['target'].value_counts() [1]
zeros = dt_train['target'].value_counts() [0]
ones_per = ones / dt_train.shape[0] * 100
zeros_per = zeros / dt_train.shape[0] * 100
print('{} out of {} rows are Class 1 and it is the {:.2f}% of the dataset.'.format(ones, dt_train.shape[0], ones_per))
print('{} out of {} rows are Class 0 and it is the {:.2f}% of the dataset.'.format(zeros, dt_train.shape[0], zeros_per))<merge>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
dt_train_unique = dt_train.agg(['nunique'] ).transpose().sort_values(by='nunique')
 dt_test_unique = dt_test.agg(['nunique'] ).transpose().sort_values(by='nunique')
 dt_uniques = dt_train_unique.drop('target' ).reset_index().merge(dt_test_unique.reset_index() , how='right', right_index=True, left_index=True)
 print(dt_uniques.head())
<drop_column>
train_df = df_train.copy()
test_df = df_test.copy()
train_df.drop(columns=["ID_code", "target"], inplace=True)
 test_df.drop(columns=["ID_code"], inplace=True)
 target = df_train.target
dt_uniques.drop(columns=['index_y'], inplace=True)
 dt_uniques.columns = ['Feature', 'Training Set Unique Count', 'Test Set Unique Count']
print(dt_uniques.head() )<prepare_x_and_y>
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
cols=["target","ID_code"]
X = dt_train.drop(cols, axis=1)
 y = dt_train.target<init_hyperparams>
model = CatBoostClassifier(subsample=0.36,
custom_loss='Logloss',
random_strength = 0,
max_depth=3,
eval_metric="AUC",
learning_rate=0.01,
iterations=60000,
bootstrap_type='Bernoulli',
l2_leaf_reg=0.3,
task_type="GPU",
random_seed=432013,
od_type="Iter",
border_count=128
)
params = {'objective': "binary",
'boost': "gbdt",
'boost_from_average': "false",
'num_threads': 8,
'learning rate': 0.001,
'num_leaves': 4,
'max_depth': 8,
'tree_learner': "serial",
'feature_fraction': 0.05,
'bagging_fraq': 1.0,
'bagging_fraction': 0.4,
'min_data_in_leaf': 80,
'verbosity': 1,
'metric': 'auc',
'sigmoid':1.0,
'lambda_l1':0.01,
'snapshot_freq':1
}<create_dataframe>
def run_cat(model, trt, tst, tar,n_splits=11, 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
folds = StratifiedKFold(n_splits=5, shuffle=False, random_state=10)
 length_train = np.zeros(len(dt_train))
dt_predictions = np.zeros(len(dt_test))
important_dt = pd.DataFrame()<train_model>
y_test_pred, oof = run_cat(model,train_df, test_df, target )
<drop_column><EOS> 
submission = pd.read_csv(".. /input/sample_submission.csv")
 submission['target'] = y_test_pred
pd.Series(oof ).to_csv("Cat_oof.csv", index = False)
 submission.to_csv('submission.csv', index=False )