
pd.crosstab(titanic['Survived'], titanic['Sex'], normalize: 'columns')

'객실등급(Pclass)이 생존여부(Survived)에 관련이 있다' 라는 가설을 세운다.
# Pclass별 생존여부를 mosaic plot 으로
mosaic(titanic, [ 'Pclass','Survived'])
plt.axhline(1- titanic['Survived'].mean(), color = 'r') # 전체 평균선
plt.show()

만약, 생존 비율이 모두 평균선에 있는 것은 귀무가설이 참인 경우이다. (=대립가설이 차이가 없다)
근데 모자이크플롯을 보면 평균선과 차이가 있다. 그 말은 객실등급별 생존여부는 관련이 있는 데이터가 된다.
# stacked Bar
temp = pd.crosstab(titanic['Pclass'],
titanic['Survived'],
normalize = 'index')
print(temp)
temp.plot.bar(stacked=True)
plt.axhline(1-titanic['Survived'].mean(), color = 'r') # 전체 평균선
plt.show()
>
Survived 0 1
Pclass
1 0.370370 0.629630
2 0.527174 0.472826
3 0.757637 0.242363

성별과 생존 여부와 어떤 관련이 있을까?
- 귀무가설: 성별에 따라 생존여부에 아무런 관련이 없다.
- 대립가설: 성별에 따라 생존여부에 차이가 있다.
pd.crosstab(행, 열)
spst.chi2_contingency()
# 1) 먼저 교차표 집계
# normalize 옵션을 사용하면 안됨
table = pd.crosstab(titanic['Survived'], titanic['Pclass'])
print(table)
print('-' * 50)
# 2) 카이제곱검정
spst.chi2_contingency(table)
>
Pclass 1 2 3
Survived
0 80 97 372
1 136 87 119
--------------------------------------------------
Chi2ContingencyResult(statistic=102.88898875696056, # 카이제곱통계량
pvalue=4.549251711298793e-23, # p-value
dof=2, # 자유도
expected_freq=array([[133.09090909, 113.37373737, 302.53535354], # 기대빈도: 계산된 값
[ 82.90909091, 70.62626263, 188.46464646]]))
sns.kdeplot(x='Age', data = titanic, hue ='Survived')
# 생존자 사망자 합쳐서 100%
plt.show()

sns.kdeplot(x='Age', data = titanic, hue ='Survived',
common_norm = False) # 생존자 사망자 모두 1임
plt.show()

sns.kdeplot(x='Age', data = titanic, hue ='Survived'
, multiple = 'fill')
plt.axhline(titanic['Survived'].mean(), color = 'r')
plt.show()

sns.histplot(x='Age', data = titanic, bins = 16
, hue ='Survived', multiple = 'fill')
plt.axhline(titanic['Survived'].mean(), color = 'r')
plt.show()

sns.histplot(x='Age', data = titanic, hue = 'Survived')
# hue: Age에 대한 Survived를 겹쳐서 그려줘
plt.show()

이제 이 그림을 보고 변수를 구변하여 데이터를 분석하면 된다.
