text_plain_output_3.png text_plain_output_2.png text_plain_output_1.png
from sklearn.model_selection import cross_val_score

from sklearn.naive_bayes import MultinomialNB

from sklearn.svm import LinearSVC

from sklearn.tree import DecisionTreeClassifier

import numpy as np # linear algebra

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

train_df = pd.read_csv('../input/train.csv')
train_df = train_df[pd.isnull(train_df['Age']) == False]
features = train_df.drop(['PassengerId', 'Survived', 'Name', 'Ticket'], axis=1)
labels = train_df['Survived']
n_samples = len(train_df)
n_features = len(features.columns)
n_survived = labels.value_counts()[1]
n_died = labels.value_counts()[0]

processed_features = pd.DataFrame(index=features.index)
for col, col_data in features.iteritems():
    if col == 'Sex':
        col_data = col_data.replace(['male', 'female'], [1, 0])
    if col == 'Embarked' or col == 'Cabin':
        col_data = pd.get_dummies(col_data, prefix=col)
    processed_features = processed_features.join(col_data)

from sklearn.linear_model import LogisticRegressionCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import cross_val_score
classifiers = [('Linear SVC', LinearSVC()), ('Decision Tree', DecisionTreeClassifier()), ('Multinomial NB', MultinomialNB())]
random_score = float(max(n_survived, n_died)) / float(n_samples)
for title, clf in classifiers:
    score = np.mean(cross_val_score(clf, processed_features, labels, cv=5))

scores = []
for max_depth in range(1, 10):
    clf = DecisionTreeClassifier(max_depth=max_depth)
    score = np.mean(cross_val_score(clf, processed_features, labels, cv=5))
    print('Max depth of {}: {:.4f}'.format(max_depth, score))
    scores.append((max_depth, score))