image_output_1.png
from sklearn.ensemble import GradientBoostingClassifier

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from subprocess import check_output

import matplotlib.pyplot as plt

import numpy as np # linear algebra

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

import numpy as np
import pandas as pd
from subprocess import check_output
train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')
train_num = train.shape[0]
full = train.append(test, ignore_index=True)
titanic = full[:train_num]

from sklearn.preprocessing import LabelEncoder
sex = pd.Series(np.where(full.Sex == 'male', 1, 0), name='Sex')
embarked = pd.get_dummies(full.Embarked, prefix='Embarked')
le = LabelEncoder()
imputed = pd.DataFrame()
imputed['Age'] = full.Age.fillna(full.Age.mean())
imputed['Fare'] = full.Fare.fillna(full.Fare.mean())
imputed['Parch'] = full.Parch.fillna(full.Parch.mean())
imputed['SibSp'] = full.SibSp.fillna(full.SibSp.mean())
title = pd.DataFrame()
title['Title'] = full['Name'].map(lambda name: name.split(',')[1].split('.')[0].strip())
Title_Dictionary = {'Capt': 'Officer', 'Col': 'Officer', 'Major': 'Officer', 'Jonkheer': 'Royalty', 'Don': 'Royalty', 'Sir': 'Royalty', 'Dr': 'Officer', 'Rev': 'Officer', 'the Countess': 'Royalty', 'Dona': 'Royalty', 'Mme': 'Mrs', 'Mlle': 'Miss', 'Ms': 'Mrs', 'Mr': 'Mr', 'Mrs': 'Mrs', 'Miss': 'Miss', 'Master': 'Master', 'Lady': 'Royalty'}
title['Title'] = title.Title.map(Title_Dictionary)
title = pd.get_dummies(title.Title)
cabin = pd.DataFrame()
cabin['Cabin'] = full.Cabin.fillna('U')
cabin['Cabin'] = cabin.Cabin.map(lambda c: c[0])
cabin = pd.get_dummies(cabin.Cabin, prefix='Cabin')

from sklearn.model_selection import train_test_split
full_X = pd.concat([imputed, embarked, cabin, sex], axis=1)
train_valid_X = full_X[:train_num]
train_valid_y = titanic.Survived
test_X = full_X[train_num:]
train_X, valid_X, train_y, valid_y = train_test_split(train_valid_X, train_valid_y, train_size=0.8)

from sklearn.ensemble import GradientBoostingClassifier
import matplotlib.pyplot as plt
train_X, valid_X, train_y, valid_y = train_test_split(train_valid_X, train_valid_y, train_size=0.8)
tree_num = 20
train_score_list = []
valid_score_list = []
x_range = range(1, tree_num)
for trees in x_range:
    model = GradientBoostingClassifier(n_estimators=trees, max_depth=10, min_samples_split=2, learning_rate=0.1, subsample=0.8, max_features=0.8)
    model.fit(train_X, train_y)
    train_score = model.score(train_X, train_y)
    valid_score = model.score(valid_X, valid_y)
    train_score_list.append(train_score)
    valid_score_list.append(valid_score)
plt.plot(x_range, train_score_list, '-r')
plt.plot(x_range, valid_score_list, '-b')
plt.show()