일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- level 2
- oracle
- python3
- 빅데이터 분석 기사
- 오라클
- Kaggle
- R
- level 1
- 데이터 분석
- 머신러닝
- seaborn
- sklearn
- 알고리즘
- Python
- Numpy
- 빅분기
- 파이썬
- Oracel
- 실습
- 코딩테스트
- 프로그래머스
- 카카오
- pandas
- SQL
- 튜닝
- 실기
- matplotlib
- Today
- Total
라일락 꽃이 피는 날
[빅분기 실기] 앙상블 배깅 (Bagging) 본문
앙상블 배깅 (Bagging)
학습 데이터에 대해 여러 개의 부트스트랩 (Bootstrap) 데이터를 생성하고
각 데이터에 하나 또는 여러 알고리즘을 학습시킨 후
산출된 결과 중 투표 (Voting) 방식에 의해 최종 결과를 선정하는 알고리즘
[주요 하이퍼파라미터]
- n_estimators : 부트스트랩 데이터셋 수
Part 1. 분류 (Classification)
1. 분석 데이터 준비
import pandas as pd
# 암 예측 분류 데이터
data=pd.read_csv('breast-cancer-wisconsin.csv', encoding='utf-8')
X=data[data.columns[1:10]]
y=data[["Class"]]
1-2. train-test 데이터셋 나누기
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, stratify=y, random_state=42)
1-3. Min-Max 정규화
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)
2. 기본모델 적용
2-1. 훈련 데이터
from sklearn.svm import SVC
from sklearn.ensemble import BaggingClassifier
model = BaggingClassifier(base_estimator=SVC(), n_estimators=10, random_state=0)
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train) # 0.982421875
10개의 데이터셋에 SVC를 훈련시킨 10개 모델 결과를 종합한다.
① 오차행렬 (confusion matrix)
from sklearn.metrics import confusion_matrix
confusion_train=confusion_matrix(y_train, pred_train)
print("훈련데이터 오차행렬:\n", confusion_train)
정상(0) 중 4명이 오분류, 환자(1) 중 5명이 오분류되었다.
② 분류예측 레포트 (classification report)
from sklearn.metrics import classification_report
cfreport_train=classification_report(y_train, pred_train)
print("분류예측 레포트:\n", cfreport_train)
정밀도(precision) = 0.98, 재현율(recall) = 0.98
2-2. 테스트 데이터
pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test) # 0.9590643274853801
① 오차행렬 (confusion matrix)
confusion_test=confusion_matrix(y_test, pred_test)
print("테스트데이터 오차행렬:\n", confusion_test)
정상(0) 중 5명이 오분류, 환자(1) 중 2명이 오분류되었다.
② 분류예측 레포트 (classification report)
cfreport_test=classification_report(y_test, pred_test)
print("분류예측 레포트:\n", cfreport_test)
정밀도(precision) = 0.95, 재현율(recall) = 0.96
Part 2. 회귀 (Regression)
1. 분석 데이터 준비
# 주택 가격 데이터
data2=pd.read_csv('house_price.csv', encoding='utf-8')
X=data2[data2.columns[1:5]]
y=data2[["house_value"]]
1-2. train-test 데이터셋 나누기
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, random_state=42)
1-3. Min-Max 정규화
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)
2. 기본모델 적용
2-1. 훈련 데이터
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import BaggingRegressor
model = BaggingRegressor(base_estimator=KNeighborsRegressor(), n_estimators=10, random_state=0)
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train) # 0.6928982134381334
2-2. 테스트 데이터
pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test) # 0.5612676280708411
① RMSE (Root Mean Squared Error)
import numpy as np
from sklearn.metrics import mean_squared_error
MSE_train = mean_squared_error(y_train, pred_train)
MSE_test = mean_squared_error(y_test, pred_test)
print("훈련 데이터 RMSE:", np.sqrt(MSE_train))
print("테스트 데이터 RMSE:", np.sqrt(MSE_test))
'데이터 분석 > 빅데이터 분석 기사' 카테고리의 다른 글
[빅분기 실기] 앙상블 스태킹 (Stacking) (0) | 2022.06.19 |
---|---|
[빅분기 실기] 앙상블 부스팅 (Boosting) (0) | 2022.06.19 |
[빅분기 실기] 투표기반 앙상블 (Voting Ensemble) (0) | 2022.06.19 |
[빅분기 실기] 랜덤 포레스트 (Random Forest) (0) | 2022.06.18 |
[빅분기 실기] 의사결정나무 (Decision Tree) (0) | 2022.06.18 |