I trained a model using xgboost v0.90 to be compatible with AWS SageMaker ML Engine. I am doing the usual encoding and hyper-parameter tuning. Some code below:
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
I have installed SHAP and based on the documentation [1], .predict() takes pred_contribs argument. Traceback:
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