Kaggle competition: Predicting a Biological Response
<SOS> metric: LogLoss Kaggle data source: predicting-a-biological-response<feature_engineering>
import numpy as np
import pandas as pd
train_data['FareBin_5'] = pd.qcut(train_data['Fare'], 5)
 test_data['FareBin_5'] = pd.qcut(test_data['Fare'], 5 )<categorify>
def read_data(file_path):
with open(file_path)as f:
f.readline()
samples = []
for line in f:
line = line.strip().split(",")
 sample = [float(x)for x in line]
samples.append(sample)
 return samples
def load(train_path, test_path):
train = read_data(train_path)
 y_train = np.array([x[0] for x in train])
 X_train = np.array([x[1:] for x in train])
 X_test = np.array(read_data(test_path))
return X_train, y_train, X_test
train_path = '/kaggle/input/bioresponse/train.csv'
test_path = '/kaggle/input/bioresponse/test.csv'
X, y, X_submission = load(train_path, test_path)
 print(X.shape)
 print(X[0])
 print(y.shape)
 print(y[0])
 print(X_submission.shape)
 print(X_submission[0] )
label = LabelEncoder()
train_data['AgeBin_Code_5'] = label.fit_transform(train_data['AgeBin_5'])
 test_data['AgeBin_Code_5'] = label.fit_transform(test_data['AgeBin_5'])
 label = LabelEncoder()
train_data['FareBin_Code_5'] = label.fit_transform(train_data['FareBin_5'])
 test_data['FareBin_Code_5'] = label.fit_transform(test_data['FareBin_5'] )<data_type_conversions>
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.calibration import CalibratedClassifierCV
train_data['Title_Code'] = train_data.Title.map({'Mr':0, 'Miss':1, 'Mrs':2, 'Master':3, 'Others':4} ).astype('int')
 test_data['Title_Code'] = test_data.Title.map({'Mr':0, 'Miss':1, 'Mrs':2, 'Master':3, 'Others':4} ).astype('int' )<count_duplicates>
np.random.seed(0)
 n_folds = 10
shuffle = False
if shuffle:
idx = np.random.permutation(y.size)
 X = X[idx]
y = y[idx]
skf = list(StratifiedKFold(n_folds ).split(X, y))
clfs = [RandomForestClassifier(n_estimators=1000, n_jobs=-1, criterion='gini'),
RandomForestClassifier(n_estimators=1000, n_jobs=-1, criterion='entropy'),
ExtraTreesClassifier(n_estimators=1000, n_jobs=-1, criterion='gini'),
ExtraTreesClassifier(n_estimators=1000, n_jobs=-1, criterion='entropy'),
GradientBoostingClassifier(
 learning_rate=0.05, subsample=0.5, max_depth=6, n_estimators=600)]
clfs = [CalibratedClassifierCV(clf, method='isotonic', cv=StratifiedKFold(5))
for clf in clfs]
dataset_blend_train = np.zeros(( X.shape[0], len(clfs)))
 dataset_blend_test = np.zeros(( X_submission.shape[0], len(clfs)))
 for clf_idx, clf in enumerate(clfs):
print(clf_idx, clf)
 dataset_blend_test_cv = np.zeros(( X_submission.shape[0], len(skf)))
 for fold_idx,(train_idxs, test_idxs)in enumerate(skf):
print('Fold', fold_idx)
 X_train, y_train = X[train_idxs], y[train_idxs]
X_test, y_test = X[test_idxs], y[test_idxs]
clf.fit(X_train, y_train)
 dataset_blend_train[test_idxs, clf_idx] = clf.predict_proba(X_test)[:, 1]
dataset_blend_test_cv[:, fold_idx] = clf.predict_proba(X_submission)[:, 1]
dataset_blend_test[:, clf_idx] = dataset_blend_test_cv.mean(1 )
<feature_engineering><EOS> 
clf = LogisticRegression()
clf.fit(dataset_blend_train, y)
 y_submission = clf.predict_proba(dataset_blend_test)[:, 1]
y_submission =(y_submission - y_submission.min())/(y_submission.max() - y_submission.min())
 tmp = np.vstack([range(1, len(y_submission)+ 1), y_submission] ).T
np.savetxt(fname='submission.csv', X=tmp, fmt='%d,%0.9f',
header='MoleculeId,PredictedProbability', comments='' )