Kaggle competition: Categorical Feature Encoding Challenge
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge<feature_engineering>
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
import scipy
from tqdm import tqdm_notebook as tqdm
sub['FVC1'] = 0.997 * pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]<prepare_output>
dd0=pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv")
 ddtest0=pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv")
 ddall=dd0.append(ddtest0, sort=False)
 num_train=len(dd0)
 ddall.head()
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()<feature_engineering>
drop_cols=["bin_0"]
ddall["ord_5a"]=ddall["ord_5"].str[0]
ddall["ord_5b"]=ddall["ord_5"].str[1]
drop_cols.append("ord_5" )
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']<load_from_csv>
X=ddall[ddall.columns.difference(["id", "target"] + drop_cols)]
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1<save_to_csv>
X_oh=X[X.columns.difference(["ord_1", "ord_4", "ord_5a", "ord_5b", "day", "month"])]
oh1=pd.get_dummies(X_oh, columns=X_oh.columns, drop_first=True, sparse=True)
 ohc1=oh1.sparse.to_coo()
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )<import_modules>
class ThermometerEncoder(TransformerMixin):

 def __init__(self, sort_key=None):
self.sort_key = sort_key
self.value_map_ = None
def fit(self, X, y=None):
self.value_map_ = {val: i for i, val in enumerate(sorted(X.unique() , key=self.sort_key)) }
return self
def transform(self, X, y=None):
values = X.map(self.value_map_)
 possible_values = sorted(self.value_map_.values())
 idx1 = []
idx2 = []
all_indices = np.arange(len(X))
for idx, val in enumerate(possible_values[:-1]):
new_idxs = all_indices[values > val]
idx1.extend(new_idxs)
 idx2.extend(repeat(idx, len(new_idxs)))
 result = scipy.sparse.coo_matrix(( [1] * len(idx1),(idx1, idx2)) , shape=(len(X), len(possible_values)) , dtype="int8")
 return result
from keras.utils.vis_utils import plot_model<choose_model_class>
thermos=[]
for col in ["ord_1", "ord_2", "ord_3", "ord_4", "ord_5a", "day", "month"]:
if col=="ord_1":
sort_key=['Novice', 'Contributor', 'Expert', 'Master', 'Grandmaster'].index
elif col=="ord_2":
sort_key=['Freezing', 'Cold', 'Warm', 'Hot', 'Boiling Hot', 'Lava Hot'].index
elif col in ["ord_3", "ord_4", "ord_5a"]:
sort_key=str
elif col in ["day", "month"]:
sort_key=int
else:
raise ValueError(col)
 enc=ThermometerEncoder(sort_key=sort_key)
 thermos.append(enc.fit_transform(X[col]))
net = make_model()<define_variables>
ohc=scipy.sparse.hstack([ohc1] + thermos ).tocsr()
display(ohc)
 X_train = ohc[:num_train]
X_test = ohc[num_train:]
y_train = dd0["target"].values
plot_model(net ,to_file='model_plot.png', show_shapes=True, show_layer_names=True )<set_options>
clf=LogisticRegression(C=0.123456789, solver="lbfgs", max_iter=5000)
 clf.fit(X_train, y_train)
 pred=clf.predict_proba(X_test)[:,1]
pd.DataFrame({"id": ddtest0["id"], "target": pred} ).to_csv("submission.csv", index=False )
pd.set_option('display.max_columns', 50)
 tf_version = tf.__version__
print("
Tensorflow version " + tf_version )<define_variables>
score=cross_validate(clf, X_train, y_train, cv=3, scoring="roc_auc")["test_score"].mean()
print(f"{score:.6f}" )