text_plain_output_1.png
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 = pd.get_dummies(all_data)

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]:]

from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
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 = model.predict(X_val)