XGBoost - 迴歸器
迴歸是XGBoost中使用的一種技術,用於預測連續數值。當目標變數顯示連續輸出時,通常將其用於預測銷售額、房地產價格和股票價值。
- 迴歸問題的結果可以是實數或連續的數字。兩種常見的迴歸演算法是決策樹和線性迴歸。迴歸評估使用多種指標,例如均方誤差 (MSE) 和均方根誤差 (RMSE)。這些是XGBoost模型的一些主要組成部分,每個部分都有其關鍵功能。
- RMSE指的是均方根誤差的平方根。然而,MAE是實際值和預期值之間差異的絕對總和,由於數學誤差,它不像其他指標那樣常用。
XGBoost是構建監督迴歸模型的有用工具。通過了解其目標函式和基礎學習器可以證明這一說法。
- 目標函式包括損失函式和正則化項。它解釋了實際值和預期值之間的差異,或者模型輸出與真實資料相差多少。
- XGBoost中迴歸和二元分類最常用的損失函式分別是reg:linear和reg:logistic。XGBoost是一種整合學習技術。整合學習需要訓練和合並單個模型以獲得單個預測。
XGBRegressor語法
Python中的XGBRegressor是XGBoost的迴歸專用版本,用於迴歸問題,目標是預測連續數值。
構建XGBRegressor模組的基本語法如下:
import xgboost as xgb model = xgb.XGBRegressor( objective='reg:squarederror', max_depth=max_depth, learning_rate=learning_rate, subsample=subsample, colsample_bytree=colsample, n_estimators=num_estimators )
引數
以下是XGBRegressor函式的引數:
objective 是一個必備引數,它決定了模型在迴歸任務中的用途。它設定為reg,這意味著它使用平方損失來計算迴歸問題中的誤差。
max_depth 是一個可選引數,它表示每棵決策樹可以達到的深度。較高的值允許樹學習更多資訊,但也可能導致過擬合。
learning_rate 是另一個可選引數。它控制模型在每一步學習的程度。較小的值可以透過減慢學習速度來防止過擬合。
subsample 是可選的,它指的是將用於建立每棵樹的資料部分。使用較少的資料可以使模型更通用。
colsample_bytree 也是可選的,它控制用於建立每棵樹的特徵(列)數量。
n_estimators 是必需的,它告訴模型要建立多少棵樹(提升輪數)。更多的樹可以提高精度,但也使模型更復雜。
XGBRegressor示例
這段程式碼訓練了一個機器學習模型,該模型使用XGBoost方法來預測房價。透過讀取資料集並將其劃分為訓練集和測試集,它將訓練模型。最後,透過計算均方根誤差或RMSE來評估預測精度。
讓我們在這個資料集上使用XGBoost框架評估迴歸技術:
# Required imports
import numpy as np
import pandas as pd
import xgboost as xg
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
# Loading the data
dataset = pd.read_csv("/Python/Datasets/HousingData.csv")
X, y = dataset.iloc[:, :-1], dataset.iloc[:, -1]
# Splitting the datasets
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size = 0.3, random_state = 123)
# Instantiation
xgb_r = xg.XGBRegressor(objective ='reg:linear', n_estimators = 10, seed = 123)
# Fitting the model
xgb_r.fit(train_X, train_y)
# Predict the model
pred = xgb_r.predict(test_X)
# RMSE Computation
rmse = np.sqrt(MSE(test_y, pred))
print("RMSE : % f" %(rmse))
輸出
以下是上述模型的輸出:
RMSE : 4.963784
線性基學習器
這段程式碼使用線性提升器和XGBoost來預測房價。載入資料集,將其拆分為訓練集和測試集,然後將每個集合轉換為XGBoost所需的DMatrix格式。在模型訓練後,透過計算均方根誤差或RMSE來確定預測精度。
# Required imports
import numpy as np
import pandas as pd
import xgboost as xg
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
# Loading the data
dataset = pd.read_csv("/Python/Datasets/HousingData.csv")
X, y = dataset.iloc[:, :-1], dataset.iloc[:, -1]
# Splitting the datasets
train_X, test_X, train_y, test_y = train_test_split(X, y,
test_size = 0.3, random_state = 123)
train_dmatrix = xg.DMatrix(data = train_X, label = train_y)
test_dmatrix = xg.DMatrix(data = test_X, label = test_y)
# Parameter dictionary
param = {"booster":"gblinear", "objective":"reg:linear"}
xgb_r = xg.train(params = param, dtrain = train_dmatrix, num_boost_round = 10)
pred = xgb_r.predict(test_dmatrix)
# RMSE Computation
rmse = np.sqrt(MSE(test_y, pred))
print("RMSE : % f" %(rmse))
輸出
以下是該模型的結果:
RMSE : 6.101922
總結
XGBoost是一個流行的迴歸問題解決框架。由於其高效的梯度提升和處理複雜資料集的能力,它非常適合精確預測連續數值的迴歸模型。由於其持續發展,XGBoost成為機器學習迴歸分析中的重要工具,這保證了它將在迴歸程式中處於領先地位。