text_html_output_1.png
from sklearn import datasets, svm, cross_validation, tree, preprocessing, metrics

import numpy as np

import pandas as pd

import sklearn.ensemble as ske

titanic_df = pd.read_csv('../input/train.csv', dtype={'Age': np.float64})
test_df = pd.read_csv('../input/test.csv', dtype={'Age': np.float64})

titanic_df.groupby('Pclass').mean()

class_sex_grouping = titanic_df.groupby(['Pclass', 'Sex']).mean()

group_by_age = pd.cut(titanic_df['Age'], np.arange(0, 90, 10))
age_grouping = titanic_df.groupby(group_by_age).mean()

titanic_df.count()

titanic_df = titanic_df.drop(['Cabin'], axis=1)

titanic_df = titanic_df.dropna()

titanic_df.count()

def preprocess_titanic_df(df):
    processed_df = df.copy()
    le = preprocessing.LabelEncoder()
    processed_df.Sex = le.fit_transform(processed_df.Sex)
    processed_df.Embarked = le.fit_transform(processed_df.Embarked)
    processed_df = processed_df.drop(['Name', 'Ticket'], axis=1)
    return processed_df

processed_df = preprocess_titanic_df(titanic_df)
processed_df.count()
processed_df

X = processed_df.drop(['Survived'], axis=1).values
Y = processed_df['Survived'].values

clf_dt = tree.DecisionTreeClassifier(max_depth=10)

clf_dt.fit(X, Y)
clf_dt.score(x_test, y_test)

shuffle_validator = cross_validation.ShuffleSplit(len(X), n_iter=20, test_size=0.2, random_state=0)
def test_classifier(clf):
    scores = cross_validation.cross_val_score(clf, X, Y, cv=shuffle_validator)

clf_rf = ske.RandomForestClassifier(n_estimators=50)
test_classifier(clf_rf)

clf_gb = ske.GradientBoostingClassifier(n_estimators=50)
test_classifier(clf_gb)

eclf = ske.VotingClassifier([('dt', clf_dt), ('rf', clf_rf), ('gb', clf_gb)])
test_classifier(eclf)