plcass | 객실 등급 |
---|---|
survived | 생존 유무 |
sex | 성별 |
age | 나이 |
sibsp | 형제 혹은 부부의 수 |
parch | 부모 혹은 자녀의 수 |
fare | 지불한 요금 |
boat | 탈출을 했다면 탑승한 보트의 번호 |
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
tatanic = pd.read_excel('./data/titanic.xls')
tatanic.head()
# 생존자 비율의 파이그래프, autopic : 숫자를 보여줌, shoadow : 그림자, explode : 구분되게 조금 띄어놓음
# 1행에 2열 생성, ax는 subplot에서 반환받은거
f ,ax = plt.subplots(1, 2, figsize=(16, 8))
tatanic['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=(16, 8))
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 and Unservived')
plt.show()
# 경제력 대비 생존률
# crosstab : 첫 번째는 인덱스, 두번째는 컬럼으로 생성해줌, margins: 합계
pd.crosstab(titanic['pclass'], titanic['survived'], margins=True)
1등실의 생존률이 다른 두군데보다 매우 높음
그럼 1등실에는 여성이 많이 타고 있나?
grid = sns.FacetGrid(titanic, row='pclass', col='sex', heigt=3, aspect =1)
grid.map(plt.his, '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=3, aspect=1)
grid.map(plt.his, 'age', alpha=0.5, bins=20)
gird.add_legend();
titanicp['age_catg'] = pd.cut(titanic['age'], bins=[0, 7, 15, 30, 60, 100],
include_lowest=True,
labels = ['baby', 'teen', 'young', 'adult', 'old'])
plt.figure(figsize=(12, 4))
plt.subplot(131)
sns.barplot('pclass', 'survived', data=titanic)
plt.subplot(132)
sns.barplot('age_catg', 'survived', data=titanic)
plt.subplot(133)
sns.barplot('sex', 'survived', data=titanic)
# 신뢰구간
plt.subplots_adjus(top=1, bottom=0.1, left=0.1, right=1, hspace=0.5, wspace=0.5)
fig, axes = flt.subplots(nrows=1, ncols=2, figsize=(14, 6))
women = titanic[titanic['sex']=='female']
men = titanic[titanic['sex']=='men']
# bins가 다를 때 높이가 다르다고 다른거 아님, 구간에 맞춰서 합해야함
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')
import re
title = []
for idx, dataset in tatanic.itterrowsd():
tmp = dataset['name']
title.append(re.search("\,\s\w+(\s\w+)?\.", tmp).group()[2:-1])
# ,로 시작하고 한칸띄우고 어떤 글자들이 나오다가 단어가 몇개일지는 모르고 .로 마침
titanic['title'] = title
pd.crosstab(titanic['title'], titanic['sex'])
# 계급이 여러개라 이름을 합침, MLLe, Ms, Mme는 Miss랑 똑같은거라 이름 바꿈
titanic['title'] = titanic['title'].replace('Mlle', 'Miss')
titanic['title'] = titanic['title'].replace('Ms', 'Miss')
titanic['title'] = titanic['title'].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', 'survived']].groupby(['title'], as_index=False).mean()
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit(titanic['sex'])
# le.classes_ 하면 클래스 나옴
titanic['gender'] = le.transfor(titanic['sex'])
# 결측치는 그냥 패쓰
titanic = titanic[titanic['age'].notnull()]
titanic = titanic[titanic['fare'].notnull()]
# 상관관계
correlation_matrix = titanic.corr().round(1)
sns.heatmap(data=correlation_matrix, annot=True, cmap='bwr')
titanic.columns
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=42)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
dt = DecisionTreeClassifier(max_depth=4, random_state=42)
dt.fit(X_train, y_train)
pred = dt.predict(X_test)
print(accuracy_score(y_test, pred))
# 디카프리오와 윈슬릿의 생존가능 확률은?
import numpy as np
# 클래스 : [['pclass', "age", 'sibsp', 'parch', 'fare', 'gender']]
dicaprio = np.array([3, 18, 0, 0, 5, 1])
print('Dicaprio :', dt.predict_proba(dicaprio)[0][1])
# 윈슬릿은?
winslet = np.array([[1, 16, 1, 1, 100, 0]])
print('Winslet :', dt.predict_proba(winslet)[0][1])
윈슬릿은 100% !