是否可以在 XGBoost 包随机森林中收集单个树预测?

问题描述 投票:0回答:1

我有一个使用 python 中的 xgboost 包制作的模型,我想知道在回归的情况下是否可以在对单个树预测进行打包(平均)之前存储和引用它们。我知道如何查看实际的树,但我不能将它们视为可调用模型来进行预测。

rfr_model = xgboost.XGBRFRegressor(n_estimators = 300,
                                max_depth = 5,
                                objective='reg:squarederror')
rfr_model.fit(X_train, y_train)

当我使用 X_test 进行预测时,我想要(测试行,树)的输出

python random-forest xgboost
1个回答
0
投票

应该可以用

predict()

例如,类似

import numpy as np

# Assume rfr_model is your trained XGBRFRegressor model
# X_test is the test dataset

# Get the Booster object from the trained XGBoost model
booster = rfr_model.get_booster()

# Number of trees in the model
num_trees = booster.get_num_boosting_rounds()

# Initialize an array to store predictions from each tree
tree_preds = np.zeros((X_test.shape[0], num_trees))

# Iterate over each tree and get predictions
for tree_idx in range(num_trees):
    tree_preds[:, tree_idx] = booster.predict(X_test, ntree_limit=tree_idx + 1)

# Now tree_preds contains the predictions of each tree for each row in X_test
© www.soinside.com 2019 - 2024. All rights reserved.