【发布时间】: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'
[2]https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.Booster.predict
【问题讨论】:
标签: python machine-learning xgboost