text_plain_output_1.png
from sklearn import cross_validation, metrics

from sklearn.ensemble import RandomForestClassifier

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

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

n_train = df_train.shape[0]
n_test = df_test.shape[0]
ratio = round(n_train / (n_train + n_test), 1)

df_train.sample(10)

df_titanic = df_train.drop(['Ticket', 'Cabin', 'Name'], axis=1)

df_titanic_na = df_titanic.dropna()

df_titanic_na.Sex = df_titanic.Sex.map({'female': 0, 'male': 1})
df_titanic_na.Embarked = df_titanic.Embarked.map({'C': 0, 'Q': 1, 'S': 2})

g = sns.distplot(df_titanic_na.Survived, color="red", hist_kws={"alpha": 0.3}, kde=None)
g.set_xticks([0,1])
g.autoscale()
g.set_xticklabels(["Dead", "Survived"])

corrmat = df_titanic_na[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch',
       'Fare', 'Embarked']].corr()

f, ax = plt.subplots(figsize=(10, 7))
sns.heatmap(corrmat, vmax=.8, square=True)

df_titanic_ml = df_titanic.copy()

df_titanic_ml.Embarked = df_titanic_ml.Embarked.fillna('Southampton')

df_titanic_ml[df_titanic_ml.Embarked.isnull()].shape

null_age = df_titanic_ml.Age.isnull()
df_titanic_ml[null_age].shape

df_titanic_ml = df_titanic_ml[np.invert(null_age)]

forest = RandomForestClassifier(n_estimators=1000, n_jobs=-1)
forest.fit(X_train, y_train)

def forest_metrics(X_test, y_test, clf):  
    
    f_preds = clf.predict_proba(X_test)[:, 1]
    f_fpr, f_tpr, _ = metrics.roc_curve(y_test, f_preds)

    fig, ax = plt.subplots()
    ax.plot(f_fpr, f_tpr)
    lims = [
        np.min([ax.get_xlim(), ax.get_ylim()]),  # min of both axes
        np.max([ax.get_xlim(), ax.get_ylim()]),  # max of both axes
    ]

    print("Model Accuracy: %.1f%%" % (clf.score(X_test,y_test) * 100))
    print ("Model ROC AUC: %.1f%%" % (metrics.roc_auc_score(y_test, f_preds)*100))
    print("ROC Curve")
    ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0)

forest_metrics(X_test, y_test, forest)