SHAPforxgboost icon indicating copy to clipboard operation
SHAPforxgboost copied to clipboard

shap.prep not working when dealing with multiclass classification problems

Open Slangevar opened this issue 2 years ago • 3 comments

Hi! I fit a multiclass classification XGBoost model and try to generate the plot of shapley values using shap.prep and then shap.plot.summary. However, I'm getting the following error when using shap.prep

Error in `colnames<-`(`*tmp*`, value = c(colnames(X_train), "BIAS")) : 
  attempt to set 'colnames' on an object with less than two dimensions

I was wondering whether there was a bug here. Thank you.

Slangevar avatar Oct 07 '22 20:10 Slangevar

Error in `colnames<-`(`*tmp*`, value = c(colnames(X_train), "BIAS")) : 
  attempt to set 'colnames' on an object with less than two dimensions

I receive the same error above after running this command for multiclass classification with a XGBoost model:

shap_values <- SHAPforxgboost::shap.values(xgb_model = model_n, X_train = trainval)

If I use the package fastshap with a similar line of code:

shap_values <- fastshap::explain(model_n, X = trainval, exact = TRUE)

I receive this error:

Error in if (ncol(res) == 1) { : argument is of length zero

While fastshap is running fine on binary classification.

I don't know what can be the cause. Thank you.

marboe123 avatar Oct 18 '22 09:10 marboe123

Same issue here. Multiclass model.

nipnipj avatar Feb 08 '23 14:02 nipnipj

One way this can happen is if there are extra arguments passed into your call to fastshap::explain that don't match a named argument. On https://github.com/bgreenwell/fastshap/blob/90a9cedb26ab217c98f9b917eaa8d0ba28270493/R/explain.R#L372 any unmatched arguments get absorbed into the ... , which likely is length one and foreach only uses the shortest iterator, so any subsequent columns (covariates) get dropped and thus there is a mismatch in the column lengths.

matthewrw avatar Jan 23 '24 02:01 matthewrw