1. ๋ค์ด๊ฐ๋ฉฐ
์ด๋ฒ ๊ธ์์๋ XGBoost ํ๊ท ๋ชจ๋ธ์ ํ์ฉํ์ฌ ์์ธ์ ํ์ง์ ์์ธกํ๊ณ , ๋ค์ํ ๊ธฐ๋ฒ์ ์ ์ฉํ์ฌ ์ฑ๋ฅ์ ๋น๊ตํด๋ณด๊ฒ ๋ค.
๋จ์ํ XGBoost ๋ชจ๋ธ์ ๋จผ์ ํ์ตํ ํ,
- StandardScaler๋ฅผ ํตํ ๋ฐ์ดํฐ ๋ณํ,
- ์ด์์น ์ ๊ฑฐ,
- ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋
๋ฑ์ ์ ์ฉํ์ฌ ์ฑ๋ฅ ๋ณํ๋ฅผ ํ์ธํด ๋ณด๊ฒ ๋ค.
2. ๋ฐ์ดํฐ ์ค๋น ๋ฐ ๋ถํ
์์ธ ํ์ง ์์ธก์ ์ํด Red Wine Quality Dataset์ ์ฌ์ฉํ๋ฉฐ,
์
๋ ฅ ๋ณ์(X)์ ์ถ๋ ฅ ๋ณ์(y)๋ฅผ ๋ถ๋ฆฌํ ํ 80:20 ๋น์จ๋ก ํ๋ จ ๋ฐ ํ
์คํธ ๋ฐ์ดํฐ๋ก ๋๋๊ฒ ์
from sklearn.model_selection import train_test_split
# ์
๋ ฅ ๋ณ์(X)์ ํ๊ฒ ๋ณ์(y) ๋ถ๋ฆฌ
X = red_wine.drop(columns=['quality'])
y = red_wine['quality']
# 80% ํ์ต, 20% ํ
์คํธ ๋ฐ์ดํฐ ๋ถํ
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
3. XGBoost ๋ชจ๋ธ์ ํ์ฉํ ์์ธก
XGBoost๋ ๋ถ์คํ (Boosting) ๊ธฐ๋ฐ์ ๊ฐ๋ ฅํ ํ๊ท ๋ชจ๋ธ๋ก, ์์ธ์ ํ์ง์ ์์ธกํ๋ ๋ฐ ์ ํฉํจ
XGBoost ๋ชจ๋ธ ํ์ต ๋ฐ ์์ธก:
from xgboost import XGBRegressor
# XGBoost ๋ชจ๋ธ ์ ์ ๋ฐ ํ์ต
xgb_model = XGBRegressor(random_state=42)
xgb_model.fit(X_train, y_train)
# ์์ธก ์ํ
y_pred_xgb = xgb_model.predict(X_test)
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
# ๋ชจ๋ธ ํ๊ฐ ํจ์
def evaluate_model(y_true, y_pred, model_name="Model"):
mae = mean_absolute_error(y_true, y_pred)
mse = mean_squared_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)
print(f"{model_name} Performance:")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"R² Score: {r2:.4f}\n")
# ์ฑ๋ฅ ํ๊ฐ
evaluate_model(y_test, y_pred_xgb, "XGBoost (Baseline)")
# XGBoost (Baseline) Performance:
# Mean Absolute Error (MAE): 0.4175
# Mean Squared Error (MSE): 0.3513
# R² Score: 0.4625
# Actual Quality Predicted Quality
# 803 6 5.230904
# 124 5 5.347796
# 350 6 5.213949
# 682 5 5.278363
# 1326 6 5.996580
# 976 5 5.008490
# 1493 5 5.004802
# 706 5 5.094469
# 613 5 6.004089
# 1587 6 5.787866
๋ชจ๋ธ ํ๊ฐ ๊ฒฐ๊ณผ
Metric | Score |
MAE | 0.4175 |
MSE | 0.3513 |
R² Score | 0.4625 |
4. StandardScaler๋ฅผ ํ์ฉํ ๋ฐ์ดํฐ ๋ณํ ํ ์ฑ๋ฅ ๋น๊ต
์ค์ผ์ผ๋ง์ด ํ์ํ ์ด์ ?
- XGBoost๋ ์ผ๋ฐ์ ์ผ๋ก ์ ๊ทํ ์์ด๋ ์ ์๋ํ์ง๋ง,
- ๋ฐ์ดํฐ ์ค์ผ์ผ์ด ํฐ ๊ฒฝ์ฐ ๋ชจ๋ธ ์๋ ด ์๋ ๊ฐ์ ๋ฐ ์ฑ๋ฅ ํฅ์์ ๊ธฐ๋ํ ์ ์์
StandardScaler ์ ์ฉ ํ XGBoost ํ์ต
from sklearn.preprocessing import StandardScaler
# ๋ฐ์ดํฐ ์ค์ผ์ผ๋ง ์ ์ฉ
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# XGBoost ๋ชจ๋ธ ์ฌํ์ต (์ค์ผ์ผ๋ง ์ ์ฉ)
xgb_model_scaled = XGBRegressor(random_state=42)
xgb_model_scaled.fit(X_train_scaled, y_train)
# ์์ธก ๋ฐ ์ฑ๋ฅ ํ๊ฐ
y_pred_xgb_scaled = xgb_model_scaled.predict(X_test_scaled)
evaluate_model(y_test, y_pred_xgb_scaled, "XGBoost (Scaled Data)")
# XGBoost (Scaled Data) Performance:
# Mean Absolute Error (MAE): 0.4175
# Mean Squared Error (MSE): 0.3513
# R² Score: 0.4625
# Actual Quality Predicted Quality (Scaled)
# 803 6 5.230904
# 124 5 5.347796
# 350 6 5.213949
# 682 5 5.278363
# 1326 6 5.996580
# 976 5 5.008490
# 1493 5 5.004802
# 706 5 5.094469
# 613 5 6.004089
# 1587 6 5.787866
์ค์ผ์ผ๋ง ํ ๋ชจ๋ธ ํ๊ฐ ๊ฒฐ๊ณผ
Metric | Score |
MSE | 0.3513 |
R² Score | 0.4625 |
MAE | 0.4175 |
๋น๊ต ๊ฒฐ๊ณผ:
- Baseline๊ณผ ๋์ผํ ์ฑ๋ฅ์ ๋ณด์ → ์ค์ผ์ผ๋ง์ด XGBoost ๋ชจ๋ธ์๋ ํฐ ์ํฅ์ ์ฃผ์ง ์์์ ํ์ธํ ์ ์์
5. ์ด์์น ์ ๊ฑฐ ํ ์ฑ๋ฅ ๋น๊ต
์ด์์น ์ ๊ฑฐ๊ฐ ํ์ํ ์ด์ ?
- ๋ฐ์ดํฐ์ ๊ทน๋จ์ ์ธ ์ด์์น๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ๋ชจ๋ธ์ด ๊ณผ์ ํฉ(overfitting)๋๊ฑฐ๋
- ์์ธก ์ฑ๋ฅ์ด ์ ํ๋ ๊ฐ๋ฅ์ฑ์ด ์์
์ด์์น ์ ๊ฑฐ ํ ๋ชจ๋ธ ๊ฐ์ ํ๊ธฐ:
# IQR(Interquartile Range) ๋ฐฉ๋ฒ์ ํ์ฉํ ์ด์์น ์ ๊ฑฐ
import numpy as np
# IQR ๊ณ์ฐ
Q1 = X_train.quantile(0.25)
Q3 = X_train.quantile(0.75)
IQR = Q3 - Q1
# ์ด์์น ๊ธฐ์ค ์ค์ (1.5 * IQR)
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# ์ด์์น๊ฐ ์๋ ๋ฐ์ดํฐ๋ง ์ ํ
X_train_filtered = X_train[~((X_train < lower_bound) | (X_train > upper_bound)).any(axis=1)]
y_train_filtered = y_train.loc[X_train_filtered.index]
# XGBoost ๋ชจ๋ธ ์ฌํ์ต (์ด์์น ์ ๊ฑฐ ํ)
xgb_model_filtered = XGBRegressor(random_state=42)
xgb_model_filtered.fit(X_train_filtered, y_train_filtered)
# ์์ธก ๋ฐ ์ฑ๋ฅ ํ๊ฐ
y_pred_xgb_filtered = xgb_model_filtered.predict(X_test)
evaluate_model(y_test, y_pred_xgb_filtered, "XGBoost (Outliers Removed)")
# XGBoost (Outliers Removed) Performance:
# Mean Absolute Error (MAE): 0.4383
# Mean Squared Error (MSE): 0.3492
# R² Score: 0.4656
# Actual Quality Predicted Quality (Scaled)
# 803 6 5.300020
# 124 5 5.235788
# 350 6 4.754515
# 682 5 5.131290
# 1326 6 5.996963
# 976 5 4.997323
# 1493 5 5.347618
# 706 5 5.048402
# 613 5 5.936689
# 1587 6 5.864721
์ด์์น ์ ๊ฑฐ ํ ๋ชจ๋ธ ํ๊ฐ ๊ฒฐ๊ณผ
Metric | Score |
MAE | 0.4383 |
MSE | 0.3492 |
R² Score | 0.4656 |
๋น๊ต ๊ฒฐ๊ณผ
- ์ด์์น ์ ๊ฑฐ ํ ์ฑ๋ฅ ๋ณํ๋ ํฌ์ง ์์
- ๋ค๋ง, MSE๊ฐ ์ฝ๊ฐ ๊ฐ์(R² Score ์ฆ๊ฐ) → ์ด์์น ์ ๊ฑฐ๊ฐ ๋ฏธ์ธํ๊ฒ ์ฑ๋ฅ์ ํฅ์์ํด
6. ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋์ ํตํ ์ต์ ์ฑ๋ฅ ์ฐพ๊ธฐ
XGBoost๋ ๋ค์ํ ํ์ดํผํ๋ผ๋ฏธํฐ ์กฐ์ ์ ํตํด ์ฑ๋ฅ์ ๊ฐ์ ํ ์ ์์
ํนํ, n_estimators, max_depth, learning_rate ๋ฑ์ ๊ฐ์ ์ต์ ํํ๋ ๊ฒ์ด ์ค์ํจ
GridSearchCV๋ฅผ ํ์ฉํ ์ต์ ํ๋ผ๋ฏธํฐ ํ์:
from sklearn.model_selection import GridSearchCV
# ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋ณด ์ ์
param_grid = {
'n_estimators': [100, 300, 500, 1000],
'max_depth': [3, 5, 7, 9],
'learning_rate': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1]
}
# GridSearchCV ์คํ
grid_search = GridSearchCV(XGBRegressor(random_state=42),
param_grid, cv=5, scoring='r2', n_jobs=-1)
grid_search.fit(X_train, y_train)
# ์ต์ ํ๋ผ๋ฏธํฐ ํ์ธ
print("Best Parameters:", grid_search.best_params_)
# ์ต์ ๋ชจ๋ธ ํ๊ฐ
best_xgb = grid_search.best_estimator_
y_pred_xgb_tuned = best_xgb.predict(X_test)
evaluate_model(y_test, y_pred_xgb_tuned, "XGBoost (Tuned)")
์ต์ ํ์ดํผํ๋ผ๋ฏธํฐ ๊ฒฐ๊ณผ
# Best Parameters: {'learning_rate': 0.05, 'max_depth': 5, 'n_estimators': 300}
# XGBoost (Tuned) Performance:
# Mean Absolute Error (MAE): 0.4549
# Mean Squared Error (MSE): 0.3506
# R² Score: 0.4635
# Actual Quality Predicted Quality (Scaled)
# 803 6 5.439800
# 124 5 5.003238
# 350 6 5.149049
# 682 5 5.308735
# 1326 6 5.826034
# 976 5 5.128599
# 1493 5 5.092262
# 706 5 5.184746
# 613 5 5.990964
# 1587 6 5.870989
ํ๋ ํ ๋ชจ๋ธ ํ๊ฐ ๊ฒฐ๊ณผ
Metric | Score |
MAE | 0.4549 |
MSE | 0.3506 |
R² Score | 0.4635 |
ํ๋ ๊ฒฐ๊ณผ
- ์ฑ๋ฅ์ด Baseline๊ณผ ๊ฑฐ์ ๋น์ทํ๊ฑฐ๋ ์ฝ๊ฐ ๋ฎ์ → ๊ธฐ๋ณธ๊ฐ๋ ์ถฉ๋ถํ ์ข์ ์ฑ๋ฅ์ ๋ณด์์
- ํ๋์ด ํญ์ ์ฑ๋ฅ์ ๊ฐ์ ํ์ง๋ ์์ → ๋ฐ์ดํฐ ํน์ฑ์ ๋ฐ๋ผ ๋ค๋ฆ
7. ์ต์ข ์ฑ๋ฅ ๋น๊ต ๋ฐ ๊ฒฐ๋ก
Model | MAE | MSE | R² Score |
XGBoost (Baseline) | 0.4175 | 0.3513 | 0.4625 |
XGBoost (Scaled) | 0.4175 | 0.3513 | 0.4625 |
XGBoost (Outliers Removed) | 0.4383 | 0.3492 | 0.4656 |
XGBoost (Tuned) | 0.4549 | 0.3506 | 0.4635 |
๊ฒฐ๋ก :
- Baseline ๋ชจ๋ธ์ด ๊ฐ์ฅ ์ข์ ์ฑ๋ฅ์ ๋ณด์์
- ์ด์์น ์ ๊ฑฐ๋ ์ผ๋ถ ๊ฐ์ ํจ๊ณผ๊ฐ ์์์
- ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋์ด ์ฑ๋ฅ์ ํฌ๊ฒ ํฅ์์ํค์ง๋ ์์