Regularization และ Base Learner ใน XGBoost

Extreme Gradient Boosting with XGBoost

Sergey Fogelson

Head of Data Science, TelevisaUnivision

Regularization ใน XGBoost

  • Regularization คือการควบคุมความซับซ้อนของโมเดล
  • ต้องการโมเดลที่แม่นยำและเรียบง่ายที่สุด
  • พารามิเตอร์ Regularization ใน XGBoost:
    • gamma - ค่าลด loss ขั้นต่ำที่อนุญาตให้แบ่ง split
    • alpha - l1 regularization บน leaf weights ค่ายิ่งมาก regularization ยิ่งสูง
    • lambda - l2 regularization บน leaf weights
Extreme Gradient Boosting with XGBoost

ตัวอย่าง L1 Regularization ใน XGBoost

import xgboost as xgb
import pandas as pd
boston_data = pd.read_csv("boston_data.csv")
X,y = boston_data.iloc[:,:-1],boston_data.iloc[:,-1]

boston_dmatrix = xgb.DMatrix(data=X,label=y) params={"objective":"reg:squarederror","max_depth":4}
l1_params = [1,10,100] rmses_l1=[]
for reg in l1_params: params["alpha"] = reg cv_results = xgb.cv(dtrain=boston_dmatrix, params=params,nfold=4, num_boost_round=10,metrics="rmse",as_pandas=True,seed=123) rmses_l1.append(cv_results["test-rmse-mean"].tail(1).values[0])
print("Best rmse as a function of l1:") print(pd.DataFrame(list(zip(l1_params,rmses_l1)), columns=["l1","rmse"]))
Best rmse as a function of l1:
        l1          rmse
    0    1  69572.517742
    1   10  73721.967141
    2  100  82312.312413
Extreme Gradient Boosting with XGBoost

Base Learner ใน XGBoost

  • Linear Base Learner:
    • ผลรวมของพจน์เชิงเส้น
    • โมเดล boosted คือผลรวมถ่วงน้ำหนักของโมเดลเชิงเส้น (จึงเป็นเชิงเส้นเช่นกัน)
    • ใช้งานน้อยมาก
  • Tree Base Learner:
    • ต้นไม้ตัดสินใจ
    • โมเดล boosted คือผลรวมถ่วงน้ำหนักของต้นไม้ตัดสินใจ (nonlinear)
    • ใช้งานเกือบทั้งหมดใน XGBoost
Extreme Gradient Boosting with XGBoost

การสร้าง DataFrame จากหลายลิสต์ที่มีความยาวเท่ากัน

  • pd.DataFrame(list(zip(list1,list2)),columns=["list1","list2"]))
  • zip สร้าง generator ของค่าที่จับคู่กัน:
    • zip([1,2,3],["a","b""c"]) = [1,"a"],[2,"b"],[3,"c"]
    • generators ต้องถูก instantiate ทั้งหมดก่อนจึงจะใช้กับออบเจกต์ DataFrame ได้
  • list() จะ instantiate generator ทั้งหมด แล้วส่งเข้า DataFrame เพื่อแปลงนิพจน์ทั้งหมด
Extreme Gradient Boosting with XGBoost

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

Extreme Gradient Boosting with XGBoost

Preparing Video For Download...