데이터 정의
[R/실습] xgboost 모델을 이용한 위스콘신 유방암 데이터 분류분석
데이터 정의 - 사용 데이터 : wisc_bc_data.csv 컬럼명 의미 id 환자 식별 번호 diagnosis 양성 여부 (M = 악성, B = 양성) 각 세포에 대한 정보 radius 반경 (중심에서 외벽까지 거리들의 평균값) texture 질감 (Gr...
robinlovesyeon.tistory.com
위 링크 참고
랜덤포레스트 모델링
1) Import packages
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
2) 데이터 전처리
df = df.drop('id', axis=1)
df.head()
df['diagnosis'] = df['diagnosis'].apply(lambda x : 1 if x== "M" else 0)
x_train, x_test, y_train, y_test = train_test_split(df.iloc[:,1:], df['diagnosis'],test_size=0.3,random_state=11)
3) 랜덤포레스트 실행
model = RandomForestClassifier(n_estimators=100)
model.fit(x_train, y_train)
트리의 갯수를 나타내는 n_estimators을 100으로 설정하여 모델을 생성하였다.
4) 분류모델 평가
ypred1 = model.predict(x_test)
print("훈련 세트 정확도: {:.3f}".format(model.score(x_train,y_train)))
print("테스트 세트 정확도: {:.3f}".format(model.score(x_test,y_test)))
결과 :
훈련 세트 정확도 | 1.000 |
테스트 세트 정확도 | 0.977 |
97.7%정도의 정확도를 보여주는 것을 확인할 수 있다.
5) 랜덤포레스트의 주요 변수 보기
feat_labels = df.iloc[:,1:].columns
importances = model.feature_importances_
indices = np.argsort(importances)
for f in range(df.iloc[:,1:].shape[1]) :
print('%2d) %-*s %f' %
(f+1,30,feat_labels[indices[f]],importances[indices[f]]))
결과 :
perimeter_worst | 0.145364 |
area_worst | 0.099239 |
radius_worst | 0.098935 |
points_worst | 0.096283 |
radius_mean | 0.076717 |
area_mean | 0.066593 |
points_mean | 0.065500 |
perimeter_mean | 0.057750 |
concavity_mean | 0.056889 |
area_se | 0.029248 |
6) 랜덤포레스트의 주요 변수 시각화
plt.title('Feature Importance')
plt.bar(range(df.iloc[:,1:].shape[1]),importances[indices],align='center')
plt.xticks(range(df.iloc[:,1:].shape[1]), feat_labels[indices], rotation=90)
plt.xlim([-1,x_train.shape[1]])
plt.tight_layout()
plt.show()
결과:
![[Python/실습] 랜덤포레스트 모델을 이용한 위스콘신 유방암 데이터 분류분석 - undefined - 랜덤포레스트 모델링 [Python/실습] 랜덤포레스트 모델을 이용한 위스콘신 유방암 데이터 분류분석 - undefined - 랜덤포레스트 모델링](https://blog.kakaocdn.net/dn/t0WBK/btrU43ISY78/GWHQspTeNJXlZTZ2HAark0/img.png)
perimeter_worst가 가장 중요한 변수이고, area_worst, radius_worst, points_worst, radius_mean, area_mean순으로 나타난다.
'python > 실습(project)' 카테고리의 다른 글
[Python/딥러닝/실습] Keras를 이용한 다중 분류 분석(인공신경망) (0) | 2023.01.12 |
---|---|
[Python/딥러닝/실습] 로지스틱 회귀 분석 (0) | 2023.01.12 |
[Python/딥러닝/실습] 단순 선형회귀 분석 (0) | 2023.01.05 |
[Python/실습] 의사결정나무를 이용한 BostonHousing 예측분석 (0) | 2023.01.03 |
[python/실습] xgboost를 이용한 위스콘신 유방암 데이터 분류분석 (0) | 2022.12.20 |
댓글