https://www.kaggle.com/c/titanic/data
데이터 살펴보기
# 라이브러리 불러오기
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# 데이터 불러오기, 동일경로
titanic_df = pd.read_csv("./train.csv")
# 상위 5개 데이터 확인
titanic_df.head(5)
# 각 column의 데이터 타입 확인
titanic_df.dtypes
->
PassengerId int64
Survived int64
Pclass int64
Name object
Sex object
Age float64
SibSp int64
Parch int64
Ticket object
Fare float64
Cabin object
Embarked object
dtype: object
.describe()
상관계수 확인
.corr()
Correlation is NOT Causation
상관성 : A up, B up, ...
인과성 : A -> B
상관관계로부터 인과성을 증명하긴 매우 어렵다. 구분해서 사용해야함!
결측치 확인
.isnull()
titanic_df.isnull().sum()
->
PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 177
SibSp 0
Parch 0
Ticket 0
Fare 0
Cabin 687
Embarked 2
survived Column
# 생존자, 사망자 명수는?
titanic_df['Survived'].sum()
-> 342
titanic_df['Survived'].value_counts()
->
0 549
1 342
# 생존자 수와 사망자 수를 Barplot으로 그려보기 sns.countplot()
sns.countplot(x ='Survived', data=titanic_df)
plt.show()
Pclass
# Pclass에 따른 인원 파악
titanic_df[['Pclass','Survived']]
->
Pclass Survived
0 3 0
1 1 1
2 3 1
3 1 1
4 3 0
... ... ...
886 2 0
887 1 1
888 3 0
889 1 1
890 3 0
#Pclass별 탑승인원
titanic_df[['Pclass','Survived']].groupby('Pclass').count()
->
Survived
Pclass
1 216
2 184
3 491
# 생존자 인원?
titanic_df[['Pclass','Survived']].groupby('Pclass').sum()
->
Survived
Pclass
1 136
2 87
3 119
# 생존률?
titanic_df[['Pclass','Survived']].groupby('Pclass').mean()
Survived
Pclass
1 0.629630
2 0.472826
3 0.242363
# 히트맵 활용
sns.heatmap(titanic_df[['Pclass','Survived']].groupby('Pclass').mean())
plt.show()
Sex
titanic_df[['Sex', 'Survived']]
->
Sex Survived
0 male 0
1 female 1
2 female 1
3 female 1
4 male 0
... ... ...
886 male 0
887 female 1
888 female 0
889 male 1
890 male 0
titanic_df.groupby(['Survived','Sex']).count()
titanic_df.groupby(['Survived','Sex'])['Survived'].count()
->
Survived Sex
0 female 81
male 468
1 female 233
male 109
# sns.catplot
sns.catplot(x='Sex', col='Survived', kind='count',data=titanic_df)
Age
titanic_df.describe()['Age']
->
count 714.000000
mean 29.699118
std 14.526497
min 0.420000
25% 20.125000
50% 28.000000
75% 38.000000
max 80.000000
# survived 1, 0과 Age의 경향성
# figure(도면) -> axis(틀) -> plot
fig, ax = plt.subplots(1, 1, figsize=(10,5))
sns.kdeplot(x=titanic_df[titanic_df.Survived==1]['Age'],ax=ax)
sns.kdeplot(x=titanic_df[titanic_df.Survived==0]['Age'],ax=ax)
plt.legend(['Survived', 'Dead'])
plt.show()
# subplots를 사용하지 않아도 그래프를 중복해서 그릴 수 있다.
plt.figure(figsize=(10,6))
sns.kdeplot(x=titanic_df[titanic_df.Survived==1]['Age'])
sns.kdeplot(x=titanic_df[titanic_df.Survived==0]['Age'])
plt.legend(['Survived', 'Dead'])
plt.show()
Appendix I.Pclass + Sex vs Survived
# 복합적인 요소에 대해 분석 진행
# 성별과 pclass, survived
# 각 점은 추정치
sns.catplot(x='Pclass', y='Survived', hue='Sex', kind='point',data=titanic_df)
plt.show()
Appendix II.Pclass + Age vs Survived
# Age graph with pclass
titanic_df['Age'][titanic_df.Pclass==1].plot(kind='kde')
titanic_df['Age'][titanic_df.Pclass==2].plot(kind='kde')
titanic_df['Age'][titanic_df.Pclass==3].plot(kind='kde')
plt.legend(['1st','2nd','3rd'])
plt.show()
함께보면 좋은 라이브러리
1.데이터 뽑아보기
2.데이터에 대한 가설을 세우기
3.가설을 검증하기 위한 증거를 찾아본다.
.groupby()
를 통해서 그룹화된 정보에 통계량을 도입하기.merge()
를 통해서 두개 이상의 dataframe을 합쳐본다