상세 컨텐츠

본문 제목

Chi-Square - For The Perfect Understanding

데이터 과학

by Taeyoon.Kim.DS 2024. 4. 5. 19:03

본문

The chi-square test is a method of analysis for two categorical variables.

For example, comparing preferred 스포츠 by 나이 범주.

머신러닝 데이터에서는 Yes or No라는 1,2 categorical 타겟 변수와, column의 feature가 서로 관련이 있는지에 대해서 알아보는 것이다. The null hypothesis (귀무가설) is rejected if the p-value <= 0.05

 

# Chi-Square
list_meaningful_column_by_chi = [] # 의미있는 컬럼들에 대해 empty list 생성.

for column_name in list_categorical_columns: # 전체 컬럼들에 대해서 하나씩 뽑아다가
  # 교차분석표로부터 chi square를 계산하는 과정. pd.crosstab을 통해서 target column과 각각의 column_name간의 교차분석표를 만든다.
  statistic, pvalue, _, _ = chi2_contingency(pd.crosstab(df[target_column], df[column_name]))
  if pvalue <= 0.05:
    list_meaningful_column_by_chi.append(column_name)
  print(column_name, statistic, pvalue)

print("all categorical columns : ", len(list_categorical_columns))
print("selected columns by chi : ", len(list_meaningful_column_by_chi), list_meaningful_column_by_chi)

 

 

'데이터 과학' 카테고리의 다른 글

[Leetcode] Algorithm (1)  (0) 2024.04.22
10 Ways to use ML with GCP  (0) 2024.04.05
YoloV5 vs Yolov8  (0) 2024.03.29
[MidJourney] A piano man  (0) 2024.03.19
[Terraform] State  (0) 2024.02.20

관련글 더보기