" 상관관계 "
두 변수 간에 어느 한 변수가 변하면 다른 변수가 함께 변하는 경향이 있는 관계
상관관계 ≠ 인과관계
"관련이 있다"는 의미, 반드시 "원인-결과(인과관계)"를 의미하지 않음
종류
" 상관계수 "
두 변수 간 선형적 관계의 강도와 방향을 수치로 나타낸 값
해석
종류
" 피어슨 상관계수 "
두 연속형 변수 간 선형(직선) 관계의 강도와 방향을 측정
특징
# 예시 데이터 생성
np.random.seed(0)
study_hours = np.random.rand(100) * 10
exam_scores = 3 * study_hours + np.random.randn(100) * 5
# 데이터프레임 생성
df = pd.DataFrame({'Study Hours': study_hours, 'Exam Scores': exam_scores})
# 피어슨 상관계수 계산
pearson_corr, _ = pearsonr(df['Study Hours'], df['Exam Scores'])
print(f"피어슨 상관계수: {pearson_corr}")
# 상관관계 히트맵 시각화
sns.heatmap(df.corr(), annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('pearson coefficient heatmap')
plt.show()
" 비모수 상관계수 "
데이터 분포에 대한 가정이 필요 없는 상관계수, 스피어만, 켄달 타우 등
활용
" 스피어만 상관계수 "
관측치를 순위로 바꾸어 두 변수의 순위 간의 일관성 측정, 단조(monotonic) 관계 감지에 적합
단조 관계 : 한 변수가 증가할 때 다른 변수도 계속 증가하거나 계속 감소하는 관계
특징
# 예시 데이터 생성
np.random.seed(0)
customer_satisfaction = np.random.rand(100)
repurchase_intent = 3 * customer_satisfaction + np.random.randn(100) * 0.5
# 데이터프레임 생성
df = pd.DataFrame({'Customer Satisfaction': customer_satisfaction, 'Repurchase Intent': repurchase_intent})
# 스피어만 상관계수 계산
spearman_corr, _ = spearmanr(df['Customer Satisfaction'], df['Repurchase Intent'])
print(f"스피어만 상관계수: {spearman_corr}")
# 상관관계 히트맵 시각화
sns.heatmap(df.corr(method='spearman'), annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('spearman coefficient heatmap')
plt.show()
" 켄달 타우 상관계수 "
관측치 쌍들 간 비율로 정의되는 순위상관 계수
특징
# 예시 데이터 생성
np.random.seed(0)
customer_satisfaction = np.random.rand(100)
repurchase_intent = 3 * customer_satisfaction + np.random.randn(100) * 0.5
# 데이터프레임 생성
df = pd.DataFrame({'Customer Satisfaction': customer_satisfaction, 'Repurchase Intent': repurchase_intent})
# 켄달의 타우 상관계수 계산
kendall_corr, _ = kendalltau(df['Customer Satisfaction'], df['Repurchase Intent'])
print(f"켄달의 타우 상관계수: {kendall_corr}")
# 상관관계 히트맵 시각화
sns.heatmap(df.corr(method='kendall'), annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('kendall coefficient heatmap')
plt.show()
" 상호정보량 "
상관의 비선형 일반화, 두 변수 X, Y가 서로 얼마나 정보를 공유하는지를 수치로 나타낸 것
특징
# 범주형 예제 데이터
X = np.array(['cat', 'dog', 'cat', 'cat', 'dog', 'dog', 'cat', 'dog', 'dog', 'cat'])
Y = np.array(['high', 'low', 'high', 'high', 'low', 'low', 'high', 'low', 'low', 'high'])
# 상호 정보량 계산
mi = mutual_info_score(X, Y)
print(f"Mutual Information (categorical): {mi:.3f}")

켄달 타우 범례에 ㅁ 킹받아.. 수정쫌여 그리구 파란색 없는데 왜 파란색 범례가 있어야 돼!!! 끼에에