Kaggle competition: Mechanisms of Action (MoA) Prediction
Y =(df_train['winPlacePerc'].astype(float)).values
sum_id = df_test["Id"].values
df_train = df_train.drop(['Id','groupId','matchId','winPlacePerc'], axis = 1)
 df_test= df_test.drop(['Id','groupId','matchId'], axis = 1)
<save_to_csv>
train_features = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 cp_type_train = train_features['cp_type']
non_ctl_idx = train_features.loc[train_features['cp_type']!='ctl_vehicle'].index.to_list()
train_features = train_features.drop(['sig_id','cp_type','cp_dose','cp_time'],axis=1)
 train_targets_scored = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 train_targets_scored = train_targets_scored.drop('sig_id',axis=1)
 labels_train = train_targets_scored.values
test_features = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_features = test_features.drop(['sig_id','cp_dose','cp_time'],axis=1 )
LR = LinearRegression()
LR.fit(df_train, Y)
 X_train, X_val, y_train,y_val = train_test_split(df_train,Y,test_size=0.3, random_state=42)
 print('Accuracy on training：
',LR.score(X_train, y_train))
print('Accuracy on validation：
',LR.score(X_val, y_val))
print('LinearRegression Accuracy：
',LR.score(df_train, Y))
pred = LR.predict(df_test)
 pred = pd.DataFrame({'Id':sum_id, 'winPlacePerc':pred})
 pred.to_csv('pred_Linear.csv',index=None )<choose_model_class>
non_moa = train_features.iloc[labels_train.sum(axis=1)==0]
non_moa_stratifier = cp_type_train[labels_train.sum(axis=1)==0]
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
 model_lgb.fit(df_train, Y)
 lgb_pred = model_lgb.predict(df_test)
 lgb_pred[lgb_pred > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = lgb_pred
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("LGB.csv", index=False )<load_from_csv>
train_features = train_features.iloc[non_ctl_idx]
labels_train = labels_train[non_ctl_idx]
dtrain = xgb.DMatrix(df_train, label=Y)
 dtest = xgb.DMatrix(df_test)
 params = {'max_depth':7,
'eta':1,
'silent':1,
'objective':'reg:linear',
'eval_metric':'rmse',
'learning_rate':0.05
}
num_rounds = 50
xb = xgb.train(params, dtrain, num_rounds)
 y_pred_xgb = xb.predict(dtest)
 y_pred_xgb[y_pred_xgb > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = y_pred_xgb
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("XGB1.csv", index=False )<load_from_csv>
scaler = preprocessing.StandardScaler()
data_train = scaler.fit_transform(train_features)
 data_test = scaler.transform(test_features.drop('cp_type',axis=1))
non_moa = scaler.transform(non_moa )
pd.set_option('display.width', 1000)
 pd.set_option('display.max_rows', 200)
 pd.set_option('display.max_columns', 200)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
<count_unique_values>
n_features = data_train.shape[1]
n_labels = labels_train.shape[1]
n_train = data_train.shape[0]
n_test = data_test.shape[0]
df_train['Id'].nunique()<count_unique_values>
activation = 'swish'
encoding_dim = 1024
dropout = 0.01
non_moa_train, non_moa_test = train_test_split(non_moa,test_size=0.15)
 class Autoencoder(Model):
def __init__(self, n_features, encoding_dim):
super(Autoencoder, self ).__init__()
self.encoder = Sequential([
layers.BatchNormalization() ,
layers.Dropout(dropout),
layers.Dense(encoding_dim,activation=activation),
layers.BatchNormalization() ,
layers.Dropout(dropout),
layers.Dense(1.5*encoding_dim,activation=activation),
layers.BatchNormalization() ,
layers.Dropout(dropout),
layers.Dense(2*encoding_dim,activation=activation),
])
 self.decoder = Sequential([
layers.Dense(2*encoding_dim,activation=activation),
layers.BatchNormalization() ,
layers.Dropout(dropout),
layers.Dense(1.5*encoding_dim,activation=activation),
layers.BatchNormalization() ,
layers.Dropout(dropout),
layers.Dense(n_features)
 ])
 def call(self, x):
encoded = self.encoder(x)
 decoded = self.decoder(encoded)
 return decoded
autoencoder = Autoencoder(n_features,encoding_dim)
 autoencoder.compile(optimizer=optimizers.Adam(learning_rate=1E-4), loss='mse')
 ae_reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=5, min_lr=1E-5)
 ae_early_stopping = callbacks.EarlyStopping(monitor='val_loss', min_delta=1E-5, patience=32, restore_best_weights=True)
 hist = autoencoder.fit(non_moa_train,non_moa_train,batch_size=32, verbose=0, validation_data =(non_moa_test,non_moa_test), epochs=1024,
shuffle=True, callbacks=[ae_reduce_lr, ae_early_stopping] )
df_train['groupId'].nunique()<count_unique_values>
autoencoded_train = autoencoder.encoder(data_train ).numpy()
autoencoded_test = autoencoder.encoder(data_test ).numpy()
autoencoded_non_moa_test = autoencoder.encoder(non_moa_test ).numpy()
autoencoder_error = mean_squared_error(non_moa_test,autoencoder.decoder(autoencoded_non_moa_test ).numpy())
 print("Autoencoder reconstruction error for Non MoA is " + str(autoencoder_error))
moa_train = data_train[labels_train.sum(axis=1)!=0]
autoencoder_error = mean_squared_error(moa_train,autoencoder.decoder(autoencoder.encoder(moa_train ).numpy() ).numpy())
 print("Autoencoder reconstruction error for train data with MoA is " + str(autoencoder_error))
df_train['matchId'].nunique()<count_unique_values>
pca = PCA(n_components=768)
 pca.fit(non_moa)
 pca_train = pca.transform(data_train)
 pca_test = pca.transform(data_test)
 pca_error_1 = mean_squared_error(non_moa,pca.inverse_transform(pca.transform(non_moa)))
 print('PCA Reconstruction Error for Non MoA is ' + str(pca_error_1))
pca_error = mean_squared_error(moa_train,pca.inverse_transform(pca.transform(moa_train)))
 print('PCA Reconstruction Error for test dataset is ' + str(pca_error))
df_train['matchId'].nunique()<count_values>
autoencoder_error_train = mean_squared_error(data_train.T,autoencoder.decoder(autoencoder.encoder(data_train ).numpy() ).numpy().T, multioutput='raw_values')
 autoencoder_error_test = mean_squared_error(data_test.T,autoencoder.decoder(autoencoder.encoder(data_test ).numpy() ).numpy().T, multioutput='raw_values')
 pca_error_train = mean_squared_error(data_train.T,pca.inverse_transform(pca.transform(data_train)).T, multioutput='raw_values')
 pca_error_test = mean_squared_error(data_test.T,pca.inverse_transform(pca.transform(data_test)).T, multioutput='raw_values')
 data_train = np.concatenate(( data_train,pca_train,autoencoded_train,autoencoder_error_train[:,np.newaxis],pca_error_train[:,np.newaxis]),axis=1)
 data_test = np.concatenate(( data_test,pca_test,autoencoded_test,autoencoder_error_test[:,np.newaxis],pca_error_test[:,np.newaxis]),axis=1 )
print(df_train['revives'].value_counts() )<count_values>
pca_error_1 = mean_squared_error(non_moa_test.T,pca.inverse_transform(pca.transform(non_moa_test)).T, multioutput='raw_values')
 pca_error = mean_squared_error(moa_train.T,pca.inverse_transform(pca.transform(moa_train)).T, multioutput='raw_values')
 autoencoder_error_1 = mean_squared_error(non_moa_test.T,autoencoder.decoder(autoencoded_non_moa_test ).numpy().T, multioutput='raw_values')
 autoencoder_error = mean_squared_error(moa_train.T,autoencoder.decoder(autoencoder.encoder(moa_train ).numpy() ).numpy().T, multioutput='raw_values' )
print(df_train['teamKills'].value_counts())
<count_values>
n_labels = labels_train.shape[1]
n_features = data_train.shape[1]
n_train = data_train.shape[0]
n_test = data_test.shape[0]
p_min = 1*1E-3
p_max = 1-1*1E-3
def logloss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred,p_min,p_max)
 return -backend.mean(y_true*backend.log(y_pred)+(1-y_true)*backend.log(1-y_pred))
n_seeds = 6
np.random.seed(1)
 seeds = np.random.randint(0,100,size=n_seeds)
 n_folds = 5
y_pred = np.zeros(( n_test,n_labels))
oof = tf.constant(0.0)
 hists = []
for seed in seeds:
fold = 0
mskf = MultilabelStratifiedKFold(n_splits=n_folds,shuffle=True,random_state=seed)
 for train, test in mskf.split(data_train,labels_train):
X_train = data_train[train]
X_test = data_train[test]
y_train = labels_train[train]
y_test = labels_train[test]
model = Sequential()
model.add(layers.Dropout(0.6))
model.add(layers.Dense(1024))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.6))
model.add(layers.Dense(256))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.1))
model.add(layers.Dense(n_labels))
model.add(layers.Activation('sigmoid'))
model.compile(optimizer=optimizers.Adam(learning_rate=1*1E-2), loss=losses.BinaryCrossentropy(label_smoothing=0.001), metrics=logloss)
 reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_logloss', factor=0.2, patience=5, mode='min', min_lr=1E-5)
 early_stopping = callbacks.EarlyStopping(monitor='val_logloss', min_delta=1E-5, patience=15, mode='min',restore_best_weights=True)
 hist = model.fit(X_train,y_train, batch_size=128, epochs=192,verbose=0,validation_data =(X_test,y_test),callbacks=[reduce_lr, early_stopping])
 hists.append(hist)
 y_val = model.predict(X_test)
 oof += logloss(tf.constant(y_test,dtype=tf.float32),tf.constant(y_val,dtype=tf.float32)) /(n_folds*n_seeds)
 y_pred += model.predict(data_test)/(n_folds*n_seeds)
 fold += 1
print(df_train['vehicleDestroys'].value_counts() )<count_values>
sub = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 sub.iloc[:,1:] = np.clip(y_pred,p_min,p_max)
 sub.iloc[test_features['cp_type'] == 'ctl_vehicle',1:] = 0
sub.to_csv('submission.csv', index=False )