๐๐๐ก๐๐๐๐๐
์นด์ด์ ๊ณฑ๊ฒ์ ๋ณธ๋ฌธ


๋ฒ์ฃผํ ๋ณ์
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ import
import pandas as pd
import scipy.stats as stats
# ํ์๋งค๋ฌผ์ฌ๋ถ์ ๋ฐ๋ฅธ ๋ค๋ฅธ ๋ฒ์ฃผํ ๋ณ์๋ค๊ณผ์ ์นด์ด์ ๊ณฑ ๊ฒ์ ์ํ
categorical_features = ['์ ๊ณตํ๋ซํผ', '์ฃผ์ฐจ๊ฐ๋ฅ์ฌ๋ถ', '๋ฐฉํฅ', '๋งค๋ฌผํ์ธ๋ฐฉ์']
def perform_chi_square_test_with_false_listing(train, feature):
"""ํ์๋งค๋ฌผ์ฌ๋ถ์ ํน์ ๋ฒ์ฃผํ ๋ณ์ ๊ฐ์ ๊ด๊ณ๋ฅผ ์นด์ด์ ๊ณฑ ๊ฒ์ ์ ํตํด ๋ถ์"""
# ํ์๋งค๋ฌผ์ฌ๋ถ vs ํน์ ์ปฌ๋ผ์ ๋ถํ ํ ์์ฑ
contingency_table = pd.crosstab(train['ํ์๋งค๋ฌผ์ฌ๋ถ'], train[feature])
# ์นด์ด์ ๊ณฑ ๊ฒ์ ์ํ
chi2_stat, p_value, dof, expected = stats.chi2_contingency(contingency_table)
print(f"\n๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs {feature}")
print(f"Chi-square Statistic: {chi2_stat:.4f}")
print(f"P-value: {p_value:.4f}")
print(f"Degrees of Freedom: {dof}")
# ์ ์์ฑ ํด์
alpha = 0.05
if p_value < alpha:
print(f"โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '{feature}' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.")
else:
print(f"โ ๊ท๋ฌด๊ฐ์ค ์ฑํ! '{feature}' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํ์ง ์์.")
# ๐น ํ์๋งค๋ฌผ ์ฌ๋ถ์ ๋ฐ๋ฅธ ์นด์ด์ ๊ณฑ ๊ฒ์ ์คํ
for feature in categorical_features:
perform_chi_square_test_with_false_listing(train, feature)
'''
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ์ ๊ณตํ๋ซํผ
Chi-square Statistic: 19.9206
P-value: 0.0686
Degrees of Freedom: 12
โ ๊ท๋ฌด๊ฐ์ค ์ฑํ! '์ ๊ณตํ๋ซํผ' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํ์ง ์์.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ์ฃผ์ฐจ๊ฐ๋ฅ์ฌ๋ถ
Chi-square Statistic: 37.9538
P-value: 0.0000
Degrees of Freedom: 1
โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '์ฃผ์ฐจ๊ฐ๋ฅ์ฌ๋ถ' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ๋ฐฉํฅ
Chi-square Statistic: 125.7040
P-value: 0.0000
Degrees of Freedom: 7
โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '๋ฐฉํฅ' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ๋งค๋ฌผํ์ธ๋ฐฉ์
Chi-square Statistic: 5.2510
P-value: 0.0724
Degrees of Freedom: 2
โ ๊ท๋ฌด๊ฐ์ค ์ฑํ! '๋งค๋ฌผํ์ธ๋ฐฉ์' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํ์ง ์์.
'''
์ฐ์ํ ๋ณ์
import pandas as pd
import scipy.stats as stats
import numpy as np
# ๐น ์ฐ์ํ ๋ณ์๋ฅผ ๋ฒ์ฃผํ์ผ๋ก ๋ณํ (qcut์ ์ฌ์ฉํ์ฌ ์๋ ๋ถํ )
def bin_numerical_feature(df, feature, bins=5):
"""์ฐ์ํ ๋ณ์๋ฅผ ์นด์ด์ ๊ณฑ ๊ฒ์ ์ ์ํ ๋ฒ์ฃผํ์ผ๋ก ๋ณํ"""
df[feature + '_bin'] = pd.qcut(df[feature], bins, duplicates='drop')
return df
# ๐น ํ์๋งค๋ฌผ ์ฌ๋ถ์ ๋ฐ๋ฅธ ์นด์ด์ ๊ณฑ ๊ฒ์ ์คํ
def perform_chi_square_test_with_false_listing(train, feature):
"""ํ์๋งค๋ฌผ์ฌ๋ถ์ ํน์ ๋ฒ์ฃผํ ๋ณ์ ๊ฐ์ ๊ด๊ณ๋ฅผ ์นด์ด์ ๊ณฑ ๊ฒ์ ์ ํตํด ๋ถ์"""
# ์ฐ์ํ ๋ณ์๋ binning ํ '_bin' ์ ๋ฏธ์ฌ๋ฅผ ๋ถ์ฌ ์ฒ๋ฆฌ
if train[feature].dtype != 'object':
train = bin_numerical_feature(train, feature)
feature = feature + '_bin' # ๋ณํ๋ ๋ฒ์ฃผํ ๋ณ์๋ฅผ ์ฌ์ฉ
# ํ์๋งค๋ฌผ์ฌ๋ถ vs ํน์ ์ปฌ๋ผ์ ๋ถํ ํ ์์ฑ
contingency_table = pd.crosstab(train['ํ์๋งค๋ฌผ์ฌ๋ถ'], train[feature])
# ์นด์ด์ ๊ณฑ ๊ฒ์ ์ํ
chi2_stat, p_value, dof, expected = stats.chi2_contingency(contingency_table)
print(f"\n๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs {feature}")
print(f"Chi-square Statistic: {chi2_stat:.4f}")
print(f"P-value: {p_value:.10f}") # ๋ ์ ๋ฐํ p-value ์ถ๋ ฅ
print(f"Degrees of Freedom: {dof}")
# ์ ์์ฑ ํด์
alpha = 0.05
if p_value < alpha:
print(f"โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '{feature}' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.")
else:
print(f"โ ๊ท๋ฌด๊ฐ์ค ์ฑํ! '{feature}' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํ์ง ์์.")
# ๐น ์ฌ์ฉํ ๋ณ์ ๋ชฉ๋ก (์ฐ์ํ ๋ณ์๋ ์๋ ๋ณํ๋จ)
categorical_features = ['๋ณด์ฆ๊ธ', '์์ธ', '์ ์ฉ๋ฉด์ ', '์ด์ธต']
# ๐น ๊ฒ์ ์คํ
for feature in categorical_features:
perform_chi_square_test_with_false_listing(train, feature)
'''
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ๋ณด์ฆ๊ธ_bin
Chi-square Statistic: 5.1292
P-value: 0.2742944260
Degrees of Freedom: 4
โ ๊ท๋ฌด๊ฐ์ค ์ฑํ! '๋ณด์ฆ๊ธ_bin' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํ์ง ์์.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ์์ธ_bin
Chi-square Statistic: 11.2643
P-value: 0.0237492058
Degrees of Freedom: 4
โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '์์ธ_bin' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ์ ์ฉ๋ฉด์ _bin
Chi-square Statistic: 13.7645
P-value: 0.0080857787
Degrees of Freedom: 4
โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '์ ์ฉ๋ฉด์ _bin' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.
๐ ์นด์ด์ ๊ณฑ ๊ฒ์ ๊ฒฐ๊ณผ: ํ์๋งค๋ฌผ์ฌ๋ถ vs ์ด์ธต_bin
Chi-square Statistic: 42.4263
P-value: 0.0000000136
Degrees of Freedom: 4
โ
๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ! '์ด์ธต_bin' ๋ณ์์ ํ์๋งค๋ฌผ์ฌ๋ถ ๊ฐ์ ๊ด๊ณ๊ฐ ์ ์๋ฏธํจ.
'''
'๋จธ์ ๋ฌ๋ ๐ฆพ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ตฐ์งํ๊ฐ ์งํ (0) | 2025.01.24 |
---|---|
๋น์ง๋ ํ์ต (0) | 2025.01.23 |
์ต๊ทผ์ ์ด์ (1) | 2025.01.23 |
์์ฌ๊ฒฐ์ ๋๋ฌด, ๋๋ค ํฌ๋ ์คํธ (0) | 2025.01.21 |
๋ก์ง์คํฑ ํ๊ท (0) | 2025.01.20 |