text_html_output_1.png
from bisect import bisect

from sklearn.ensemble import (RandomForestClassifier, AdaBoostClassifier,

from sklearn.model_selection import KFold

import numpy as np

import os

import pandas as pd

data_dir = '../input'
os.listdir(f'{data_dir}')

train_df_raw = pd.read_csv(f'{data_dir}/train.csv', low_memory=False)
test_df_raw = pd.read_csv(f'{data_dir}/test.csv', low_memory=False)
train_df_raw.sample(10)

train_df_raw.describe(include='all').T

test_df_raw.describe(include='all').T

train_df = train_df_raw
test_df = test_df_raw
all_df = [train_df, test_df]

category_maps = {}
def Categorify(df: pd.DataFrame, cat_names):
    for cat_name in cat_names:
        uniques = df[cat_name].unique()
        category_maps[cat_name] = {i: uniques[i] for i in range(len(uniques))}
        df[cat_name] = [np.where(uniques == key)[0][0] for key in df[cat_name]]

def Quantile(df: pd.DataFrame, quant_names, quants=[0.25, 0.5, 0.75]):
    for quant_name in quant_names:
        quant_col_name = f'{quant_name}_quantile'
        quant_vals = [np.quantile(df[quant_name], quant) for quant in quants]
        df[quant_col_name] = [bisect(quant_vals, x) for x in df[quant_name]]

SEED = 0
NFOLDS = 5
kf = KFold(n_splits=NFOLDS, random_state=SEED)
class SklearnHelper(object):
    def __init__(self, name, clf, seed=0, params=None):
        params['random_state'] = seed
        self.name = name
        self.clf = clf(**params)
    def train(self, x_train, y_train):
        self.clf.fit(x_train, y_train)
    def predict(self, x):
        return self.clf.predict(x)
    def fit(self, x, y):
        return self.clf.fit(x, y)
    def feature_importances(self, x, y):
        return self.clf.fit(x, y).feature_importances_

def get_oof(clf, x_train, y_train, x_test):
    oof_train = np.zeros((x_train.shape[0],))
    oof_test = np.zeros((x_test.shape[0],))
    oof_test_skf = np.empty((NFOLDS, x_test.shape[0]))
    for i, (train_index, test_index) in enumerate(kf.split(x_train, y_train)):
        x_tr = x_train[train_index]
        y_tr = y_train[train_index]
        x_te = x_train[test_index]
        clf.train(x_tr, y_tr)
        oof_train[test_index] = clf.predict(x_te)
        oof_test_skf[i, :] = clf.predict(x_test)
    oof_test[:] = oof_test_skf.mean(axis=0)
    return (oof_train.reshape(-1, 1), oof_test.reshape(-1, 1))

classifier_stack = [SklearnHelper('RandomForest', clf=RandomForestClassifier, seed=SEED, params={'n_jobs': -1, 'n_estimators': 500, 'max_depth': 6, 'min_samples_leaf': 2, 'max_features': 'sqrt', 'verbose': 0}), SklearnHelper('ExtraTrees', clf=ExtraTreesClassifier, seed=SEED, params={'n_jobs': -1, 'n_estimators': 500, 'max_depth': 8, 'min_samples_leaf': 2, 'verbose': 0}), SklearnHelper('AdaBoost', clf=AdaBoostClassifier, seed=SEED, params={'n_estimators': 500, 'learning_rate': 0.75}), SklearnHelper('GradientBoost', clf=GradientBoostingClassifier, seed=SEED, params={'n_estimators': 500, 'max_depth': 5, 'min_samples_leaf': 2, 'verbose': 0})]

dep_var = 'Survived'
drop_vars = ['PassengerId', 'Name', 'Ticket', 'Cabin']
x_train_df = train_df.drop(drop_vars, axis=1).drop(dep_var, axis=1)
x_train = x_train_df.values
y_train = train_df[dep_var].ravel()
x_test = test_df.drop(drop_vars, axis=1).values

oofs = {clf.name: get_oof(clf, x_train, y_train, x_test) for clf in classifier_stack}

base_predictions_train = pd.DataFrame({key: oofs[key][0].ravel() for key in oofs})
base_predictions_train.head()