ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [머신러닝][Kaggle][Python] 개인 건강 데이터를 이용한 심부전 예측 모델2
    머신러닝 | 딥러닝 2022. 5. 22. 21:46
    더보기

    [머신러닝][Kaggle][Python] 개인 건강 데이터를 이용한 심부전 예측 모델1 :

    https://ford.tistory.com/33

    출처 : https://www.kaggle.com/datasets/andrewmvd/heart-failure-clinical-data

     

    Heart Failure Prediction

    12 clinical features por predicting death events.

    www.kaggle.com

    Boxplot 계열의 그래프를 이용하여 범주별 통계 확인

    Boxplot 계열의 그래프는 각 범주별로 표시된 4분위 수와 이상치를 통해서 범주 사이의 차이를 확인할 수 있고,

    이를 통해 범주(독립 변수)와 종속 변수 사이 상관관계에 대한 가설 생성을 돕는다. 

    사망 - 박출계수

    sns.boxplot(x='DEATH_EVENT', y='ejection_fraction', data=df)

    # 바이올린 그래프를 이용하면 그래프에 데이터의 밀집 위치에 대한 정보도 포함할 수 있다.
    sns.violinplot(x='DEATH_EVENT', y='ejection_fraction', data=df)

    흡연 - 박출계수

    sns.boxplot(x='smoking', y='ejection_fraction', data=df)

    사망 - 박출계수 (세분 흡연)

    # swarmplot은 바이올린 그래프의 역할을 포함하고,
    # hue 파라미터를 이용해 더 세분화된 분포를 확인 할 수 있다.
    sns.swarmplot(x='DEATH_EVENT', y='platelets', hue='smoking', data=df)

    모델 생성

    표준화

    연속형 데이터에 표준화 진행

     

    # 연속형 입력 데이터, 범주형 입력 데이터, 출력 데이터로 구분
    X_num = df[['age', 'creatinine_phosphokinase','ejection_fraction', 'platelets','serum_creatinine', 'serum_sodium']]
    X_cat = df[['anaemia', 'diabetes', 'high_blood_pressure', 'sex', 'smoking']]
    y = df['DEATH_EVENT']
    
    # 수치형 입력 데이터를 전처리하고 입력 데이터 통합하기
    scaler = StandardScaler()
    scaler.fit(X_num)
    X_scaled = scaler.transform(X_num) 
    X_scaled = pd.DataFrame(data=X_scaled, index=X_num.index, columns=X_num.columns)
    X = pd.concat([X_scaled, X_cat], axis=1) # 행 방향으로 조인

    학습, 테스트 데이터 분리

    연속형 데이터에 표준화 진행

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)

    Logistic Regression 모델 생성

    모델 학습 및 평가

    model_lr = LogisticRegression(max_iter=1000) # 학습 1000번 진행
    model_lr.fit(X_train, y_train)
    model_lr.score(X_test, y_test) # 0.7555555555555555

    학습한 모델을 통한 예측

    pred = model_lr.predict(X_test) # X_test 데이터를 기반으로 y값 예측
    print(classification_report(y_test, pred)) # 실제 y_test값과 예측된 y값 비교 평가
    
    #               precision    recall  f1-score   support
    # 
    #            0       0.78      0.92      0.84        64
    #            1       0.64      0.35      0.45        26
    # 
    #     accuracy                           0.76        90
    #    macro avg       0.71      0.63      0.65        90
    # weighted avg       0.74      0.76      0.73        90

    XGBoost 모델 생성

    모델 학습 및 평가

    model_xgb = XGBClassifier()
    model_xgb.fit(X_train, y_train)
    model_xgb.score(X_test, y_test) # 0.8

    학습한 모델을 통한 예측

    pred = model_xgb.predict(X_test)
    print(classification_report(y_test, pred))
    #               precision    recall  f1-score   support
    # 
    #            0       0.84      0.89      0.86        64
    #            1       0.68      0.58      0.62        26
    # 
    #     accuracy                           0.80        90
    #    macro avg       0.76      0.73      0.74        90
    # weighted avg       0.79      0.80      0.79        90

    각 열(특성)의 중요도 시각화

    plt.bar(X.columns, model_xgb.feature_importances_)
    plt.xticks(rotation=90)
    plt.show()

    댓글

Designed by Tistory.