Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<feature_engineering>
import scipy
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder
from sklearn.compose import make_column_transformer
from sklearn.pipeline import make_union, make_pipeline
from sklearn.preprocessing import MinMaxScaler, FunctionTransformer
from sklearn.model_selection import cross_validate
from statistics import mean, stdev
from math import sqrt
import category_encoders as ce
X_train = X_train / 255
test = test / 255<categorify>
D0 = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv", index_col="id")
 D_test = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv", index_col="id")
 y_train = D0["target"]
D = D0.drop(columns="target")
 test_ids = D_test.index
X_train = X_train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1)
 Y_train = to_categorical(Y_train, num_classes = 10 )<choose_model_class>
ordinal_order = {
"ord_1": ["Novice", "Contributor", "Expert", "Master", "Grandmaster"],
**{col: sorted(D[col].dropna().unique())for col in ["ord_3", "ord_4", "ord_5"]},
}
for k, vals in ordinal_order.items() :
vals.insert(len(vals)//2, "nan")
 ordinal_order[k] = list(map(str, vals))
model = Sequential()
model.add(Conv2D(input_shape=(28,28,1), kernel_size=(5,5),
filters=20, activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=2, padding='same'))
model.add(Conv2D(kernel_size=(5,5), filters=50, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=2, padding='same'))
model.add(Flatten())
 model.add(Dense(500, activation='relu'))
model.add(Dense(10, activation='softmax'))<choose_model_class>
target_encs = {"nom_6": ce.LeaveOneOutEncoder(cols=["nom_6"])}
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'] )<train_model>
X_train=D.astype(str)
 X_test=D_test.astype(str )
history = model.fit(X_train, Y_train, validation_split=0.1,
epochs=100, batch_size=64 )<predict_on_test>
def make_cat_trans(onehot_cols, ordinal_order, target_encs):
encs = []
if onehot_cols:
encs.append(make_column_transformer(( OneHotEncoder(sparse=True, dtype="int8", handle_unknown="ignore"), onehot_cols)))
 if ordinal_order:
ordinal_enc = make_column_transformer(*[(OrdinalEncoder([vals], dtype="float32"), [col])for col, vals in ordinal_order.items() ])
 encs.append(make_pipeline(ordinal_enc, MinMaxScaler(copy=False)))
 encs.append(make_pipeline(ordinal_enc, MinMaxScaler(copy=False), FunctionTransformer(lambda x:4*(x-0.5)**2, validate=False)))
 if target_encs:
encs.append(make_column_transformer(*[(enc, [col])for col, enc in target_encs.items() ]))
trans = make_union(*encs)
 return trans
onehot_cols = X_train.columns.difference(ordinal_order.keys() ).tolist()
trans = make_cat_trans(onehot_cols, ordinal_order, target_encs )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
%%time
clf=LogisticRegression(C=0.05, solver="lbfgs", max_iter=5000)
 model=make_pipeline(trans, clf)
 model.fit(X_train, y_train)
 pred = model.predict_proba(X_test)[:, 1]
pd.DataFrame({"id": test_ids, "target": pred} ).to_csv("submission.csv", index=False )