Kaggle competition: Mechanisms of Action (MoA) Prediction
input_cols = ["Lat","Long","Date"]
output_cols = ["ConfirmedCases","Fatalities"]<feature_engineering>
X_train = pd.read_csv('.. /input/lish-moa/train_features.csv')
 y_train = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 X_test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
for i in range(df.shape[0]):
df["Date"][i] = df["Date"][i][:4] + df["Date"][i][5:7] + df["Date"][i][8:]
df["Date"][i] = int(df["Date"][i] )<feature_engineering>
def rank_gauss(df):
for col in df.columns:
transformer = QuantileTransformer(n_quantiles=100, random_state=0, output_distribution='normal')
 vec_len = len(df[col].values)
 raw_vec = df[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 df[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
return df
for i in range(test.shape[0]):
test["Date"][i] = test["Date"][i][:4] + test["Date"][i][5:7] + test["Date"][i][8:]
test["Date"][i] = int(test["Date"][i] )<prepare_x_and_y>
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
df = rank_gauss(df)
 return df
X = df[input_cols]
Y1 = df[output_cols[0]]
Y2 = df[output_cols[1]]<prepare_x_and_y>
y_train = y_train.loc[X_train['cp_type']=='trt_cp'].reset_index(drop=True)
 X_train = X_train.loc[X_train['cp_type']=='trt_cp'].reset_index(drop=True)
 train = preprocess(X_train)
 test = preprocess(X_test)
 del y_train['sig_id']
X_test = test[input_cols]<choose_model_class>
def create_model(num_columns):
model = keras.models.Sequential([
keras.layers.Input(num_columns),
keras.layers.BatchNormalization() ,
keras.layers.Dropout(rate=0.2),
tfa.layers.WeightNormalization(keras.layers.Dense(2048, activation='relu')) ,
keras.layers.BatchNormalization() ,
keras.layers.Dropout(rate=0.2),
tfa.layers.WeightNormalization(keras.layers.Dense(1024, activation='relu')) ,
keras.layers.BatchNormalization() ,
keras.layers.Dropout(rate=0.5),
tfa.layers.WeightNormalization(keras.layers.Dense(512, activation='relu')) ,
keras.layers.BatchNormalization() ,
keras.layers.Dropout(rate=0.5),
tfa.layers.WeightNormalization(keras.layers.Dense(206, activation='sigmoid'))
])
 model.compile(
 optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() ,sync_period=10),
loss='binary_crossentropy' 
)
 return model
sk_tree = DecisionTreeClassifier(criterion='entropy' )<train_model>
top_feats = [i for i in range(train.shape[1])]
sk_tree.fit(X,Y1 )<predict_on_test>
def exponential_delay_fn(epoch):
return 0.01 * 0.1 **(epoch/20 )
pred1 = sk_tree.predict(X_test )<train_model>
N_STARTS = 7
K_FOLD = 7
BATCH_NUM = 128
EPOCH_NUM = 35
tf.random.set_seed(1)
 ss.loc[:, y_train.columns] = 0
res = y_train.copy()
res.loc[:, y_train.columns] = 0
historys = dict()
for seed in range(N_STARTS):
for n,(tr, te)in enumerate(
 MultilabelStratifiedKFold(n_splits=K_FOLD,
random_state=seed, shuffle=True ).split(train, train)) :
print(f"--{train.values[tr].shape}--{train.values[te].shape}--")
 print(f"Seed: {seed}, Fold: {n}")
 model = create_model(len(top_feats))
checkpoint_path = f'repeated:{seed}_Fold:{n}.hdf5'
lr_scheduler = keras.callbacks.LearningRateScheduler(exponential_delay_fn)
 checkpoint_cb = keras.callbacks.ModelCheckpoint(checkpoint_path, save_best_only=True)
 early_stopping_cb = keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=0, mode='auto')
 history = model.fit(train.values[tr][:, top_feats],
y_train.values[tr],
validation_data=(train.values[te][:, top_feats], y_train.values[te]),
epochs=EPOCH_NUM,
batch_size=BATCH_NUM,
callbacks=[checkpoint_cb, early_stopping_cb, lr_scheduler],
verbose=2 
)
 historys[f'history_{seed+1}'] = history
model.load_weights(checkpoint_path)
 test_predict = model.predict(test.values[:, top_feats])
 val_predict = model.predict(train.values[te][:, top_feats])
 ss.loc[:, y_train.columns] += test_predict
res.loc[te, y_train.columns] += val_predict
print('')
 ss.loc[:, y_train.columns] /=(( n+1)* N_STARTS)
 res.loc[:, y_train.columns] /= N_STARTS
sk_tree.fit(X,Y2 )<predict_on_test>
def metric(y_true, y_pred):
metrics = []
for _target in y_train.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float)))
 return np.mean(metrics )
pred2 = sk_tree.predict(X_test )<save_to_csv>
print(f'OOF Metric :{metric(y_train, res)}' )
output = pd.DataFrame({ 'ForecastId' : ids, 'ConfirmedCases': pred1,'Fatalities':pred2 })
 output.to_csv('submission.csv', index=False )<load_from_csv>
