글 한눈에 보기

문제 설정
10.결정트리와 앙상블(XGBoost)에서 XGBoost 회귀, XGBoost 분류 흐름을 직접 따라가며 구현했습니다.
원본 구조
XGBoost 회귀 -> XGBoost 분류
데이터 맥락
특정 데이터셋 설명보다 XGBoost 회귀, XGBoost 분류 같은 실습 흐름을 직접 익히는 데 초점을 둔 노트입니다.
주요 장
XGBoost 회귀 · XGBoost 분류
구현 흐름
California Housing 불러오기 -> XGBoost 모델 학습 -> 회귀 성능 평가
자료
ipynb / md · 코드 12 · 실행 11
주요 스택
sklearn, xgboost, numpy

1. XGBoost 회귀

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
import xgboost as xgb
from sklearn.metrics import mean_squared_error
import numpy as np
data = fetch_california_housing()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model = xgb.XGBRegressor(
    n_estimators=100,
    learning_rate=0.1,
    max_depth=4
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
rmse

2. XGBoost 분류

import xgboost as xgb
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model = xgb.XGBClassifier(
    objective='multi:softmax',                  # 다중 클래스 분류
    num_class=3                                 # objective='multi:softmax'와 num_class는 같이 써야함
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
acc