目标(损失)函数与基学习器

使用 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 的极端梯度提升

开始动手!

使用 XGBoost 的极端梯度提升

Preparing Video For Download...