import pandas as pd
titanic_url = 'https://raw.githubusercontent.com/PinkWink/\
ML_tutorial/master/dataset/titanic.xls'
titanic = pd.read_excel(titanic_url)
titanic.head()
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(1, 2, figsize = (9,4))
titanic['survived'].value_counts().plot.pie(ax = ax[0], autopct = '%1.1f%%',\
shadow = True, explode = [0, 0.05])
ax[0].set_title('Pie plot - survived')
ax[0].set_ylabel('')
sns.countplot(x='survived', data=titanic, ax = ax[1])
ax[1].set_title('Count plot - survived')
38.2%가 생존(1)하였다.
1300명 가량이 탑승하였으며 500명 가량이 생존하였다.
fig, ax = plt.subplots(1, 2, figsize = (9,4))
sns.countplot(x='sex', data=titanic, ax = ax[0])
ax[0].set_title('Count of passengers of sex')
ax[0].set_ylabel('')
sns.countplot(x='sex', data=titanic, hue = 'survived', ax = ax[1])
ax[1].set_title('Sex : survived')
plt.show()
pd.crosstab(titanic['pclass'], titanic['survived'], margins = True)
#!pip install plotly_express
import plotly.express as px
grid = sns.FacetGrid(titanic, row = 'pclass', col = 'survived',\
height = 4, aspect = 2)
grid.map(plt.hist, 'age', alpha = 0.8, bins = 20)
grid.add_legend()
titanic['age_cat'] = pd.cut(titanic['age'], bins = [0,7,15,30,60,100],
include_lowest = True,
labels = ['baby','teen','young','adult','old'])
import re
title = []
for idx, dataset in titanic.iterrows():
tmp = dataset['name']
title.append(re.search('\,\s\w+(\s\w+)?\.', tmp).group()[2:-1])
titanic['title'] = title
titanic['title'] = titanic['title'].replace('Mlle', 'Miss')
titanic['title'] = titanic['title'].replace('Ms', 'Miss')
titanic['title'] = titanic['title'].replace('Mme', 'Mrs')
# 귀족 분류
Rare_f = ['Dona','Lady','the Countess']
Rare_m = ['Capt', 'Don', 'Col', 'Dr', 'Major', 'Rev', 'Sir',\
'Jonkheer', 'Rev','Master']
for each in Rare_f:
titanic['title'] = titanic['title'].replace(each, 'Rare_f')
for each in Rare_m:
titanic['title'] = titanic['title'].replace(each, 'Rare_m')
titanic['title'].unique()
titanic = titanic[titanic['age'].notnull()]
titanic = titanic[titanic['fare'].notnull()]
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit(titanic['sex'])
titanic['gender'] = le.transform(titanic['sex'])
# fit_transform으로 한번에 수행할 수도 있다.
titanic['age_cat_le']=le.fit_transform(titanic['age_cat'])
titanic['title_le'] = le.fit_transform(titanic['title'])
from sklearn.model_selection import train_test_split
# 필요한 컬럼만 추출
X = titanic[['pclass', 'age_cat_le','title_le', 'sibsp','parch','fare','gender']]
y = titanic['survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,\
random_state = 13)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
dt = DecisionTreeClassifier(max_depth= 4, random_state=13)
dt.fit(X_train, y_train)
pred = dt.predict(X_test)
print(accuracy_score(y_test, pred))