Kaggle competition: Categorical Feature Encoding Challenge II
train['target']=target
scaler=StandardScaler()
test_data=scaler.fit_transform(test[features])
 test_data=test_data.reshape(test_data.shape[0],test_data.shape[1],1 )<choose_model_class>
path=Path('/kaggle/input/cat-in-the-dat-ii/train.csv')
 df = pd.read_csv(path)
 df.set_index('id',drop=True,inplace=True)
 for column in df.columns:
df[column].fillna(df[column].mode() [0], inplace=True)
 df.head()
def NN_model() :
model=Sequential()
model.add(Flatten(input_shape=(23,1)))
 model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer=Adam(lr=1e-3), loss='binary_crossentropy', metrics=['accuracy'])
 return model<split>
dfprocessed= df.copy()
dfprocessed['bin_3'] = dfprocessed['bin_3'].apply(lambda x: 0 if x == 'F' else 1)
 dfprocessed['bin_4'] = dfprocessed['bin_4'].apply(lambda x: 0 if x == 'N' else 1)
 dfprocessed.ord_1.replace(to_replace = ['Novice', 'Contributor','Expert', 'Master', 'Grandmaster'],
value = [0, 1, 2, 3, 4], inplace = True)
 dfprocessed.ord_2.replace(to_replace = ['Freezing', 'Cold', 'Warm', 'Hot','Boiling Hot', 'Lava Hot'],
value = [0, 1, 2, 3, 4, 5], inplace = True)
 dfprocessed.ord_3.replace(to_replace = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], inplace = True)
 dfprocessed.ord_4.replace(to_replace = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R','S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25], inplace = True)
 high_card = ['nom_0','nom_1','nom_2','nom_3','nom_4','nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9','ord_5']
for col in high_card:
enc_nom =(dfprocessed.groupby(col ).size())/ len(dfprocessed)
 dfprocessed[f'{col}'] = dfprocessed[col].apply(lambda x: hash(str(x)) % 5000)
 df=dfprocessed.copy()
df.head()
oof_preds = np.zeros(( len(train)))
 test_preds = np.zeros(( len(test)))
 skf = StratifiedKFold(n_splits=10)
 for train_index, test_index in skf.split(train, train.target.values):
X_train, X_test = train.iloc[train_index, :], train.iloc[test_index, :]
X_train = X_train.reset_index(drop=True)
 X_test = X_test.reset_index(drop=True)
 y_train, y_test = X_train.target, X_test.target
X_train=scaler.fit_transform(X_train[features])
 X_test=scaler.fit_transform(X_test[features])
 y_train=y_train.to_numpy()
y_test=y_test.to_numpy()
X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],1)
 X_test=X_test.reshape(X_test.shape[0],X_test.shape[1],1)
 es = callbacks.EarlyStopping(monitor='val_accuracy', min_delta=0.001, patience=5,verbose=1, mode='max', baseline=None, restore_best_weights=True)
 rlr = callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.5,patience=3, min_lr=1e-6, mode='max', verbose=1)
 model = NN_model()
class_weight = {0: 0.25, 1: 0.75}
model.fit(X_train, y_train, epochs=25, batch_size=1024, callbacks=[es, rlr], verbose=1,validation_data=(X_test, y_test), class_weight=class_weight)
 valid_fold_preds = model.predict(X_test)
 test_fold_preds = model.predict(test_data)
 oof_preds[test_index] = valid_fold_preds.ravel()
test_preds += test_fold_preds.ravel()
print(metrics.roc_auc_score(y_test, valid_fold_preds))
K.clear_session()<save_to_csv>
X = df.drop('target', axis=1)
 y = df.target
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.15, random_state=111)
 categorical_features_indices = []
model=CatBoostClassifier(iterations=600,
learning_rate=0.1,
depth=5,
bootstrap_type='Bernoulli',
loss_function='Logloss',
subsample=0.9,
eval_metric='AUC',
metric_period=20,
allow_writing_files=False)
 model.fit(X_train, y_train, cat_features=categorical_features_indices, eval_set=(X_val, y_val))
test_preds /= 10
print("Saving submission file")
 submission = pd.DataFrame.from_dict({
'id': test_id,
'target': test_preds
})
 submission.to_csv("submission.csv", index=False )<set_options>
