AI 헬스케어 첫번째 미니 프로젝트 “흡연 여부 데이터 분석을 통한 건강 인사이트 도출”(2)

다른 조의 예시인데 코드가 훨씬 단순하고 시각적으로 보기 좋아 가져왔다.

히스토그램을 사용했다. 


from scipy.stats import ttest_ind


graph = ['혈압', '고밀도지단백','저밀도지단백','중성 지방', '헤모글로빈']
for col in graph:
    if col != 'label':
        group0 = health_data[health_data['label'] == 0][col]
        group1 = health_data[health_data['label'] == 1][col]

        t_stat, p = ttest_ind(group0, group1)
        print(f"\n=== {col} ===")
        print(f"t-statistic: {t_stat:.4f}, p-value: {p:.6f}")
        print(f"비흡연자 mean: {group0.mean():.4f}, 흡연자 mean: {group1.mean():.4f}")
        print(f"비흡연자 std: {group0.std():.4f}, 흡연자 std: {group1.std():.4f}")
        print(f"비흡연자 size: {len(group0)}, 흡연자 size: {len(group1)}")

        plt.figure(figsize=(5,4))
        sns.histplot(group0, kde=True, label='비흡연자')
        sns.histplot(group1, kde=True, label='흡연자')
        plt.legend()
        plt.title(f'Histogram: {col}')
        plt.show()

댓글