import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
titanic_url = 'titanic.xls'
titanic = pd.read_excel(titanic_url)
titanic.head()
컬럼의 의미

|
pclass |
survived |
name |
sex |
age |
sibsp |
parch |
ticket |
fare |
cabin |
embarked |
boat |
body |
home.dest |
| 0 |
1 |
1 |
Allen, Miss. Elisabeth Walton |
female |
29.0000 |
0 |
0 |
24160 |
211.3375 |
B5 |
S |
2 |
NaN |
St Louis, MO |
| 1 |
1 |
1 |
Allison, Master. Hudson Trevor |
male |
0.9167 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
11 |
NaN |
Montreal, PQ / Chesterville, ON |
| 2 |
1 |
0 |
Allison, Miss. Helen Loraine |
female |
2.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
| 3 |
1 |
0 |
Allison, Mr. Hudson Joshua Creighton |
male |
30.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
135.0 |
Montreal, PQ / Chesterville, ON |
| 4 |
1 |
0 |
Allison, Mrs. Hudson J C (Bessie Waldo Daniels) |
female |
25.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
f, ax = plt.subplots(1,2,figsize = (12,5))
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')
plt.show()

f, ax = plt.subplots(1,2,figsize = (12,5))
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)
| survived |
0 |
1 |
All |
| pclass |
|
|
|
| 1 |
123 |
200 |
323 |
| 2 |
158 |
119 |
277 |
| 3 |
528 |
181 |
709 |
| All |
809 |
500 |
1309 |
grid = sns.FacetGrid(titanic, row='pclass', col = 'sex', height = 4, aspect=2)
grid.map(plt.hist,'age', alpha=0.8,bins = 20)
grid.add_legend();

import plotly.express as px
fig = px.histogram(titanic, x='age')
fig.show()
grid = sns.FacetGrid(titanic, row='pclass', col = 'survived', height = 4, aspect=2)
grid.map(plt.hist,'age', alpha=0.5,bins = 20)
grid.add_legend();

