Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<load_from_csv>
import numpy as np
import pandas as pd
import os
fi_depth_1 = pd.read_excel(xls, 'Interaction Depth 1')
 fi_depth_2 = pd.read_excel(xls, 'Interaction Depth 2')
 fi_depth_3 = pd.read_excel(xls, 'Interaction Depth 3' )<drop_column>
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_ids = list(submission['sig_id'].values)
 submission.shape
X_lr_total['int_1'] = X_lr_total['ord_0'] * X_lr_total['ord_3'] * X_lr_total['ord_5']
X_lr_total.drop(['bin_3', 'x1_Square', 'x2_Cat', 'x4_Oboe', 'day_5.0'], axis=1, inplace=True)
 X_lr = X_lr_total.iloc[:600000]
X_test_lr = X_lr_total.iloc[600000:1000000]<split>
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_ids = list(test['sig_id'].values)
 private_ids = list(set(test_ids)-set(public_ids))
len(private_ids )
start = datetime.now()
train_scores = []
val_scores = []
y_validations = []
y_predictions = []
for index_train, index_val in StratifiedKFold(n_splits=5, random_state=42, shuffle=True ).split(X_lr, y):
X_train = X_lr.iloc[index_train]
y_train = y.iloc[index_train]
X_valid = X_lr.iloc[index_val]
y_valid = y.iloc[index_val]
lr = LogisticRegression(random_state=0, max_iter=10000, fit_intercept=True, class_weight='balanced',
penalty='l2', C=0.4, verbose=0)
 scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
 X_valid_scaled = scaler.transform(X_valid)
 lr.fit(X_train_scaled, y_train)
 y_pred_train = lr.predict_proba(X_train_scaled)[:,1]
train_score = roc_auc_score(y_train, y_pred_train)
 train_scores.append(train_score)
 y_pred_valid = lr.predict_proba(X_valid_scaled)[:,1]
val_score = roc_auc_score(y_valid, y_pred_valid)
 val_scores.append(val_score)
 y_validations.append(y_valid)
 y_predictions.append(y_pred_valid)
 display_scores(train_scores, val_scores, y_validations, y_predictions)
 runtime = datetime.now() - start
print('
Runtime ', runtime )<train_model>
def make_filename(idx):
return glob.glob('.. /input/moa-1082020/' + str(idx)+ '.csv')[0]
def read_predictions(idx):
temp = pd.read_csv(make_filename(idx))
return temp
predictions = [read_predictions(idx)for idx in range(1,18)]
len(predictions )
lr = LogisticRegression(random_state=0, max_iter=10000, fit_intercept=True, class_weight='balanced', penalty='l2',
C=0.4, verbose=0)
 scaler = StandardScaler()
X_lr_scaled = scaler.fit_transform(X_lr)
 X_test_lr_scaled = scaler.transform(X_test_lr)
 lr.fit(X_lr_scaled, y )<predict_on_test>
y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0)
 y_pred.shape
<save_to_csv><EOS> 
submission = pd.DataFrame(index = public_ids + private_ids, columns=target_columns)
 submission.index.name = 'sig_id'
submission[:] = 0
submission.loc[y_pred.sig_id,:] = y_pred[target_columns].values
submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
 submission.head().T