Study_note(zb_data)/Machine Learning

μŠ€ν„°λ””λ…ΈνŠΈ (ML3)

KloudHyun 2023. 9. 21. 11:49

πŸ“Œ Titanic_EDA

πŸ”» Titanic μƒμ‘΄μœ¨ ν™•μΈν•˜κΈ°

import pandas as pd

titanic = pd.read_excel('../data/titanic.xls')
titanic.head()
import matplotlib.pyplot as plt
import seaborn as sns

# graph 2개 μ œμž‘
f, ax = plt.subplots(1, 2, figsize = (16, 8)); 

# autopct = %ν‘œμ‹œ
titanic['survived'].value_counts().plot.pie(ax=ax[0], autopct='%1.1f%%', shadow=True, explode = [0, 0.1]);
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()

πŸ”» 성별을 놓고 bar 차트둜 ν™•μΈν•΄λ³΄μž

# graph 2개 μ œμž‘
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 Unsurvived')
plt.show()

πŸ”» crosstab

# survived
pd.crosstab(titanic['pclass'], titanic['survived'], margins=True)

πŸ”» Class λ³„λ‘œ ꡬ뢄 해보기

- 3등싀에 20λŒ€ 남성이 λ§Žμ•˜λ‹€λŠ” 사싀을 μ•Œ 수 μžˆλ‹€.

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()

πŸ”»λ‚˜μ΄λ³„ 승객 ν˜„ν™©

- 아이와 2-30λŒ€κ°€ λ§Žμ•˜λ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.

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()

πŸ”»λ‚˜μ΄λ₯Ό μ •ν™•ν•˜κ²Œ κ΅¬λΆ„ν•΄λ³΄μž

- pandas의 cut을 ν™œμš©ν•˜μ—¬ label을 λΆ™μ—¬λ³΄μž

titanic['age_category'] = pd.cut(titanic['age'], bins=[0,7,15,30,60,100],
       include_lowest=True,
       labels=['baby', 'teen', 'young', 'adult', 'old'])
titanic.head()

- 어리고, μ—¬μ„±, 1등싀일 수둝 μƒμ‘΄ν•˜κΈ° μœ λ¦¬ν–ˆμ„κΉŒ?

plt.figure(figsize=(12, 4))
plt.subplot(131)
sns.barplot(x='pclass', y='survived', data=titanic)
plt.subplot(132)
sns.barplot(x='age_category', 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=20, 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 titanic.iterrows():
    tmp = dataset['name']
    title.append(re.search('\,\s\w+(\s\w+)?\.', tmp).group()[2:-1])

title
>>>>
['Miss',
 'Master',
 'Miss',
 'Mr',
 'Mrs',
 'Mr',
...

pd.crosstab(titanic['title'], titanic['sex'])

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')
titanic['title'] = titanic['title'].replace('Ms', 'Miss')
titanic['title'] = titanic['title'].replace('Mme', 'Mrs')

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')

πŸ”»group by둜 신뢄을 λ¬Άμ–΄μ„œ ν‘œν˜„ν•΄λ³΄μž

- κ·€μ‘±μ˜ 남성 --> μ ˆλ°˜λ„ 살아남지 λͺ»ν–ˆλ‹€.

titanic[['title', 'survived']].groupby(['title'], as_index=False).mean()