titanic['age_cut'] = pd.cut(titanic['age'], bins=[0,7,15,30,60,100],
include_lowest=True,
labels=['baby','teen','young','adult','old'])
titanic.head()
|
pclass |
survived |
name |
sex |
age |
sibsp |
parch |
ticket |
fare |
cabin |
embarked |
boat |
body |
home.dest |
age_cut |
| 0 |
1 |
1 |
Allen, Miss. Elisabeth Walton |
female |
29.0000 |
0 |
0 |
24160 |
211.3375 |
B5 |
S |
2 |
NaN |
St Louis, MO |
young |
| 1 |
1 |
1 |
Allison, Master. Hudson Trevor |
male |
0.9167 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
11 |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
| 2 |
1 |
0 |
Allison, Miss. Helen Loraine |
female |
2.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
| 3 |
1 |
0 |
Allison, Mr. Hudson Joshua Creighton |
male |
30.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
135.0 |
Montreal, PQ / Chesterville, ON |
young |
| 4 |
1 |
0 |
Allison, Mrs. Hudson J C (Bessie Waldo Daniels) |
female |
25.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
young |
plt.figure(figsize=(14,6))
plt.subplot(131)
sns.barplot( x='pclass',y = 'survived',data = titanic)
plt.subplot(132)
sns.barplot( x='age_cut',y = 'survived',data = titanic)
plt.subplot(133)
sns.barplot( x='sex',y = 'survived',data = titanic)
plt.show()

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(14,6))
women = titanic[titanic['sex']=='female']
men = titanic[titanic['sex']=='male']
ax = sns.distplot(women[women['survived']==1]['age'], bins=20, label = 'survived', ax=axes[0], kde=False)
ax = sns.distplot(women[women['survived']==0]['age'], bins=40, label = 'not survived', ax=axes[0], kde=False)
ax.legend(); ax.set_title('Female')
ax = sns.distplot(men[men['survived']==1]['age'], bins=18, label = 'survived', ax=axes[1], kde=False)
ax = sns.distplot(men[men['survived']==0]['age'], bins=40, label = 'not survived', ax=axes[1], kde=False)
ax.legend(); ax.set_title('Male')
Text(0.5, 1.0, 'Male')

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.head()
|
pclass |
survived |
name |
sex |
age |
sibsp |
parch |
ticket |
fare |
cabin |
embarked |
boat |
body |
home.dest |
age_cut |
title |
| 0 |
1 |
1 |
Allen, Miss. Elisabeth Walton |
female |
29.0000 |
0 |
0 |
24160 |
211.3375 |
B5 |
S |
2 |
NaN |
St Louis, MO |
young |
Miss |
| 1 |
1 |
1 |
Allison, Master. Hudson Trevor |
male |
0.9167 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
11 |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
Master |
| 2 |
1 |
0 |
Allison, Miss. Helen Loraine |
female |
2.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
Miss |
| 3 |
1 |
0 |
Allison, Mr. Hudson Joshua Creighton |
male |
30.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
135.0 |
Montreal, PQ / Chesterville, ON |
young |
Mr |
| 4 |
1 |
0 |
Allison, Mrs. Hudson J C (Bessie Waldo Daniels) |
female |
25.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
young |
Mrs |
pd.crosstab(titanic['title'], titanic['sex'])
| sex |
female |
male |
| title |
|
|
| Capt |
0 |
1 |
| Col |
0 |
4 |
| Don |
0 |
1 |
| Dona |
1 |
0 |
| Dr |
1 |
7 |
| Jonkheer |
0 |
1 |
| Lady |
1 |
0 |
| Major |
0 |
2 |
| Master |
0 |
61 |
| Miss |
260 |
0 |
| Mlle |
2 |
0 |
| Mme |
1 |
0 |
| Mr |
0 |
757 |
| Mrs |
197 |
0 |
| Ms |
2 |
0 |
| Rev |
0 |
8 |
| Sir |
0 |
1 |
| the Countess |
1 |
0 |
titanic['title'].unique()
array(['Miss', 'Master', 'Mr', 'Mrs', 'Col', 'Mme', 'Dr', 'Major', 'Capt',
'Lady', 'Sir', 'Mlle', 'Dona', 'Jonkheer', 'the Countess', 'Don',
'Rev', 'Ms'], dtype=object)
titanic['title'] = titanic['title'].replace('Mlle','Miss').replace('Ms','Miss').replace('Mme','Miss')
Rare_f = ['Dona','Lady','the Countess']
Rare_m = ['Capt','Col','Don','Major','Rev','Sir','Dr','Master','Jonkheer']
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()
array(['Miss', 'Rare_m', 'Mr', 'Mrs', 'Rare_f'], dtype=object)
titanic[['title','survived']].groupby(['title'],as_index=False).mean()
|
title |
survived |
| 0 |
Miss |
0.679245 |
| 1 |
Mr |
0.162483 |
| 2 |
Mrs |
0.786802 |
| 3 |
Rare_f |
1.000000 |
| 4 |
Rare_m |
0.448276 |
titanic['sex'].unique()
array(['female', 'male'], dtype=object)
import sklearn
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
titanic['gender'] = le.fit_transform(titanic['sex'])
titanic.head()
|
pclass |
survived |
name |
sex |
age |
sibsp |
parch |
ticket |
fare |
cabin |
embarked |
boat |
body |
home.dest |
age_cut |
title |
gender |
| 0 |
1 |
1 |
Allen, Miss. Elisabeth Walton |
female |
29.0000 |
0 |
0 |
24160 |
211.3375 |
B5 |
S |
2 |
NaN |
St Louis, MO |
young |
Miss |
0 |
| 1 |
1 |
1 |
Allison, Master. Hudson Trevor |
male |
0.9167 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
11 |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
Rare_m |
1 |
| 2 |
1 |
0 |
Allison, Miss. Helen Loraine |
female |
2.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
baby |
Miss |
0 |
| 3 |
1 |
0 |
Allison, Mr. Hudson Joshua Creighton |
male |
30.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
135.0 |
Montreal, PQ / Chesterville, ON |
young |
Mr |
1 |
| 4 |
1 |
0 |
Allison, Mrs. Hudson J C (Bessie Waldo Daniels) |
female |
25.0000 |
1 |
2 |
113781 |
151.5500 |
C22 C26 |
S |
NaN |
NaN |
Montreal, PQ / Chesterville, ON |
young |
Mrs |
0 |
titanic = titanic[titanic['age'].notnull()]
titanic = titanic[titanic['fare'].notnull()]
titanic.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1045 entries, 0 to 1308
Data columns (total 17 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 pclass 1045 non-null int64
1 survived 1045 non-null int64
2 name 1045 non-null object
3 sex 1045 non-null object
4 age 1045 non-null float64
5 sibsp 1045 non-null int64
6 parch 1045 non-null int64
7 ticket 1045 non-null object
8 fare 1045 non-null float64
9 cabin 272 non-null object
10 embarked 1043 non-null object
11 boat 417 non-null object
12 body 119 non-null float64
13 home.dest 685 non-null object
14 age_cut 1045 non-null category
15 title 1045 non-null object
16 gender 1045 non-null int32
dtypes: category(1), float64(3), int32(1), int64(4), object(8)
memory usage: 135.9+ KB
corr_matric = titanic.corr().round(1)
sns.heatmap(data=corr_matric,annot=True, cmap='bwr')

from sklearn.model_selection import train_test_split
X = titanic[['pclass','age','sibsp','parch','fare','gender']]
y = titanic['survived']
x_train,x_test,y_train,y_test = train_test_split(X,y, test_size = 0.8, 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))
0.7655502392344498
import numpy as np
dicaprio = np.array([[3,18,0,0,5,1]])
print('Decaprio: ',dt.predict_proba(dicaprio)[0,1])
Decaprio: 0.22950819672131148
winslet = np.array([[1,16,1,1,100,0]])
print('winslet: ',dt.predict_proba(winslet)[0,1])
winslet: 1.0