Kaggle competition: Categorical Feature Encoding Challenge
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )<feature_engineering>
data_path = "/kaggle/input/simple-preprocessing/"
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']<filter>
with open(data_path+"/df_train.pkl", "rb")as myfile:
df_train = pickle.load(myfile )
train = data.loc[data.WHERE=='train']
test = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<choose_model_class>
with open(data_path+"/df_test.pkl", "rb")as myfile:
df_test = pickle.load(myfile )
L.LSTM(25,kernel_initializer=initializer )<set_options>
N_ROUNDS_CATB = 25
N_FOLDS = 10
initializer = tf.keras.initializers.GlorotNormal()<compute_test_metric>
target = df_train["target"]
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model() :
z = L.Input(( 9,), name="Patient")
 y = L.LSTM(25, kernel_initializer=initializer, return_sequences=True )(tf.expand_dims(z, axis=-1))
y = L.LSTM(25, kernel_initializer=initializer,return_sequences=True )(y)
 y = L.Reshape([25*9] )(y)
 x = L.Dense(100,kernel_initializer=initializer, activation="relu", name="d1" )(y)
 x = L.Dense(100,kernel_initializer=initializer, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.8), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model<choose_model_class>
df_train.drop("target", inplace=True, axis=1 )
net=make_model()<choose_model_class>
catb_opt = CatboostOptimizer()
catb_opt.n_trials = N_ROUNDS_CATB
catb_opt.n_folds = N_FOLDS
catb_opt.use_gpu = True
catb_opt.cat_features = categorical_indices(df_train)
 X_cols = df_train.columns
catb_results = catb_opt.classify_binary(X_cols, df_train, df_test, target)
NFOLD = 5
kf = KFold(n_splits=NFOLD )<prepare_x_and_y>
submission = pd.read_csv("/kaggle/input/cat-in-the-dat/sample_submission.csv")
 submission.head()
y = train['FVC'].values
z = train[FE].values
ze = sub[FE].values
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<train_model>
submission["target"] = catb_results.predictions[:, 1]
submission.head()
<compute_test_metric><EOS> 
submission.to_csv("submission_catb.csv", index=False )