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

import numpy as np

import pandas as pd

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()

test_df.count()

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

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

titanic_df = titanic_df.dropna()

test_df = test_df.dropna()

titanic_df.count()

test_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

processed_test_df = preprocess_titanic_df(test_df)
processed_test_df.count()
processed_test_df

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

X_test = processed_test_df.values

clf_dt = tree.DecisionTreeClassifier(max_depth=10)

clf_dt.fit(X, Y)
Y_test = clf_dt.predict(X_test)
clf_dt.score(X_test, Y_test)