Kaggle competition: Categorical Feature Encoding Challenge II
tf.keras.models.save_model(model, '.. /output/kaggle/working/', overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None )<compute_test_metric>
with timer("Load"):
nrow = None
PATH = "/kaggle/input/cat-in-the-dat-ii/"
train = pd.read_csv(PATH + "train.csv", index_col = 'id', nrows = nrow)
 test = pd.read_csv(PATH + "test.csv", index_col = 'id')
 submission_df = pd.read_csv(PATH + "sample_submission.csv")
 [print(x.shape)for x in [train, test, submission_df]]
traindex = train.index
testdex = test.index
y = train.target.copy()
df = pd.concat([train.drop('target',axis = 1), test], axis = 0)
 del train, test, submission_df
print("Overall AUC={}".format(metrics.roc_auc_score(train.target.values, oof_preds)) )<save_to_csv>
with timer("Categorical Processing"):
categorical = df.columns
for col in categorical:
diff = list(set(df.loc[testdex, col].unique())- set(df.loc[traindex,col].unique()))
if diff:
print("Column {} has {} unseen categories in test set".format(col, len(diff)))
 df.loc[df[col].isin(diff),col] = 999
if df[col].dtypes == object:
df[col] = df[col].astype(str)
 lbl = preprocessing.LabelEncoder()
df[col] = pd.Series(lbl.fit_transform(df[col].values)).astype('category' )
test_preds /= 50
test_ids = test.id.values
print("Saving submission file")
 submission = pd.DataFrame.from_dict({
'id': test_ids,
'target': test_preds
})
 submission.to_csv("submission.csv", index=False )<count_values>
categorical_index = list(range(0, len(categorical)))
 features_names = df.columns
catboost_pool = catboost.Pool(df.loc[traindex,:],
label=y,
cat_features=categorical_index)
 test_pool = catboost.Pool(data=df.loc[testdex,:],
cat_features = categorical_index)
 del df
gc.collect()
train_df['target'].value_counts()<drop_column>
def catboost_blackbox(max_depth, reg_lambda):
param = {
'learning_rate': 0.2,
'bagging_temperature': 0.1,
'l2_leaf_reg': reg_lambda,
'depth': int(max_depth),
'iterations' : top_boosting_rounds,
'task_type':'GPU',
'loss_function' : "Logloss",
'objective':'Logloss',
'eval_metric' : "AUC",
'bootstrap_type' : 'Bayesian',
'random_seed': seed,
'early_stopping_rounds' : early_stopping_rounds,
'use_best_model': False,
"verbose": False
}
modelstart= time.time()
scores = catboost.cv(catboost_pool,
param,
fold_count = 2,
stratified = True,
shuffle = True,
partition_random_seed = seed,
plot = False 
)
 runtime =(time.time() - modelstart)/60
optimise = scores.loc[scores['test-AUC-mean'].idxmax() ,'test-AUC-mean'] - scores.loc[scores['test-AUC-mean'].idxmax() ,'test-AUC-std']
optimisation_info.append([scores['test-AUC-mean'].idxmax() , optimise, runtime, param, scores['test-AUC-mean'].idxmax() ])
 return optimise
target=train_df['target']
del train_df['target']<count_values>
parameter_bounds = {
'reg_lambda':(0.1, 10),
'max_depth':(3,16)
 }
init_points = 2
n_iter = 4
optimisation_info = []
CATBOOST_BO = BayesianOptimization(catboost_blackbox,
parameter_bounds,
random_state=seed )
train_df['nom_0'].value_counts()<count_values>
with timer("Bayesian Optimisation - {} Iterations".format(init_points + n_iter)) :
with warnings.catch_warnings() :
warnings.filterwarnings('ignore')
 CATBOOST_BO.maximize(init_points = init_points,
n_iter = n_iter,
acq = 'ucb',
xi = 0.0,
alpha = 1e-6 )
train_df['nom_1'].value_counts()<count_values>
print("Best Score: {}".format(CATBOOST_BO.max['target']))
train_df['nom_2'].value_counts()<count_values>
optimisation_pd = pd.DataFrame(optimisation_info, columns = ['Best Round', 'Score', 'Runtime','Param', 'Iterations'])
 optimisation_pd.head()
train_df['nom_3'].value_counts()<count_values>
best_param = optimisation_pd.loc[optimisation_pd['Score'].idxmax() ,'Param']
best_param['iterations'] = top_boosting_rounds*3
best_param['learning_rate'] = 0.04
best_param['early_stopping_rounds'] = early_stopping_rounds
best_param
for i in range(10):
print(train_df['nom_{}'.format(i)].value_counts())
 print('
' )<concatenate>
with timer("Catboost Single Model"):
best_param['iterations'] = best_iteration
model = catboost.CatBoostClassifier(**best_param)
 model.fit(catboost_pool )
data=pd.concat([train_df, test_df] )<define_variables>
cm = catboost.utils.get_confusion_matrix(model, catboost_pool)
 print(cm )
cols=['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4'] 
<categorify>
roc_curve_values = catboost.utils.get_roc_curve(model, catboost_pool, plot=True )
mode = data.filter(cols ).mode()
data[cols]=data[cols].fillna(mode.iloc[0] )<categorify>
( thresholds, fnr)= catboost.utils.get_fnr_curve(curve=roc_curve_values, plot=True )
data['bin_3']=data['bin_3'].map({'F': 0, 'T':1})
 data['bin_4']=data['bin_4'].map({'Y':1, 'N':0} )<data_type_conversions>
results = model.predict_proba(test_pool)[:, 1]
submission = pd.DataFrame({'id': testdex, 'target': results})
 submission.to_csv('submission.csv', index=False )
data[['bin_0', 'bin_1', 'bin_2']]=data[['bin_0', 'bin_1', 'bin_2']].astype('int8' )<define_variables>
print("Notebook Runtime: %0.2f Hours"%(( time.time() - notebookstart)/60/60))