ฟังก์ชันวัตถุประสงค์ (loss) และ base learner

Extreme Gradient Boosting with XGBoost

Sergey Fogelson

Head of Data Science, TelevisaUnivision

ฟังก์ชันวัตถุประสงค์และความสำคัญ

  • วัดว่าค่าที่พยากรณ์คลาดเคลื่อนจากค่าจริงมากเพียงใด
  • วัดความต่างระหว่างค่าที่ประมาณกับค่าจริงในชุดข้อมูล
  • เป้าหมาย: หาโมเดลที่ทำให้ค่า loss function ต่ำที่สุด
Extreme Gradient Boosting with XGBoost

Loss function ที่ใช้บ่อยใน XGBoost

  • ชื่อ loss function ใน xgboost:
    • reg:squarederror - ใช้กับปัญหา regression
    • reg:logistic - ใช้กับปัญหา classification เมื่อต้องการเฉพาะผลการตัดสินใจ ไม่ใช่ความน่าจะเป็น
    • binary:logistic - ใช้เมื่อต้องการความน่าจะเป็นแทนการตัดสินใจ
Extreme Gradient Boosting with XGBoost

Base learner และเหตุผลที่ต้องใช้

  • XGBoost สร้าง meta-model จากโมเดลย่อยหลายตัวที่รวมกันเพื่อให้ได้ผลพยากรณ์สุดท้าย
  • โมเดลย่อยแต่ละตัว = base learner
  • ต้องการ base learner ที่เมื่อรวมกันแล้วให้ผลพยากรณ์ที่เป็น non-linear
  • base learner แต่ละตัวควรถนัดแยกแยะหรือพยากรณ์ส่วนต่างกันของชุดข้อมูล
  • base learner มี 2 ประเภท: tree และ linear
Extreme Gradient Boosting with XGBoost

ตัวอย่าง tree เป็น base learner: 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)
Extreme Gradient Boosting with XGBoost

ตัวอย่าง tree เป็น base learner: Scikit-learn API

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

print("RMSE: %f" % (rmse))
RMSE: 129043.2314
Extreme Gradient Boosting with XGBoost

ตัวอย่าง linear base learner: 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)
Extreme Gradient Boosting with XGBoost

ตัวอย่าง linear base learner: learning API เท่านั้น

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

print("RMSE: %f" % (rmse))
RMSE: 124326.24465
Extreme Gradient Boosting with XGBoost

มาฝึกกันเถอะ!

Extreme Gradient Boosting with XGBoost

Preparing Video For Download...