shap.initjs()
explainer = shap.TreeExplainer(model)
 shap_values = explainer.shap_values(Pool(X_train, y_train, cat_features=categorical_features_indices))
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )<load_from_csv>
leaveOneOut_encoder = LeaveOneOutEncoder()
path=Path('/kaggle/input/cat-in-the-dat-ii/train.csv')
 df = pd.read_csv(path)
 df.set_index('id',drop=True,inplace=True)
 df['bin_3'] = df['bin_3'].apply(lambda x: 0.0 if x == 'F' else 1.0)
 df['bin_4'] = df['bin_4'].apply(lambda x: 0.0 if x == 'N' else 1.0)
 df.ord_1.replace(to_replace = ['Novice', 'Contributor','Expert', 'Master', 'Grandmaster'],
value = [0, 1, 2, 3, 4], inplace = True)
 df.ord_2.replace(to_replace = ['Freezing', 'Cold', 'Warm', 'Hot','Boiling Hot', 'Lava Hot'],
value = [0, 1, 2, 3, 4, 5], inplace = True)
 df.ord_3.replace(to_replace = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], inplace = True)
 df.ord_4.replace(to_replace = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R','S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25], inplace = True)
 pathsub=Path('/kaggle/input/cat-in-the-dat-ii/test.csv')
 dfsub = pd.read_csv(pathsub)
 dfsub.set_index('id',drop=True,inplace=True)
 dfsub['bin_3'] = dfsub['bin_3'].apply(lambda x: 0.0 if x == 'F' else 1.0)
 dfsub['bin_4'] = dfsub['bin_4'].apply(lambda x: 0.0 if x == 'N' else 1.0)
 dfsub.ord_1.replace(to_replace = ['Novice', 'Contributor','Expert', 'Master', 'Grandmaster'],
value = [0, 1, 2, 3, 4], inplace = True)
 dfsub.ord_2.replace(to_replace = ['Freezing', 'Cold', 'Warm', 'Hot','Boiling Hot', 'Lava Hot'],
value = [0, 1, 2, 3, 4, 5], inplace = True)
 dfsub.ord_3.replace(to_replace = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], inplace = True)
 dfsub.ord_4.replace(to_replace = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R','S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
value = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25], inplace = True)
 high_card = ['nom_0','nom_1','nom_2','nom_3','nom_4','nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9','ord_5']
for nom in high_card:
df[f'{nom}_lOO'] = leaveOneOut_encoder.fit_transform(df[nom], df["target"])
 dfsub[f'{nom}_lOO'] = leaveOneOut_encoder.transform(dfsub[nom])
 df['nom_9_lOO_bool']=df['nom_9_lOO']
df['nom_9_lOO_bool'].fillna(df['nom_9_lOO_bool'].mode() [0], inplace=True)
 df['nom_9_lOO_bool']=df['nom_9_lOO_bool'].apply(lambda x: 0 if(x>0.35 and x<0.7)else 1)
 dfsub['nom_9_lOO_bool']=dfsub['nom_9_lOO']
dfsub['nom_9_lOO_bool'].fillna(dfsub['nom_9_lOO_bool'].mode() [0], inplace=True)
 dfsub['nom_9_lOO_bool']=dfsub['nom_9_lOO_bool'].apply(lambda x: 0 if(x>0.35 and x<0.7)else 1)
 procs = [Normalize,FillMissing,Categorify]
dep_var = 'target'
cat_names=['nom_9_lOO_bool','bin_0','bin_1','bin_2','bin_3','bin_4','ord_0','ord_1','ord_2','ord_3','ord_4','day','month','nom_0','nom_1','nom_2','nom_3','nom_4','nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9','ord_5']
cont_names = ['nom_0_lOO','nom_1_lOO','nom_2_lOO','nom_3_lOO','nom_4_lOO','nom_5_lOO', 'nom_6_lOO', 'nom_7_lOO', 'nom_8_lOO','nom_9_lOO','ord_5_lOO']
valid_idx=np.random.random_integers(0,600000,100000)
 test = TabularList.from_df(dfsub, path=pathsub, cat_names=cat_names,cont_names=cont_names, procs=procs)
 data =(TabularList.from_df(df, path=path, cat_names=cat_names,cont_names=cont_names, procs=procs)
.split_by_idx(valid_idx=valid_idx)
.label_from_df(cols=dep_var)
.add_test(test)
.databunch(bs=1024))
data.show_batch()
%%time
train = h2o.import_file(".. /input/multi-cat-encodings/X_train_te.csv")
 test = h2o.import_file(".. /input/multi-cat-encodings/X_test_te.csv" )<feature_engineering>
weights = [0.3, 0.7]
class_weights=torch.FloatTensor(weights )
x = test.columns
y = 'target'
train[y] = train[y].asfactor()<train_model>
learn = tabular_learner(data, layers=[1500,500,250], metrics=AUROC() ,callback_fns=ShowGraph,path='.',emb_drop=0.01,use_bn=True ).to_fp32()
learn.loss_func = nn.CrossEntropyLoss(weight=class_weights )
aml = H2OAutoML(seed=47, max_runtime_secs=30000)
 aml.train(x=x, y=y, training_frame=train, fold_column='fold_column' )<find_best_params>
learn.fit_one_cycle(4, 1e-2,wd = 0.25 )
aml.leader<predict_on_test>
learn.fit_one_cycle(3, 1e-3,wd = 0.25 )
preds = aml.predict(test)
 preds['p1'].as_data_frame().values.flatten().shape<load_from_csv>
ClassConfusion(interp,[0, 1],varlist=cont_names,figsize=(12,12))
sample_submission = pd.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv')
 sample_submission.shape<save_to_csv>
learn.save('deepmodel' )
sample_submission['target'] = preds['p1'].as_data_frame().values
sample_submission.to_csv('h2o_automl_submission_4.csv', index=False )<set_options>
test_preds = learn.get_preds(ds_type=DatasetType.Test )
%matplotlib inline<categorify>
test_preds = test_preds[0][:,1]
test_preds
<load_from_csv><EOS> 
pathsub=Path('/kaggle/input/cat-in-the-dat-ii/test.csv')
 dfsub = pd.read_csv(pathsub)
 dfsub['target'] = test_preds
dfsub.to_csv('submission.csv', columns=['id', 'target'], index=False )