์ฃผ์ : ํ์ดํ๋ ์น๊ฐ๋ค์ ์์กด๋ฅ ๊ณผ ์์ฑ๋ค๊ฐ์ ๊ด๊ณ ๋ถ์
ํ์ฌ ์งํ ์ํฉ
*Colab์ ์ฌ์ฉํด ์์ ํจ
[๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ]
ํ์ํ ๋ชจ๋ ๋ฐ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
#Colab์ ํ๊ตญ์ด ํฐํธ๋ฅผ ๋ถ๋ฌ์ด
import matplotlib.font_manager as fm
!apt-get update -qq
!apt-get install fonts-nanum* -qq
fe = fm.FontEntry(fname=r'/usr/share/fonts/truetype/nanum/NanumGothic.ttf', name='NanumGothic')
fm.fontManager.ttflist.insert(0, fe)
plt.rcParams.update({'font.size': 10, 'font.family': 'NanumGothic'})
1. ๋ฐ์ดํฐ ํ์ธ
df = pd.read_csv('train.csv')
df
- Survived - ์์กด์ ๋ฌด, target ๊ฐ. (0 = ์ฌ๋ง, 1 = ์์กด)
- Name - ํ์น๊ฐ ์ฑ๋ช
- Pclass - ํฐ์ผ ํด๋์ค. (1 = 1st, 2 = 2nd, 3 = 3rd)
- Sex - ์ฑ๋ณ
- Age - ๋์ด(์ธ)
- SibSp - ํจ๊ป ํ์นํ ํ์ ์๋งค, ๋ฐฐ์ฐ์ ์ ์ดํฉ
- Parch - ํจ๊ป ํ์นํ ๋ถ๋ชจ, ์๋ ์ ์ดํฉ
- Embarked - ํ์น ํญ๊ตฌ
- Fare - ํ์น ์๊ธ
- Ticket - ํฐ์ผ ๋๋ฒ
- Cabin - ๊ฐ์ค ๋๋ฒ
์ฌ๊ธฐ์ PassengerId๋ ์น๊ฐ์ ๋ฒํธ์ด๋ฏ๋ก ์์กด์ ๋ฌด์ ์ฐ๊ด์ฑ์ด ์์ด๋ณด์ ๋๋ค.
df.info()
#๋ฐ์ดํฐ ๋ชจ์ ํ์ธ
df.shape
#๊ฒฐ์ธก์น ํ์ธ
df.isnull().sum()
#null์ด ์ฐจ์งํ๋ ๋น์ค
df.isnull().mean()
Age, Cabin, Embarked ํญ๋ชฉ์ ๊ฒฐ์ธก์น๊ฐ ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
ํนํ Cabin์ ๊ฒฐ์ธก์น๊ฐ ์๋นํ ๋ง์ด ์กด์ฌํด ์ ์๋ฏธํ ์ปฌ๋ผ์ด ๋์ง ๋ชปํ ๊ฒ ๊ฐ์ ํด๋น ์ปฌ๋ผ์ ์ ๊ฑฐํ๊ฒ ์ต๋๋ค.
2. Cabin ๊ฒฐ์ธก์น ์ฒ๋ฆฌ
# Cabin์ด ์ ๊ฑฐ
new_df = df.copy()
new_df = new_df.drop(['Cabin'],axis=1) # ์ธ๋ก ์ด ์ ๊ฑฐ
์ด์ Age, Embarked ํญ๋ชฉ์ ๊ฒฐ์ธก์น๋ฅผ ์ฑ์๋ณด๊ฒ ์ต๋๋ค.
3. Age ๊ฒฐ์ธก์น ์ฒ๋ฆฌ
#Age ์๊ฐํ(1)(๊ฒฐ์ธก์น ์ ์ธ)
sns.boxplot(y='Age', data = new_df)
##Age ์๊ฐํ(2)(๊ฒฐ์ธก์น ์ ์ธ)
sns.displot(data=new_df, x='Age', kind='kde')
Age ๋ฐ์ดํฐ์ ๊ฐ์ด ๋ค์ํ๊ณ ํ๊ท ๊ฐ์ ์ฌ์ฉํ๊ธฐ์ ์ด์์น๊ฐ ๋ง์ต๋๋ค.
๋ฐ๋ผ์ ๊ฒฐ์ธก์น๋ฅผ ์ค์๊ฐ์ผ๋ก ๋์ฒดํ๊ฒ ์ต๋๋ค.
# Age ๊ฒฐ์ธก์น, ์ค์๊ฐ์ผ๋ก ๋์ฒด
age_median = np.nanmedian(new_df["Age"])
new_df['Age'].fillna(age_median,inplace=True)
4. Embarked ๊ฒฐ์ธก์น ์ฒ๋ฆฌ
# Embarked ์ด๋ฆ๋ณ ์น๊ฐ์
new_df['Embarked'].value_counts()
Null ๊ฐ์ด 2๊ฐ์ด๊ณ , 'S'์ ๊ฐ์ฅ ๋ง์ ํ์น๊ฐ์ด ์์ผ๋ฏ๋ก ๊ฐ๋จํ๊ฒ ์ต๋น๊ฐ์ธ "S"๋ก ๊ฒฐ์ธก์น๋ฅผ ์ฑ์ฐ๊ฒ ์ต๋๋ค.
# Embarked ๊ฒฐ์ธก์น, ์ต๋น๊ฐ S๋ก ์ฑ์ฐ๊ธฐ
new_df['Embarked'].fillna('S',inplace=True)
#๊ฒฐ์ธก์น ํ์ธ
new_df.isnull().sum()
[๋ฐ์ดํฐ ์๊ฐํ]
1. Survived
#์์กด ๋น์จ
fig, ax = plt.subplots(1, 2, figsize=(14,5))
labels = ['์ฌ๋ง', '์์กด']
colors=['gray','red']
new_df['Survived'].value_counts().plot.pie(ax=ax[0], explode=[0,0.1], shadow=True, autopct='%1.1f%%', labels=labels, colors=colors, title="์์กด๋น์จ")
ax[0].set(ylabel='')
plt.title('์์กด ๋น์จ')
survived_counts = new_df['Survived'].value_counts()
plt.bar(survived_counts.index, survived_counts.values, color=['gray', 'red'])
plt.title('์ฌ๋ง์ ๋ฐ ์์กด์ ์')
plt.xticks(survived_counts.index, labels)
plt.ylabel('Count')
plt.show()
์ฌ๋ง ๋น์จ์ 61.6%, ์์กด ๋น์จ์ 38.4%๋ก ์ฌ๋งํ ํ์น๊ฐ์ ์๊ฐ ๋ ๋ง์ ๊ฒ์ด ํ์ธ๋ฉ๋๋ค.
300๋ช
์ด์์ด ์์กดํ๊ณ , 600๋ช
๊ฐ๊น์ด ์ฌ๋งํ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค.
2. Survived๊ณผ ๋ค๋ฅธ ์์ฑ๊ณผ์ ๊ด๊ณ
2-1 Pclass
#Pclass ์๊ฐํ_01_ํด๋์ค ๋ณ ํ์น์ ๋ฐ ์์กด/์ฌ๋ง ์
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].set_title("ํด๋์ค๋ณ ํ์น์ \n", size=15)
sns.countplot(data=new_df, x='Pclass', ax=axes[0], alpha = 0.7, palette=['blue'])
axes[1].set_title("ํด๋์ค๋ณ ์์กด/์ฌ๋ง ์ \n", size=15)
sns.countplot(x="Pclass", hue="Survived", data=new_df, ax=axes[1], alpha = 0.7, palette= ['gray','red'])
axes[1].legend(labels = ['์ฌ๋ง', '์์กด'])
3๋ฑ์ ํ์น๊ฐ์ด ๊ฐ์ฅ ๋ง๊ณ , 3๋ฑ์ ํ์น๊ฐ์ ์ฌ๋ง ์ธ์์ ๋ํ ๊ฐ์ฅ ๋ง์ต๋๋ค.
์๋์ ์ผ๋ก 1๋ฑ์ ํ์น๊ฐ๋ค์ ๋ง์ด ์ด์๋จ์ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค.
#Pclass ์๊ฐํ_02_๋ฑ๊ธ๋ณ ์์กด ๋น์จ
fig, ax = plt.subplots(1,2,figsize=(10,4), constrained_layout=True)
colors=['royalblue','tomato','limegreen']
explode=[0.01,0.01,0.01]
labels = ['1๋ฑ์', '2๋ฑ์', '3๋ฑ์']
#Survived ๊ฐ 0์ธ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ Pie Plot
new_df[new_df['Survived'] == 0]['Pclass'].value_counts().sort_index().plot.pie(ax=ax[0], autopct='%1.1f%%',colors=colors,explode=explode,labels=labels)
ax[0].set(ylabel='', title='์ฌ๋ง ๋น์จ - Pclass')
#Survived ๊ฐ 1์ธ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ Pie Plot
new_df[new_df['Survived'] == 1]['Pclass'].value_counts().sort_index().plot.pie(ax=ax[1], autopct='%1.1f%%',colors=colors,startangle=45,explode=explode, labels=labels)
ax[1].set(ylabel='', title='์์กด ๋น์จ - Pclass')
plt.show()
#์ฌ๋งํ ํ์น๊ฐ ๊ทธ๋ํ์์ 3๋ฑ์์ ๋น์จ์ด 67.8%๋ก ์๋์ ์ผ๋ก ๋๋ค.
์ฌ๋งํ ํ์น๊ฐ ๊ทธ๋ํ์์ 3๋ฑ์์ ๋น์จ์ด 67.8%๋ก ์๋์ ์ผ๋ก ๋์ต๋๋ค.
2-2 Name
#2 Name_ํธ์นญ๋ง ๋ผ์ด ์๋ก์ด ์ด ์์ฑ
for i in range(len(new_df["Name"])):
tmp = new_df["Name"][i].split(',')[1][1:]
title = tmp.split('.')[0]
new_df.loc[i, 'Title'] = title
new_df['Title'].value_counts()
- Mr (Mister) : "์ข
์กฑ"์ด๋ "์ง์"๋ฅผ ๋ํ๋ด๋ฉฐ, ์ฑ์ธ ๋จ์ฑ์๊ฒ ์ฌ์ฉ๋๋ ์ผ๋ฐ์ ์ธ ํธ์นญ์
๋๋ค.
๊ฒฐํผ ์ฌ๋ถ์ ์๊ด์์ด ์ฌ์ฉ๋ฉ๋๋ค. - Mrs (Missus): ๊ฒฐํผํ ์ฌ์ฑ์๊ฒ ์ฌ์ฉ๋๋ ํธ์นญ์ผ๋ก, ๊ทธ๋
์ ๋จํธ์ ์ฑ์ ๋ฐ๋ผ ์ฌ์ฉ๋ฉ๋๋ค.
"Missus"๋ ๋ฐ์์ "๋ฏธ์์ฆ"๋ก ์๋ ค์ ธ ์์ต๋๋ค. - Ms : ๊ฒฐํผ ์ฌ๋ถ๋ฅผ ๋ํ๋ด์ง ์๋ ์ฌ์ฑ์๊ฒ ์ฌ์ฉ๋๋ ํธ์นญ์
๋๋ค. "๋ฏธ์ค"์ "๋ฏธ์ธ์ฆ" ์ฌ์ด์ ๋ฐ์์ผ๋ก,
์ฌ์ฑ์ด ์์ ์ ๊ฒฐํผ ์ํ๋ฅผ ๊ฐ์กฐํ๊ณ ์ถ์ง ์์ ๋ ์ฃผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. - Master: ์ฃผ๋ก ๋จ์ ์ด๋ฆฐ์ด์๊ฒ ์ฌ์ฉ๋๋ ํธ์นญ์ผ๋ก, ์ฑ์ธ์ด ๋๊ธฐ ์ ๊น์ง ์ฌ์ฉ๋ฉ๋๋ค.
- Dr : ๋ฐ์ฌ
- Rev : ์ ๋ถ๋ (=Mr)
- Mlle : ์ด๋ฆฐ ์ฌ์ ์์ด (Mademoiselle) (=Miss)
- Major : ๊ตฐ์ธ์ด๋ ๊ตฐ์ฌ์ ์ธ ๊ฒฝํ์ด ์๋ ์ฌ๋ (=Mr)
- Col : ์ค๋ น (=Mr)
- the Countess : ๋ฐฑ์ ๋ถ์ธ (=Mrs)
- Capt : ๊ตฐ ์ฅ๊ต (=Mr)
- Ms : Miss (=Miss)
- Sir : ๋จ์ฑ ๊ณต์ํ ํํ (=Mr)
- Lady : ์ฌ์ฑ ๊ณต์ํ ํํ (=Miss)
- Mme : Mrs ํ๋์ค์ด (=Mrs)
- Don : ์คํ์ธ์ด๋ก ์ง์ ๋์ ๋จ์ฑ (=Mr)
์ฌ๋ฌ ํธ์นญ๋ค์ ๋ค Mr, Mrs, Ms, Master๋ก ๋ค ์์ ํ๊ฒ ์ต๋๋ค.
์ฌ๊ธฐ์ ์ฑ๋ณ์ด ๋ช ํํ์ง ์์ ํธ์นญ์ธ "Dr"๋ง ๋ฐ๋ก ํ์ธํด ์๊ธฐ๋ก ๋ฐ๊ฟ์ฃผ๊ฒ ์ต๋๋ค.
new_df[new_df['Title']=='Dr']
#"Dr"์์ ์ฑ๋ณ์ด ์ฌ์ฑ์ธ ํ๋ง ์๊ธฐ๋ก "Miss"๋ก ๋ณ๊ฒฝ
new_df.loc[796,"Title"] = "Miss"
#ํ์ดํ ๋งตํ ์งํ
title_map = {"Mr":"Mr", "Miss":"Miss", "Mrs":"Mrs", "Master":"Master", "Dr":"Mr", "Rev":"Mr", "Mlle":"Miss",
"Major":"Mr", "Col":"Mr", "the Countess":"Mrs", "Capt":"Mr", "Ms":"Miss", "Sir":"Mr",
"sir":"Mr", "Lady":"Miss", "Mme":"Mrs", "Don":"Mr", "Jonkheer":"Mr"}
new_df['Title'] = new_df['Title'].map(title_map)
#Name ์๊ฐํ_01_ํธ์นญ๋ณ ์์กด/์ฌ๋ง ์
import matplotlib.patches as mpatches
survived = new_df[new_df['Survived'] == 1].groupby('Title').size()
dead = new_df[new_df['Survived'] == 0].groupby('Title').size()
labels = ['Mr', 'Master', 'Mrs', 'Miss']
survived_colors = ['blue', 'blue', 'red', 'red']
survived_ordered = survived.reindex(labels, fill_value=0)
dead_ordered = dead.reindex(labels, fill_value=0)
x = np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, survived_ordered, width, label='Survived', alpha=0.6, color=survived_colors)
rects2 = ax.bar(x + width/2, dead_ordered, width, label='Dead', color='gray')
handles = [mpatches.Patch(color='gray', label='Dead'),
mpatches.Patch(color='blue', label='Male_Survived'),
mpatches.Patch(color='red', label='Female_Survived')]
ax.legend(handles=handles)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_ylabel('Count')
ax.set_title('ํธ์นญ๋ณ ์์กด/์ฌ๋ง ์ \n')
fig.tight_layout()
plt.show()
์ฌ์ฑ์ ํธ์นญ์ธ Mrs/Miss ๋๋ค ์์กด์ ์๊ฐ ๋ง์ต๋๋ค.
Mr์ ์ฌ๋ง์ ์๊ฐ ์๋์ ์ผ๋ก ๋ง์ต๋๋ค.
Master์ ์ ๋ฐ ์ ๋์ ์์กด์จ์ ๋ณด์
๋๋ค.
2-3 Sex
#Sex ์๊ฐํ_01_ํ์น์ ๋ง๋ ๊ทธ๋ํ
plt.figure(figsize=(8, 6))
sns.countplot(data=new_df, x='Sex', alpha=0.7, palette= ['blue','red'])
plt.title('์ฑ๋ณ ํ์น์ ์')
plt.show()
๋จ์ฑ ํ์น์๊ฐ ์ฌ์ฑ ํ์น์ ์๋ณด๋ค ์ฝ 2๋ฐฐ ๋ ๋ง์ต๋๋ค.
#Sex ์๊ฐํ_02_์ฑ๋ณ๋ณ ์์กด/์ฌ๋ง ์
survived = new_df[new_df['Survived'] == 1].groupby('Sex').size()
dead = new_df[new_df['Survived'] == 0].groupby('Sex').size()
labels = ['Male', 'Female']
x = np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, survived[::-1], width, label='Survived', alpha=0.6, color=['blue', 'red']) # male ์์กด์๋ ํ๋์, female ์์กด์๋ ๋นจ๊ฐ์
rects2 = ax.bar(x + width/2, dead[::-1], width, label='Dead', color='gray')
handles = [mpatches.Patch(color='gray', label='Dead'),
mpatches.Patch(color='blue', label='Male_Survived'),
mpatches.Patch(color='red', label='Female_Survived')]
ax.legend(handles=handles)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_ylabel('Count')
ax.set_title('์ฑ๋ณ๋ณ ์์กด/์ฌ๋ง ์ \n')
fig.tight_layout()
plt.show()
๋จ์ฑ ํ์น์๊ฐ ์ฌ์ฑ ํ์น์ ์๋ณด๋ค ์ฝ 2๋ฐฐ ๋ ๋ง์ต๋๋ค.
์ฌ์ฑ ํ์น์์ ์์กด๋ฅ ์ด ๋จ์ฑ ํ์น์์ ์์กด๋ฅ ๋ณด๋ค ๋์๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
#์ฑ๋ณ ์๊ฐํ_03_์ฑ๋ณ ์์กด/์ฌ๋ง์ ์
fig, ax = plt.subplots(1,2,figsize=(10,6), constrained_layout=True)
labels = ['์ฌ์ฑ', '๋จ์ฑ']
colors=['tomato', 'royalblue']
explode=[0.01,0.01]
#Survived ๊ฐ 0์ธ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ Pie Plot
new_df[new_df['Survived'] == 0]['Sex'].value_counts().sort_index().plot.pie(ax=ax[0], autopct='%1.1f%%', labels=labels,colors=colors, explode=explode)
ax[0].set(ylabel='', title='์ฌ๋ง - Sex')
#Survived ๊ฐ 1์ธ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ Pie Plot
new_df[new_df['Survived'] == 1]['Sex'].value_counts().sort_index().plot.pie(ax=ax[1], autopct='%1.1f%%', labels=labels,colors=colors, explode=explode)
ax[1].set(ylabel='', title='์์กด - Sex')
plt.show()
์๊ทธ๋ํ๋ก ๋น์จ์ ํ์ธํ๋ฉด ๋ ์ฑ๋ณ์ ์ฌ๋ง๋ฅ ์ด ๋์ฑ ํ์ฐํ๊ฒ ์ฐจ์ด๊ฐ ๋ฉ๋๋ค.
2-3-2 Pclass์ Sex
#Pclass&Sex ์๊ฐํ
pd.crosstab([new_df.Sex,new_df.Survived],new_df.Pclass,margins=True).style.background_gradient(cmap='summer_r')
#Pclass&Sex ์๊ฐํ_2
sns.catplot(x='Pclass',y="Survived",hue='Sex',data=new_df,kind='point', palette={'female': 'red', 'male': 'blue'})
plt.title('Sex์ Pclass์ ์์กด๋ฅ ์๊ด ๊ด๊ณ\n' , fontsize=16)
plt.xlabel('Pclass', fontsize=12)
plt.ylabel('Survived', fontsize=12)
plt.show()
1๋ฑ๊ธ ๊ฐ์ค์ ์ฌ์ฑ์ด ์ ์ผ ์์กด์๊ฐ ๋ง์ ๊ฒ์ผ๋ก ํ์ธ๋ฉ๋๋ค.
๋ฐ๋๋ก 3๋ฑ๊ธ ๊ฐ์ค์ ๋จ์ฑ์ด ์ ์ผ ์ฌ๋ง์๊ฐ ๋ง์ต๋๋ค.
1๋ฑ์์ ์๋ ํ์น๊ฐ์ด ์ฑ๋ณ์ ์๊ด์์ด ๋ค๋ฅธ ํด๋์ค์ ๋นํด ์๋์ ์ผ๋ก ๋ง์ด ์์กดํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
ํด๋์ค์ ๊ด๊ณ์์ด ์ฌ์ฑ์ ์์กด๋ฅ ์ด ๋์ ๊ฒ์ด ํ์ธ๋ฉ๋๋ค.
2-4 Age
#Age_์๊ฐํ (stack, ํ์คํ ๊ทธ๋จ)
plt.figure(figsize=(8, 5))
sns.histplot(data=new_df, x='Age', hue='Survived', multiple='stack', kde=False, bins=20, palette={0: 'red', 1: 'blue'})
plt.title('Age vs Survived', fontsize=16, weight='bold')
plt.xlabel('Age', fontsize=12)
plt.ylabel('Count', fontsize=12)
plt.grid(True, which='major', linestyle='--', linewidth=0.5)
plt.show()
20-30๋ ์น๊ฐ์ด ๊ฐ์ฅ ๋ง์ต๋๋ค.
์ด๋ฆฐ ์น๊ฐ(0-10์ธ)์ ์์กด์จ์ด ๋์ต๋๋ค.
: ๊ทธ๋ํ ์ผ์ชฝ์์ ๋ณผ ์ ์๋ฏ์ด, 0-10์ธ ๊ตฌ๊ฐ์์ ํ๋์(์์กด์)์ด ๋นจ๊ฐ์(์ฌ๋ง์)๋ณด๋ค ๋ง์ ๊ฒ์ ์ ์ ์์ต๋๋ค. 30๋ ์ดํ ์์กด๋ฅ ๊ฐ์: 30๋ ์ดํ๋ถํฐ๋ ํ๋์ ๋ถ๋ถ์ด ์๋์ ์ผ๋ก ์ ์ด์ง๊ณ ๋นจ๊ฐ์์ด ์ง๋ฐฐ์ ์ธ ๋ชจ์ต์ ๋ณด์
๋๋ค. ๊ณ ๋ น ์น๊ฐ์ ์ฌ๋ง๋ฅ ๋์: 60๋ ์ด์์ ํ์น์๋ค์ ๋งค์ฐ ์ ์ง๋ง, ๋๋ถ๋ถ ์ฌ๋งํ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
#Age_์๊ฐํ2
age_range_survival_ratio = []
# 1์ด ๋ถํฐ ์์กด์จ ๊ตฌํ๊ธฐ
for i in range(1,80):
age_range_survival_ratio.append(new_df[new_df['Age'] < i]['Survived'].sum() / len(new_df[new_df['Age'] < i]['Survived']))
plt.figure(figsize=(7,7))
plt.plot(age_range_survival_ratio)
plt.title('Age vs Survival Rate')
plt.ylabel('Survived Rate')
plt.xlabel('Age')
plt.show()
ํ์ดํ๋ ์ฌ๊ณ ๋น์ ๋์ด๊ฐ ์ด๋ฆด์๋ก ์์กด ๊ฐ๋ฅ์ฑ์ด ๋์๊ณ , ํนํ ์ด๋ฆฐ์ด๋ค์ ์์กด์จ์ด ๋งค์ฐ ๋์๋ค๋ ๊ฒ์ ๋ณด์ฌ์ค๋๋ค.
2-5 Sibsp
pd.crosstab(new_df.SibSp, new_df.Survived, margins=True).style.background_gradient(cmap='summer_r')
sns.barplot(x='SibSp', y='Survived', data=new_df,palette='Blues_d').set_title('SibSp vs Survived')
plt.show()
๊ทธ๋ํ๋ฅผ ํตํด SibSp๊ฐ 1์ธ ๊ฒฝ์ฐ ์์กด์จ์ด 53.58%๋ก ๊ฐ์ฅ ๋๊ณ , ๊ฐ์กฑ ์๊ฐ ๋ง์์๋ก ์์กด์จ์ด ๋ฎ์์ง๋ ๊ฒฝํฅ์ ํ์ธํ ์ ์์ต๋๋ค.
2-6 parch
pd.crosstab(new_df.Parch, new_df.Survived).style.background_gradient(cmap='summer_r')
new_df[['Parch', 'Survived']].groupby(['Parch']).mean()
sns.barplot(x='Parch', y='Survived', data=new_df,palette='Purples_d').set_title('Parch vs Survived')
plt.show()
Parch๊ฐ 1์์ 3์ผ ๋ ์์กด๋ฅ ์ด ๊ฐ์ฅ ๋์ผ๋ฉฐ, Parch๊ฐ 0์ผ ๋๋ ์์กด๋ฅ ์ด ์ฝ 34%๋ก ์๋์ ์ผ๋ก ๋ฎ์์ง๋๋ค. Parch๊ฐ 4 ์ด์์ผ ๋ ์์กด๋ฅ ์ด ๊ธ๊ฒฉํ ๋จ์ด์ง๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค.
2-6-2 Sibsp์ parch
# ์๊ฐํ: SibSp vs Survived
plt.figure(figsize=(14, 6))
# SibSp ๋ง๋ ๊ทธ๋ํ
plt.subplot(1, 2, 1)
sns.barplot(data=sibsp_survived.reset_index(), x='SibSp', y='Survival Rate', palette='coolwarm')
plt.title('SibSp vs Survival Rate', fontsize=16)
plt.xlabel('SibSp (Number of Siblings/Spouses)', fontsize=12)
plt.ylabel('Survival Rate', fontsize=12)
plt.xticks(rotation=0)
plt.grid(axis='y')
# Parch ๋ง๋ ๊ทธ๋ํ
plt.subplot(1, 2, 2)
sns.barplot(data=parch_survived.reset_index(), x='Parch', y='Survival Rate', palette='coolwarm')
plt.title('Parch vs Survival Rate', fontsize=16)
plt.xlabel('Parch (Number of Parents/Children)', fontsize=12)
plt.ylabel('Survival Rate', fontsize=12)
plt.xticks(rotation=0)
plt.grid(axis='y')
plt.tight_layout()
plt.show()
SibSp(ํ์ /์๋งค ์)์ Parch(๋ถ๋ชจ/์๋ ์) ๊ฐ์ ๊ด๊ณ๋ ์ ์ฌํ ๊ฒฝํฅ์ ๋ณด์ ๋๋ค.
๋ ๋ณ์ ๋ชจ๋ ์์กด๋ฅ ์ ์ํฅ์ ๋ฏธ์น๋ฉฐ, ๊ฐ์กฑ๊ณผ ํจ๊ป ํ์นํ ์น๊ฐ์ ๊ตฌ์กฐ๋ ๊ฐ๋ฅ์ฑ์ด ๋์์ต๋๋ค.
SibSp๊ฐ 1~2์ผ ๋ ์์กด๋ฅ ์ด ๋์์ง๊ณ , 0์ผ ๊ฒฝ์ฐ ์์กด๋ฅ ์ด ๊ฐ์ํฉ๋๋ค. Parch๋ ๋น์ทํ๊ฒ, 1์์ 3์ผ ๋ ์์กด๋ฅ ์ด ๋์ง๋ง, 0์ผ ๋๋ ์ฝ 34%๋ก ๋ฎ์์ง๋ฉฐ, 4 ์ด์์ผ ๋๋ ์์กด๋ฅ ์ด ๊ธ๊ฒฉํ ๊ฐ์ํฉ๋๋ค.
2-7 Ticket
#Ticket ์๊ฐํ
ticket_survival = new_df.groupby('Ticket')['Survived'].mean().reset_index()
ticket_pivot = ticket_survival.pivot(index='Ticket', columns='Survived', values='Survived')
plt.figure(figsize=(12, 8))
sns.heatmap(ticket_pivot, cmap="YlGnBu", annot=True, fmt=".2f")
plt.title('Survival Rate by Ticket')
plt.xlabel('Ticket')
plt.ylabel('Survival Rate')
plt.show()
ํฐ์ผ์ ๋ฒํธ๊ฐ ๋๋ฌด ๋ง๊ณ ๋ค์ํด ์๊ฐํ๋ฅผ ํตํด ์์กด์จ๊ณผ์ ์๊ด๊ด๊ณ๋ฅผ ๋ถ์ํ๊ธฐ์ ์ ํฉํ ๋ณ์๋ ์๋๋ฏํฉ๋๋ค.
2-8 Fare
#Fare ์๊ฐํ_01
plt.figure(figsize=(10, 6))
sns.distplot(df[df['Survived'] == 1]['Fare'], label='Survived', hist=False, kde=True, color='blue')
sns.distplot(df[df['Survived'] == 0]['Fare'], label='dead', hist=False, kde=True, color='red')
plt.title('Survival by Fare')
plt.xlabel('Fare')
plt.ylabel('Density') #์์กด์,์ฌ๋ง์์ ๋ฐ๋ ํ์
plt.legend()
plt.show()
0~50 ์ฌ์ด์ Fare ๊ตฌ๊ฐ:
์ฌ๋ง์์ ์์กด์์ ๋ฐ๋๊ฐ ์ด ๊ตฌ๊ฐ์์ ๊ฐ์ฅ ๋์ต๋๋ค.
ํนํ ์๊ธ์ด ๋งค์ฐ ๋ฎ์ ๊ตฌ๊ฐ์์ ์ฌ๋ง์๊ฐ ๋ง์ด ์ง์ค๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์ฆ ์๊ธ์ด ๋ฎ์๋ 3๋ฑ์ ์น๊ฐ๋ค์ด ์ฃผ๋ก ์ฌ๋งํ์์ ์์ฌํฉ๋๋ค.
50~150 ์ฌ์ด์ Fare ๊ตฌ๊ฐ:
์์กด์์ ๋ฐ๋๊ฐ ์ฌ๋ง์๋ณด๋ค ์๋์ ์ผ๋ก ๋์์ง๋๋ค.
๋น๊ต์ ๋์ ๊ธ์ก์ ๋ธ ์น๊ฐ๋ค์ด ์์กดํ ํ๋ฅ ์ด ๋ ๋์์์ ์๋ฏธํฉ๋๋ค.
150 ์ด์์ Fare ๊ตฌ๊ฐ:
์์กด์์ ๋ฐ๋๊ฐ ๋งค์ฐ ์์ง๋ง ์ฌ์ ํ ๋ํ๋จ. 1๋ฑ์ ์น๊ฐ๋ค์ด ์์กดํ ๊ฐ๋ฅ์ฑ์ด ๋์์์ ๋ณด์ฌ์ค๋๋ค.
2-9 Embarked
Embarked ์๊ฐํ_01
plt.figure(figsize=(10, 6))
sns.barplot(x='Embarked', y='Survived', data=new_df)
plt.title('Survival Rate by Embarked')
plt.ylabel('Survival Rate')
plt.xlabel('Embarked')
plt.show()
Sํญ๊ตฌ, Qํญ๊ตฌ, Cํญ๊ตฌ ์์ผ๋ก ์์กด์จ์ด ๋์์ง๋๋ค.
2-9-2 Pclass& Embarked
Pclass&Embarked ์๊ฐํ
Pclass1 = new_df[new_df['Pclass']==1]['Embarked'].value_counts()
Pclass2 = new_df[new_df['Pclass']==2]['Embarked'].value_counts()
Pclass3 = new_df[new_df['Pclass']==3]['Embarked'].value_counts()
fig, ax = plt.subplots(figsize=(10,6))
df = pd.DataFrame([Pclass1, Pclass2, Pclass3])
df.index = ['1st class','2nd class','3rd class']
df.plot(kind='bar', stacked=True, ax=ax)
plt.xticks(rotation=45)
plt.xlabel('Class')
plt.ylabel('Count')
plt.title('Class Distirbution in Embarked')
plt.show()
์์กด์จ์ด ๊ฐ์ฅ ๋ฎ์ Sํญ๊ตฌ์ 3ํด๋์ค๊ฐ ๊ฐ์ฅ ๋ง์ด ๋ถํฌํฉ๋๋ค.
๋ฐ๋๋ก ์์กด์จ์ด ๊ฐ์ฅ ๋์ Cํญ๊ตฌ์ 1ํด๋์ค๊ฐ ๊ฐ์ฅ ๋ง์ด ๋ถํฌํฉ๋๋ค.
3. ๊ฐ ์์ฑ ๊ฐ์ ์๊ด๊ด๊ณ
train_heat=pd.read_csv("/content/train.csv")
train_heat['Sex'] = train_heat['Sex'].replace('male',0)
train_heat['Sex'] = train_heat['Sex'].replace('female',1)
train_heat['Embarked'] = train_heat['Embarked'].fillna("S")
train_heat.loc[train_heat.Embarked == 'C','Embarked']=0
train_heat.loc[train_heat.Embarked == 'Q','Embarked']=1
train_heat.loc[train_heat.Embarked == 'S','Embarked']=2
train_heat['Embarked'] = train_heat['Embarked'].astype('int64')
age_median = np.nanmedian(train_heat["Age"])
train_heat['Age'].fillna(age_median,inplace=True)
for i in range(len(train_heat["Name"])):
tmp = train_heat["Name"][i].split(',')[1][1:]
title = tmp.split('.')[0]
train_heat.loc[i, 'Title'] = title
train_heat['Title'].value_counts()
train_heat.loc[796,"Title"] = "Miss"
title_map = {"Mr":"Mr", "Miss":"Miss", "Mrs":"Mrs", "Master":"Master", "Dr":"Mr", "Rev":"Mr", "Mlle":"Miss",
"Major":"Mr", "Col":"Mr", "the Countess":"Mrs", "Capt":"Mr", "Ms":"Miss", "Sir":"Mr",
"sir":"Mr", "Lady":"Miss", "Mme":"Mrs", "Don":"Mr", "Jonkheer":"Mr"}
train_heat['Title'] = train_heat['Title'].map(title_map)
train_heat.loc[train_heat.Title == 'Mr','Title']=0
train_heat.loc[train_heat.Title == 'Miss','Title']=1
train_heat.loc[train_heat.Title == 'Mrs','Title']=2
train_heat.loc[train_heat.Title == 'Master','Title']=3
train_heat['Title'] = train_heat['Title'].astype('int64')
train_heat = train_heat.drop('PassengerId', axis=1)
def category_age(x):
if x < 10:
return 0
elif x < 20:
return 1
elif x < 30:
return 2
elif x < 40:
return 3
elif x < 50:
return 4
elif x < 60:
return 5
elif x < 70:
return 6
else:
return 7
train_heat['Age'] = train_heat['Age'].apply(category_age)
train_heat['Survived'] = pd.to_numeric(train_heat['Survived'], errors='coerce')
corr = train_heat.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('๊ฐ ์์ฑ ๊ฐ์ ์๊ด๊ด๊ณ\n')
plt.show()
๋์ ์๊ด๊ด๊ณ๋ฅผ ๊ฐ๋ ๊ฒ
Fare, Pclss
Title, Sex
Sex, Survived
Pclass, Survived
Title, Survived
Age, Pclass
Parch, SibSp
'๐ก WIDA > ํ๋ก์ ํธ ๋ณด๊ณ ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ WIDA ] 2ํ๋ _์ค๊ฐ ๋ฐํ์๋ฃ (1) | 2024.10.09 |
---|---|
[WIDA] 1ํ๋ ์กฐ ์ค๊ฐ๋ฐํ ppt (0) | 2024.10.09 |
[WIDA]2ํ๋ _2์ฐจ ๋ณด๊ณ ์ (0) | 2024.09.25 |
[WIDA] 3ํ๋ ์กฐ_2์ฐจ ๋ณด๊ณ ์ (0) | 2024.09.24 |
[WIDA] 2์กฐ_์ต์ข ๋ฐํ ์๋ฃ (0) | 2024.05.29 |