Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
def get_model() :
x = keras.layers.Input(shape=[X_train.shape[1]])
 a = keras.layers.Dense(units=512, input_shape=[X_train.shape[1]], name="dens_1" )(x)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 a = keras.layers.Dense(units=1024 )(a)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 a = keras.layers.Dense(units=512 )(a)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 output = keras.layers.Dense(units=199, activation='softmax', name = "real_output" )(a)
 out_reg = keras.layers.Dense(1, activation=None, name = "out_reg" )(a)
 model = keras.models.Model(inputs=[x], outputs=[output, out_reg])
 return model
def train_model(X_train, y_train, X_val, y_val, batch_size=64, epochs=200):
model = get_model()
model.compile(optimizer=Adam(learning_rate=0.005, beta_1=0.9, beta_2=0.99), loss=[crps, keras.losses.mae],
loss_weights=[1.0, 0.01])
 er = EarlyStopping(patience=10, min_delta=1e-4, restore_best_weights=True, monitor='val_loss')
 model.fit(X_train, y_train, epochs=epochs, callbacks=[er], validation_data=[X_val, y_val], batch_size=batch_size)
 return model<prepare_x_and_y>
LABELS = ["toxic"]
all_files = glob.glob(".. /input/jigsaw-data/*.csv" )
X_train, y_train = transform(train_df, player_features, game_features )<define_variables>
outs = [pd.read_csv(f, index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "m" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True )
feature_name = [f"{i}_{k}" for k in range(int(( X_train.shape[1]-len(game_features)) / len(player_features)))for i in player_features] + game_features<create_dataframe>
warnings.filterwarnings("ignore")
 rank = np.tril(concat_sub.iloc[:,1:].corr().values,-1)
 m =(rank>0 ).sum()
m_gmean, s = 0, 0
for n in range(min(rank.shape[0],m)) :
mx = np.unravel_index(rank.argmin() , rank.shape)
 w =(m-n)/(m+n/10)
 m_gmean += w*(np.log(concat_sub.iloc[:,mx[0]+1])+np.log(concat_sub.iloc[:,mx[1]+1])) /2
s += w
rank[mx] = 1
m_gmean = np.exp(m_gmean/s ).clip(0.0,1.0 )
X_train_df = pd.DataFrame(X_train, columns=feature_name )<prepare_x_and_y>
predict_list = []
predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9139.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9158.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9189.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9234.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9240.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9242.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9257.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9312.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9322.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9332.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9336.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9338.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9346.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9358.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9361.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9368.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9377.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9383.csv")[LABELS].values)
 predict_list.append(pd.read_csv(".. /input/jigsaw-data/submission-.9397.csv")[LABELS].values)
 predict_list_2 = []
predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9414-1.csv")[LABELS].values)
 predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9416.csv")[LABELS].values)
 predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9422.csv")[LABELS].values)
 predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9427.csv")[LABELS].values)
 predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9487.csv")[LABELS].values)
 predict_list_2.append(pd.read_csv(".. /input/jigsaw-data/submission-.9522.csv")[LABELS].values )
y_train_raw = train_df['Yards'].iloc[::22].values<train_model>
warnings.filterwarnings("ignore")
 print("Rank averaging on ", len(predict_list), " files")
 predictions = np.zeros_like(predict_list[0])
 for predict in predict_list:
for i in range(1):
predictions[:, i] = np.add(predictions[:, i], rankdata(predict[:, i])/predictions.shape[0])
 predictions = predictions /len(predict_list )
models = []
losses = []
batch_size = 64
epochs = 100
groups = train_df['GameId'].iloc[::22].values
tr_inds =(groups != 2018123011)&(groups != 2018123012)&(groups != 2018123013)&(groups != 2018123014)&(groups != 2018123015)
 val_inds = ~tr_inds
trainX, valX = X_train[tr_inds], X_train[val_inds]
trainY, valY = y_train[tr_inds], y_train[val_inds]
y_train_raw_tr = y_train_raw[tr_inds]
y_train_raw_val = y_train_raw[val_inds]
model = train_model(trainX, [trainY, y_train_raw_tr], valX, [valY, y_train_raw_val], batch_size, epochs)
 models.append(model)
 losses.append(model.history.history['val_real_output_loss'][-1])
 print("CROSS-VALID: ", np.mean(np.array(losses)) )<predict_on_test>
warnings.filterwarnings("ignore")
 print("Rank averaging on ", len(predict_list_2), " files")
 predictions_2 = np.zeros_like(predict_list_2[0])
 for predict in predict_list_2:
for i in range(1):
predictions_2[:, i] = np.add(predictions_2[:, i], rankdata(predict[:, i])/predictions_2.shape[0])
 predictions_2 = predictions_2 /len(predict_list_2 )
<feature_engineering><EOS> 
submission_1 = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 submission_1[LABELS] = predictions
submission_2 = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 submission_2[LABELS] = predictions_2
submission = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 submission[LABELS] = submission_1[LABELS]*.35 + submission_2[LABELS]*.65
submission.to_csv('submission.csv', index=False )