Kaggle competition: Categorical Feature Encoding Challenge II
procs = [FillMissing, Categorify, Normalize]
FillMissing.FillStrategy='MEAN'
dep_var = 'target'
cat_names = ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4', 'nom_0', 'nom_1', 'nom_2',
'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9',
'ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5', 'day', 'month']
cont_names = []
X=train[cont_names]
X=train[cat_names]<create_dataframe>
%%time
train = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv")
 test = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv" )
PATH = Path('/kaggle/input/cat-in-the-dat-ii/')
 test = TabularList.from_df(X_test, path=PATH, cat_names=cat_names, cont_names=cont_names )<split>
test["target"] = -1
data = pd.concat([train, test] ).reset_index(drop=True )
data =(TabularList.from_df(train, path=PATH,
cat_names=cat_names,
cont_names=cont_names,
procs=procs)
.split_by_idx(valid_idx = range(len(train)-50000, len(train)))
.label_from_df(cols=dep_var)
.add_test(test)
.databunch())
 print(data.train_ds.cont_names)
 print(data.train_ds.cat_names )<define_variables>
for feature in features:
train[feature] = train[feature].map(feature_map[feature])
 test[feature] = test[feature].map(feature_map[feature] )
data.show_batch()<choose_model_class>
warnings.filterwarnings("ignore" )
learn = tabular_learner(data, layers=[200,100], metrics=accuracy,model_dir="/tmp/model/", ps=0.15 )<train_model>
def auc(y_true, y_pred):
def fallback_auc(y_true, y_pred):
try:
return metrics.roc_auc_score(y_true, y_pred)
 except:
return 0.5
return tf.py_function(fallback_auc,(y_true, y_pred), tf.double )
learn.fit(1, lr=1e-3 )<define_search_space>
def create_model() :
inp = layers.Input(shape=(23,))
x = layers.Embedding(5725, 10 )(inp)
 x = layers.Flatten()(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(500, activation="relu" )(x)
 x = layers.Dropout(0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(300, activation="relu" )(x)
 x = layers.Dropout(0.3 )(x)
 x = layers.BatchNormalization()(x)
 y = layers.Dense(2, activation="softmax" )(x)
 model = Model(inputs=inp, outputs=y)
 return model
lr=1e-03<train_model>
model = create_model()
learn.fit_one_cycle(1, slice(lr))<define_search_space>
oof_preds = np.zeros(( len(train_data)))
 test_preds = np.zeros(( len(test_data)) )
lr=1e-03<train_model>
%%time
skf = StratifiedKFold(n_splits=10)
 k = 0
for train_index, test_index in skf.split(train_data, train_label):
x_train, x_test = train_data.iloc[train_index, :], train_data.iloc[test_index, :]
y_train, y_test = train_label.iloc[train_index, :], train_label.iloc[test_index, :]
model = create_model()
adam = Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, amsgrad=False)
 model.compile(loss='binary_crossentropy', optimizer=adam, metrics=[auc])
 lr = callbacks.ReduceLROnPlateau(monitor='val_auc', factor=0.1,
patience=3, min_lr=1e-6, mode='max', verbose=1)
 es = callbacks.EarlyStopping(monitor='val_auc', min_delta=0.001, patience=5,
verbose=1, mode='max', baseline=None, restore_best_weights=True)
 model.fit(x_train,
utils.to_categorical(y_train),
validation_data=(x_test, utils.to_categorical(y_test)) ,
verbose=1,
batch_size=1024,
callbacks=[lr, es],
epochs=20 
)
 valid_fold_preds = model.predict(x_test)[:, 1]
test_fold_preds = model.predict(test_data)[:, 1]
oof_preds[test_index] = valid_fold_preds.ravel()
test_preds += test_fold_preds.ravel()
print("KFold %d, the best auc is %.4f" %(k, metrics.roc_auc_score(y_test, valid_fold_preds)))
 k += 1
K.clear_session()
<save_model><EOS> 
test_preds /= 10
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 )