application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import RandomizedSearchCV

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

def prepareFeatuers(df):
    df['CabinPrefix'] = [str(cabinname)[0] for cabinname in df.Cabin]
    df.loc[df.Cabin.isnull(), 'CabinPrefix'] = 'None'
    df['CabinKnown'] = [value for value in df.Cabin.isnull()]
    df['TicketSplitLen'] = [len(t.split()) for t in df.Ticket]
    df['Sex_Ind'] = -1
    df.loc[df.Sex == 'female', 'Sex_Ind'] = 1
    df.loc[df.Sex == 'male', 'Sex_Ind'] = 2
    df['Age'] = df.Age.fillna(0)
    df['Fare'] = df.Fare.fillna(0)
    return df
train = prepareFeatuers(train)
test = prepareFeatuers(test)

cols = ['Pclass','Sex', 'SibSp', 'Parch', 'Embarked', 'CabinPrefix', 'TicketSplitLen', 'CabinKnown']
for col in cols:
    q = train.groupby(col).Survived.sum()
    # 
    t = train.groupby(col).Survived.sum() + train.groupby(col).Survived.count()
    fig, ax = plt.subplots()
    pos = [i for i,name in enumerate(q.index)]
    vals = [name for i,name in enumerate(q.index)]
    ax.barh(pos, t, color='r', label='died')
    ax.barh(pos, q, label='survived')
    ax.set_yticks(pos)
    ax.set_yticklabels(vals)
    ax.set_ylabel(col)
    ax.legend()

for col in ['Pclass', 'SibSp', 'Parch', 'TicketSplitLen', 'Sex', 'CabinPrefix']:
    unique_vals = np.array(train[col].unique())
    unique_vals.sort()
    for unique_value in unique_vals:
        for df in [train, test]:
            df.loc[df[col] == unique_value, f'{col} {unique_value}'] = 1
            df.loc[df[col] != unique_value, f'{col} {unique_value}'] = 0

feature_cols = ['Age', 'Fare', 'Pclass 1', 'Pclass 2', 'Pclass 3', 'SibSp', 'Parch', 'TicketSplitLen 1', 'TicketSplitLen 2', 'TicketSplitLen 3', 'Sex female', 'Sex male', 'CabinPrefix A', 'CabinPrefix B', 'CabinPrefix C', 'CabinPrefix D', 'CabinPrefix E', 'CabinPrefix F', 'CabinPrefix G', 'CabinPrefix None']

from sklearn.model_selection import train_test_split
X = np.array(train[feature_cols])
y = np.array(train.Survived)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train.shape
X_submission = np.array(test[feature_cols])

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
n_estimators = [int(x) for x in np.linspace(start=200, stop=2000, num=10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 110, num=11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
random_grid = {'n_estimators': n_estimators, 'max_features': max_features, 'max_depth': max_depth, 'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf, 'bootstrap': bootstrap}
rf_model = RandomForestClassifier()
rf_model.fit(X_train, y_train)
rf_random = RandomizedSearchCV(estimator=rf_model, param_distributions=random_grid, n_iter=100, cv=3, verbose=2, random_state=42, n_jobs=-1)
rf_random.fit(X_train, y_train)
best_random = rf_random.best_estimator_