目標(損失)函式與基學習器

使用 XGBoost 的極端梯度提升

Sergey Fogelson

Head of Data Science, TelevisaUnivision

為何要用目標函式

  • 量化預測與實際結果的差距
  • 衡量某組資料中估計值與真實值的差異
  • 目標:找到使損失函式最小的模型
使用 XGBoost 的極端梯度提升

常見損失函式與 XGBoost

  • xgboost 中常見的損失函式:
    • reg:squarederror-用於回歸問題
    • reg:logistic-分類問題需決策結果、不需機率時使用
    • binary:logistic-需要機率而非僅決策時使用
使用 XGBoost 的極端梯度提升

基學習器與其必要性

  • XGBoost 建立由多個個別模型組成的中介模型,整合後產生最終預測
  • 個別模型=基學習器
  • 期望多個基學習器組合後的最終預測為非線性
  • 每個基學習器應擅長分辨或預測資料集的不同部分
  • 兩種基學習器:樹、線性
使用 XGBoost 的極端梯度提升

以樹為基學習器:Scikit-learn API 範例

import xgboost as xgb
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

boston_data = pd.read_csv("boston_housing.csv")

X, y = boston_data.iloc[:,:-1],boston_data.iloc[:,-1] X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=123)
xg_reg = xgb.XGBRegressor(objective='reg:squarederror', n_estimators=10, seed=123) xg_reg.fit(X_train, y_train) preds = xg_reg.predict(X_test)
使用 XGBoost 的極端梯度提升

以樹為基學習器:Scikit-learn API 範例

rmse = np.sqrt(mean_squared_error(y_test,preds))

print("RMSE: %f" % (rmse))
RMSE: 129043.2314
使用 XGBoost 的極端梯度提升

線性基學習器:僅使用 learning API 範例

import xgboost as xgb
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

boston_data = pd.read_csv("boston_housing.csv")

X, y = boston_data.iloc[:,:-1],boston_data.iloc[:,-1]

X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, 
                                                         random_state=123)

DM_train = xgb.DMatrix(data=X_train,label=y_train) DM_test = xgb.DMatrix(data=X_test,label=y_test)
params = {"booster":"gblinear","objective":"reg:squarederror"}
xg_reg = xgb.train(params = params, dtrain=DM_train, num_boost_round=10) preds = xg_reg.predict(DM_test)
使用 XGBoost 的極端梯度提升

線性基學習器:僅使用 learning API 範例

rmse = np.sqrt(mean_squared_error(y_test,preds))

print("RMSE: %f" % (rmse))
RMSE: 124326.24465
使用 XGBoost 的極端梯度提升

Let's get to work!

使用 XGBoost 的極端梯度提升

Preparing Video For Download...