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