Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<define_variables>
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn import linear_model
import os
cat_feat_to_encode = list(set(df_train.columns ).difference(set(['real_ord_0', 'real_ord_1',
'real_ord_2', 'real_ord_3', 'real_ord_4', 'real_ord_5'])))
 smoothing=100
folds = 20
oof = np.zeros(df_train[cat_feat_to_encode].shape)
 test_oof = np.zeros(df_test[cat_feat_to_encode].shape)
<categorify>
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")
 sample = pd.read_csv(".. /input/cat-in-the-dat-ii/sample_submission.csv")
 train.tail()
cat_feat_to_encode = df_train.columns[:].tolist()
oof = np.zeros(df_train.shape)
 test_oof = np.zeros(df_test.shape)
 for tr_idx, oof_idx in StratifiedKFold(n_splits=folds, random_state= 1032, shuffle=True ).split(df_train, y_train):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(df_train.iloc[tr_idx, :], y_train.iloc[tr_idx])
 oof[oof_idx,:] = ce_target_encoder.transform(df_train.iloc[oof_idx, :] ).values
test_oof[:,:] += ce_target_encoder.transform(df_test ).values
test_oof /= folds<create_dataframe>
class CategoricalFeatures:
def __init__(self, df, categorical_features, encoding_type, handle_na=False):

 self.df = df
self.cat_feats = categorical_features
self.enc_type = encoding_type
self.handle_na = handle_na
self.label_encoders = dict()
self.binary_encoders = dict()
self.ohe = None
if self.handle_na:
for c in self.cat_feats:
self.df.loc[:, c] = self.df.loc[:, c].astype(str ).fillna("-9999999")
 self.output_df = self.df.copy(deep=True)
 def _label_encoding(self):
for c in self.cat_feats:
lbl = preprocessing.LabelEncoder()
lbl.fit(self.df[c].values)
 self.output_df.loc[:, c] = lbl.transform(self.df[c].values)
 self.label_encoders[c] = lbl
return self.output_df
def _label_binarization(self):
for c in self.cat_feats:
lbl = preprocessing.LabelBinarizer()
lbl.fit(self.df[c].values)
 val = lbl.transform(self.df[c].values)
 self.output_df = self.output_df.drop(c, axis=1)
 for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
self.output_df[new_col_name] = val[:, j]
self.binary_encoders[c] = lbl
return self.output_df
def _one_hot(self):
ohe = preprocessing.OneHotEncoder()
ohe.fit(self.df[self.cat_feats].values)
 return ohe.transform(self.df[self.cat_feats].values)
 def fit_transform(self):
if self.enc_type == "label":
return self._label_encoding()
elif self.enc_type == "binary":
return self._label_binarization()
elif self.enc_type == "ohe":
return self._one_hot()
else:
raise Exception("Encoding type not understood")
 def transform(self, dataframe):
if self.handle_na:
for c in self.cat_feats:
dataframe.loc[:, c] = dataframe.loc[:, c].astype(str ).fillna("-9999999")
 if self.enc_type == "label":
for c, lbl in self.label_encoders.items() :
dataframe.loc[:, c] = lbl.transform(dataframe[c].values)
 return dataframe
elif self.enc_type == "binary":
for c, lbl in self.binary_encoders.items() :
val = lbl.transform(dataframe[c].values)
 dataframe = dataframe.drop(c, axis=1)
 for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
dataframe[new_col_name] = val[:, j]
return dataframe
elif self.enc_type == "ohe":
return self.ohe(dataframe[self.cat_feats].values)
 else:
raise Exception("Encoding type not understood" )
new_train = pd.DataFrame(data=oof,columns=['te_'+ c for c in cat_feat_to_encode],index=df_train.index.values)
 new_test = pd.DataFrame(data=test_oof,columns=['te_'+ c for c in cat_feat_to_encode],index=df_test.index.values )<concatenate>
train_len = len(train)
 test["target"] = -1
full_data = pd.concat([train, test])
df_train = pd.concat([df_train,new_train],axis=1)
 df_test = pd.concat([df_test,new_test],axis=1 )<concatenate>
cols = [c for c in train.columns if c not in ["id", "target"]]
cat_feats = CategoricalFeatures(full_data,
categorical_features=cols,
encoding_type="ohe",
handle_na=True)
 full_data_transformed = cat_feats.fit_transform()
alldata = pd.concat([df_train,df_test] )<count_missing_values>
X = full_data_transformed[:train_len, :]
X_test = full_data_transformed[train_len:, :]
for c in alldata.columns:
print(c)
 mn = alldata.loc[~alldata[c].isnull() ,c].mean()
sd = alldata.loc[~alldata[c].isnull() ,c].std()
alldata.loc[~alldata[c].isnull() ,c] -= mn
alldata.loc[~alldata[c].isnull() ,c] /= sd<drop_column>
clf = linear_model.LogisticRegression()
clf.fit(X, train.target.values)
 preds = clf.predict_proba(X_test)[:, 1]
clf.score(X, train.target.values)
 sample.loc[:, "target"] = preds
sample.to_csv("submission.csv", index=False )
alldata.drop(nominals,inplace=True,axis=1 )<split>
clf.score(X, train.target.values )