※ Chi-Square Test in Python
# 기본 사용법
> from scipy.stats import *
> chi2_contingency(observed, correction = True, lambda = None)
> chi2_contingency(...).statistic # 카이제곱 통계량
> chi2_contingency(...).pvalue # p-value
> chi2_contingency(...).dof # 자유도
> chi2_contingency(...).expected_freq # 기댓값
# 사용 예시
> df
# 교차 테이블 생성
> cross_table = pd.crosstab(df['성별'], df['만족도'])
> obs = cross_table.values
# 카이제곱 검정
> chi2_contingency(obs)
> chi2_contingency(obs).statistic # 카이제곱 통계량
> chi2_contingency(obs).pvalue # p-value
> chi2_contingency(obs).expected_freq # 기댓값
> new_df = pd.DataFrame(chi2_contingency(obs).expected_freq, index = cross_table.index, columns = cross_table.columns)
댓글남기기