ss.loc[test['cp_type']==1, y_train.columns] = 0
ss.to_csv('submission1.csv', index=False )
df = pd.read_csv("/kaggle/input/titanic/train.csv", index_col="PassengerId")
 rows, cols = df.shape
print(f"Original DataFrame has {rows} rows and {cols} columns")
 df.head()<categorify>
del historys
label_encoder = LabelEncoder()
df["Sex"] = label_encoder.fit_transform(df["Sex"])
 df["Family_Size"] = df["SibSp"] + df["Parch"] + 1
df["Fare_Per_Person"] = df["Fare"] / df["Family_Size"]
df.tail()<drop_column>
warnings.filterwarnings('ignore' )
set1 = df.copy()
set1 = set1[["Survived", "Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"]]
set1.dropna(axis=0, inplace=True)
 rows, cols = set1.shape
print(f"set1 DataFrame has {rows} rows and {cols} columns")
 set1.tail()<prepare_x_and_y>
X_train = pd.read_csv('.. /input/lish-moa/train_features.csv')
 y_train = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 X_test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 y_train = y_train.loc[X_train['cp_type']=='trt_cp'].reset_index(drop=True)
 X_train = X_train.loc[X_train['cp_type']=='trt_cp'].reset_index(drop=True)
 train = preprocess(X_train)
 test = preprocess(X_test)
 del y_train['sig_id']
X = train.to_numpy()
X_test = test.to_numpy()
y = y_train.to_numpy()
y = set1["Age"]
X = set1[["Survived", "Pclass", "Sex", "SibSp", "Parch", "Fare"]]<train_model>
classifier = MultiOutputClassifier(XGBClassifier(tree_method='gpu_hist'))
clf = Pipeline([('encode', CountEncoder(cols=[0, 2])) , 
('classify', classifier)
 ])
 params = {'classify__estimator__colsample_bytree': 0.6522,
'classify__estimator__gamma': 3.6975,
'classify__estimator__learning_rate': 0.0503,
'classify__estimator__max_delta_step': 2.0706,
'classify__estimator__max_depth': 10,
'classify__estimator__min_child_weight': 31.5800,
'classify__estimator__n_estimators': 166,
'classify__estimator__subsample': 0.8639
}
_ = clf.set_params(**params)
train_X, val_X, train_y, val_y = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0)
 print(f"Training features shape: {train_X.shape}")
 print(f"Training labels shape: {train_y.shape}")
 print(f"Testing features shape: {val_X.shape}")
 print(f"Testing labels shape: {val_y.shape}" )<choose_model_class>
N = 7
oof_preds = np.zeros(y.shape)
 test_preds = np.zeros(( test.shape[0], y.shape[1]))
oof_losses = []
kf = KFold(n_splits=N)
 for fn,(trn_idx, val_idx)in enumerate(kf.split(X, y)) :
print('Starting fold: ', fn)
 X_train, X_val = X[trn_idx], X[val_idx]
y_train, y_val = y[trn_idx], y[val_idx]
clf.fit(X_train, y_train)
 val_preds = clf.predict_proba(X_val)
 val_preds = np.array(val_preds)[:,:,1].T
oof_preds[val_idx] = val_preds
loss = log_loss(np.ravel(y_val), np.ravel(val_preds))
oof_losses.append(loss)
 preds = clf.predict_proba(X_test)
 preds = np.array(preds)[:,:,1].T
test_preds += preds / N
print(oof_losses)
 print('Mean OOF loss across folds', np.mean(oof_losses))
print('STD OOF loss across folds', np.std(oof_losses))
def get_mae1(max_leaf_nodes, train_X, val_X, train_y, val_y):
age_model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
 age_model.fit(train_X, train_y)
 val_predictions = age_model.predict(val_X)
 return mean_absolute_error(val_y, val_predictions)
 for max_leaf_nodes in [5, 10, 15, 20, 50, 100]:
mae = get_mae1(max_leaf_nodes, train_X, val_X, train_y, val_y)
 print(f"Max leaf nodes: {max_leaf_nodes} \t\t Mean Absolute Error: {mae}")
<train_model>
control_mask = train['cp_type']=='ctl_vehicle'
oof_preds[control_mask] = 0
print('OOF log loss: ', log_loss(np.ravel(y), np.ravel(oof_preds)) )
age_model = DecisionTreeRegressor(max_leaf_nodes=10, random_state=0)
 age_model.fit(X, y )<prepare_output>
sub = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
set2 = df.copy()
columns = ["Survived", "Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"]
set2 = set2.loc[set2["Age"].isnull() , columns]
rows, cols = set2.shape
print(f"set2 DataFrame has {rows} rows and {cols} columns")
 set2.tail()<predict_on_test>
sub.iloc[:,1:] = test_preds
sub.to_csv('submission2.csv', index=False )
X = set2[["Survived", "Pclass", "Sex", "SibSp", "Parch", "Fare"]]
set2["Age"] = age_model.predict(X)
 set2.head()<concatenate>
stack =(oof_preds + res)/2
set1 = set1.append(set2)
 rows, cols = set1.shape
print(f"set1 DataFrame has {rows} rows and {cols} columns")
 set1.head()<prepare_x_and_y>
stack.to_csv('submission.csv', index=False )