xgboost : TypeError: predict() got an unexpected keyword argument 'pred_contribs'
Posted
技术标签:
【中文标题】xgboost : TypeError: predict() got an unexpected keyword argument \'pred_contribs\'【英文标题】:xgboost : TypeError: predict() got an unexpected keyword argument 'pred_contribs'xgboost : TypeError: predict() got an unexpected keyword argument 'pred_contribs' 【发布时间】:2022-01-12 06:14:30 【问题描述】:我使用 xgboost v0.90 训练了一个模型,使其与 AWS SageMaker ML Engine 兼容。我正在做通常的编码和超参数调整。下面是一些代码:
import pandas as pd
import pickle
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
# split df into train and test
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,0:21], df.iloc[:,-1], test_size=0.1)
X_train.shape
(1000,21)
# Encode categorical variables
cat_vars = ['cat1','cat2','cat3']
cat_transform = ColumnTransformer([('cat', OneHotEncoder(handle_unknown='ignore'), cat_vars)], remainder='passthrough')
encoder = cat_transform.fit(X_train)
X_train = encoder.transform(X_train)
X_test = encoder.transform(X_test)
X_train.shape
(1000,420)
# Define a xgboost regression model
model = XGBRegressor()
# Do hyper-parameter tuning
.....
# Fit model
model.fit(X_train, y_train)
# Forecast on test data
y_pred = model.predict(X_test, pred_contribs=True)
y_pred
我已经安装了 SHAP 并根据文档 [1],.predict()
采用 pred_contribs
参数。追溯:
TypeError Traceback (most recent call last)
<ipython-input-1119-37e607e853fd> in <module>
1 # Forecast on test data
----> 2 y_pred = model.predict(X_test, pred_contribs=True)
3 y_pred
TypeError: predict() got an unexpected keyword argument 'pred_contribs'
[1]https://shap.readthedocs.io/en/latest/example_notebooks/tabular_examples/tree_based_models/Python%20Version%20of%20Tree%20SHAP.html?highlight=pred_contribs
[2]https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.Booster.predict
【问题讨论】:
【参考方案1】:XGBRegressor 的 predict 没有pred_contribs
参数,它只有这些参数:
predict(self, X, output_margin=False, ntree_limit=None,
validate_features=True, base_margin=None, iteration_range=None)
你说的参数pred_contribs
在xgboost.Booster.predict()
中,这是一个低级XGBoost,它有这些参数:
predict(self, data: xgboost.core.DMatrix, output_margin: bool = False,
ntree_limit: int = 0, pred_leaf: bool = False, pred_contribs: bool = False,
approx_contribs: bool = False, pred_interactions: bool = False,
validate_features: bool = True, training: bool = False,
iteration_range: Tuple[int, int] = (0, 0), strict_shape: bool = False)
所以你必须使用它:
data = xgboost.DMatrix(X, label = y)
model = xgboost.train("learning_rate": 0.01, "max_depth": 4, data)
model.predict(data, pred_contribs = True)
【讨论】:
我明白了。什么是低级 XGBoost?XGBRegressor
和 XGBClassifier
是类似 sklearn 的包装器,可以使用 XGBRegressor
和 XGBClassifier
完成的所有操作都可以通过底层的 xgboost.train
函数完成。反过来,这显然不是真的。 xgboost.train
有更多参数,它让您可以更好地控制训练、验证和预测。这就是为什么xgboost.train
有pred_contribs
参数而XGBRegressor
没有。阅读this Stack Overflow 答案以获取更多信息。
正确。该模型是使用 XGBRegressor() 训练的,我无法更新它。当我进行推理/预测时会发生异常。我想我需要一个中间人将测试数据更新为 xgboost.DMatrix 然后调用 predict 方法?
你必须使用xgboost.train()
训练你的模型以上是关于xgboost : TypeError: predict() got an unexpected keyword argument 'pred_contribs'的主要内容,如果未能解决你的问题,请参考以下文章
基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable”
Python XGBoost 分类器无法“预测”:“TypeError: Not supported type for data”
基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable”
基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable”