梯度提升(GB)

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

梯度提升樹

  • 逐步修正前一模型的錯誤。

  • 不會調整訓練樣本的權重。

  • 每個預測器以前一個的殘差當作標籤來訓練。

  • 梯度提升樹:以 CART 為基學習器。

Machine Learning with Tree-Based Models in Python

回歸的梯度提升樹:訓練

GBT-train

Machine Learning with Tree-Based Models in Python

縮減(Shrinkage)

GBT-lr

Machine Learning with Tree-Based Models in Python

梯度提升樹:預測

  • 回歸:

    • $y_{pred} = y_1 + \eta r_1 + ... + \eta r_N$
    • 在 sklearn 中:GradientBoostingRegressor
  • 分類:

    • 在 sklearn 中:GradientBoostingClassifier
Machine Learning with Tree-Based Models in Python

sklearn 的梯度提升(auto 資料集)

# Import models and utility functions
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE

# Set seed for reproducibility
SEED = 1

# Split dataset into 70% train and 30% test
X_train, X_test, y_train, y_test = train_test_split(X,y, 
                                                    test_size=0.3, 
                                                    random_state=SEED)
Machine Learning with Tree-Based Models in Python
# Instantiate a GradientBoostingRegressor 'gbt'
gbt = GradientBoostingRegressor(n_estimators=300, max_depth=1, random_state=SEED)

# Fit 'gbt' to the training set gbt.fit(X_train, y_train) # Predict the test set labels y_pred = gbt.predict(X_test) # Evaluate the test set RMSE rmse_test = MSE(y_test, y_pred)**(1/2) # Print the test set RMSE print('Test set RMSE: {:.2f}'.format(rmse_test))
Test set RMSE: 4.01
Machine Learning with Tree-Based Models in Python

一起來練習吧!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...