liuyanguu / SHAPforxgboost

SHAP (SHapley Additive exPlnation) visualization for 'XGBoost' in 'R'
https://liuyanguu.github.io/SHAPforxgboost/
Other
109 stars 23 forks source link

shap.prep not working when dealing with multiclass classification problems #35

Open Slangevar opened 2 years ago

Slangevar commented 2 years ago

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.

marboe123 commented 2 years ago
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.

nipnipj commented 1 year ago

Same issue here. Multiclass model.

matthewrw commented 10 months ago

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.