text_plain_output_1.png
from sklearn import feature_selection

from sklearn import model_selection

from sklearn import tree

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

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

all_data = pd.concat((train.loc[:, 'Pclass':'Embarked'], test.loc[:, 'Pclass':'Embarked']))

all_data.Age = all_data.Age.fillna(all_data.Age.median())
all_data.Fare = all_data.Fare.fillna(all_data.Fare.median())
all_data.Embarked = all_data.Embarked.fillna(all_data.Embarked.mode()[0])

all_data['Title'] = all_data['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
stat_min = 10
title_names = all_data['Title'].value_counts() < stat_min
all_data['Title'] = all_data['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == True else x)
all_data = all_data.drop(['Name'], axis=1)
all_data = all_data.drop(['Ticket'], axis=1)
all_data = all_data.drop(['Cabin'], axis=1)

all_data = pd.get_dummies(all_data)

cv_split = model_selection.ShuffleSplit(n_splits=10, test_size=0.3, train_size=0.6, random_state=0)

train_cleared = all_data[:train.shape[0]]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train_cleared, train.Survived, random_state=0, test_size=0.1)
X_val = all_data[train.shape[0]:]

dtree = tree.DecisionTreeClassifier(random_state=0)
base_results = model_selection.cross_validate(dtree, train_cleared, train.Survived, cv=cv_split, return_train_score=True)
dtree.fit(train_cleared, train.Survived)
param_grid = {'criterion': ['gini', 'entropy'], 'max_depth': [2, 4, 6, 8, 10, None], 'random_state': [0]}
tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring='roc_auc', cv=cv_split, return_train_score=True)
tune_model.fit(train_cleared, train.Survived)

dtree_rfe = feature_selection.RFECV(dtree, step=1, scoring='accuracy', cv=cv_split)
dtree_rfe.fit(train_cleared, train.Survived)
X_rfe = train_cleared.columns.values[dtree_rfe.get_support()]
rfe_results = model_selection.cross_validate(dtree, train_cleared[X_rfe], train.Survived, cv=cv_split)
rfe_tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring='roc_auc', cv=cv_split)
rfe_tune_model.fit(train_cleared[X_rfe], train.Survived)

model = DecisionTreeClassifier(random_state=0, max_depth=5)
model.fit(X_train, y_train)
print('Train score: {:.3f}'.format(model.score(X_train, y_train)))
print('Test score: {:.3f}'.format(model.score(X_test, y_test)))
decision_tree_predicts_base = model.predict(X_val)
decision_tree_predicts_tuned_param = tune_model.predict(X_val)
decision_tree_predicts_tuned_param_rfe = rfe_tune_model.predict(X_val[X_rfe])