Kaggle competition: Categorical Feature Encoding Challenge
LD_test = patients_tab_test.reset_index()
pred_cols = ['Patient', 'Weeks', 'Patient_Week', 'FVC_init_avg', 'init_week']
return_cols = ['Patient', 'Weeks', 'Patient_Week', 'FVC_hat', 'sigma']
def get_sigma_function(s_intercept, s_multiplier, s_power):
def alt_sigma(coeff, init_week):
coeff = abs(coeff)
 week_distance = abs(init_week)
 sigma = s_intercept + s_multiplier*coeff*(week_distance**s_power)
 return sigma
return alt_sigma
def pred_test(model, sigma_fn):
X = LD_test[linear_decay_features].copy()
XID = LD_test[['Patient']].copy()
XID['coeff_pred'] = model.predict(X, batch_size = 32)
 P = sub[pred_cols].copy()
P = P.merge(XID, how='left', on='Patient')
 P['FVC_hat'] = P['FVC_init_avg'] +(P['coeff_pred'] * P['init_week'])
 P['sigma'] = P.apply(lambda x: sigma_fn(x.coeff_pred, x.init_week), axis = 1)
 return P[return_cols]<find_best_model_class>
submission = pd.read_csv("/kaggle/input/cat-in-the-dat/sample_submission.csv")
 train = pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv")
 test = pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv")
 labels = train.pop('target')
 train_id = train.pop("id")
 test_id = test.pop("id" )
for fold_num in range(5):
prefix = LD_inference.loc[fold_num].prefix
fname = '{}/{}_weights.h5'.format(pretrained_path, prefix)
 s_intercept, s_multiplier, s_power = eval(LD_inference.loc[fold_num].alt_sigma_param)
 f_sigma = get_sigma_function(s_intercept, s_multiplier, s_power)
 model = tf.keras.models.load_model(fname)
 P = pred_test(model, sigma_fn = f_sigma)
 PREDICTIONS['FVC_LD{}'.format(fold_num)] = P['FVC_hat']
PREDICTIONS['Confidence_LD{}'.format(fold_num)] = P['sigma']
del P, fold_num, fname, model<load_from_csv>
labels = labels.values
QR_inference = pd.read_csv(pretrained_path + '/inference_quant_reg_2020Sep23.csv')
 QR_inference<define_variables>
data = pd.concat([train, test] )
qr_features8 = ['fvc_init','week', 'sex_code', 'age', 'height', 'has_smoked', 'current_smoker', 'percent_init']
qr_features7 = ['fvc_init','week', 'sex_code', 'age', 'has_smoked', 'current_smoker', 'percent_init']<choose_model_class>
data["ord_5a"] = data["ord_5"].str[0]
data["ord_5b"] = data["ord_5"].str[1]
for fold_num in range(5):
prefix = QR_inference.loc[fold_num].prefix
fname = '{}/{}_weights.h5'.format(pretrained_path, prefix)
 model = tf.keras.models.load_model(fname, compile = False)
 model.compile(loss='mae', optimizer='adam', metrics=['mae'])
 num_features = QR_inference.loc[fold_num].num_features
if num_features == 7: features = qr_features7
else: features = qr_features8
X = sub[features].copy()
preds = model.predict(X)
 PREDICTIONS['FVC_QR{}'.format(fold_num)] = preds[:,1]
PREDICTIONS['Confidence_QR{}'.format(fold_num)] = preds[:,2] - preds[:,0]
del X, fold_num, fname, model, prefix, num_features, features, preds<feature_engineering>
data.drop(["bin_0", "ord_5"], axis=1, inplace=True )
QR_FVC_cols = [col for col in PREDICTIONS.columns if 'FVC_QR' in col]
LD_FVC_cols = [col for col in PREDICTIONS.columns if 'FVC_LD' in col]
QR_Conf_cols = [col for col in PREDICTIONS.columns if 'Confidence_QR' in col]
LD_Conf_cols = [col for col in PREDICTIONS.columns if 'Confidence_LD' in col]
PREDICTIONS['QR_FVC_mean'] = PREDICTIONS[QR_FVC_cols].mean(axis = 1)
 PREDICTIONS['QR_Conf_mean'] = PREDICTIONS[QR_Conf_cols].mean(axis = 1)
 PREDICTIONS['LD_FVC_mean'] = PREDICTIONS[LD_FVC_cols].mean(axis = 1)
 PREDICTIONS['LD_Conf_mean'] = PREDICTIONS[LD_Conf_cols].mean(axis = 1)
 PREDICTIONS['QR_LD_FVC_mean'] = PREDICTIONS[['QR_FVC_mean','LD_FVC_mean']].mean(axis = 1)
 PREDICTIONS['QR_LD_Conf_mean'] = PREDICTIONS[['QR_Conf_mean','LD_Conf_mean']].mean(axis = 1 )<rename_columns>
columns = [i for i in data.columns]
dummies = pd.get_dummies(data,
columns=columns,
drop_first=True,
sparse=True)
 del data
to_submit = PREDICTIONS[['Patient_Week','QR_LD_FVC_mean', 'QR_LD_Conf_mean']]
to_submit.columns = ['Patient_Week','FVC','Confidence']
to_submit<save_to_csv>
train = dummies.iloc[:train.shape[0], :]
test = dummies.iloc[train.shape[0]:, :]
del dummies
to_submit.to_csv('submission.csv', index=False )<install_modules>
train = train.sparse.to_coo().tocsr()
test = test.sparse.to_coo().tocsr()
train = train.astype("float32")
 test = test.astype("float32" )
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index<set_options>
lr = LogisticRegression(C=0.1338,
solver="lbfgs",
tol=0.0003,
max_iter=5000)
 lr.fit(train, labels)
 lr_pred = lr.predict_proba(train)[:, 1]
score = roc_auc_score(labels, lr_pred)
 print("score: ", score )
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )<set_options>
submission["id"] = test_id
submission["target"] = lr.predict_proba(test)[:, 1]
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<load_from_csv>
submission.to_csv("submission.csv", index=False )
<concatenate><EOS> 
submission.to_csv("submission.csv", index=False )