ModelOriented / shapviz

SHAP Plots in R
https://modeloriented.github.io/shapviz/
GNU General Public License v2.0
79 stars 14 forks source link

Cannot rename colnames/dimnames in post-processing #98

Closed madprogramer closed 1 year ago

madprogramer commented 1 year ago

Consider a simple force plot generated from the diamonds dataset:

library(shapviz)
library(ggplot2)
library(xgboost)

set.seed(3653)

x <- c("carat", "cut", "color", "clarity")
dtrain <- xgb.DMatrix(data.matrix(diamonds[x]), label = diamonds$price)
fit <- xgb.train(params = list(learning_rate = 0.1), data = dtrain, nrounds = 65)

# Explanation data
dia_small <- diamonds[sample(nrow(diamonds), 2000), ]

shp <- shapviz(fit, X_pred = data.matrix(dia_small[x]), X = dia_small)

sv_force(shp, row_id = 1)

image

Because I currently live in Europe, I would like to change the color=F in this plot to colour=F as in UK ortography.

> colnames(shp)
[1] "carat"   "cut"     "color"   "clarity"
> colnames(shp) <- c("carat", "cut", "colour", "clarity")
Error in dimnames(x) <- dn : 'dimnames' applied to non-array

Although the shapviz class defines an S3 method for colnames, there is no way to alter the colnames/dimnames because the class does not have array-like inheritance for the underlying matrix. Passing dimnames for both axes of the dimensions will also fail:

> dimnames(shp)
[[1]]
NULL

[[2]]
[1] "carat"   "cut"     "color"   "clarity"

> dimnames(shp) <- list(NULL, c("carat", "cut", "colour", "clarity"))
Error in dimnames(shp) <- list(NULL, c("carat", "cut", "colour", "clarity")) : 
  'dimnames' applied to non-array

Essentially, I want to be able to post-process the shapviz object for human readibility. Another use-case could be when working with categorical variables and using collapse (https://github.com/ModelOriented/shapviz/issues/7 ) where renaming variables before the shapviz object is generated might not be feasible.

mayer79 commented 1 year ago

Good idea. We can add a dimnames.shapviz<- method, which would automatically give you also the possibility to use

colnames(shp) <- c("carat", "cut", "colour", "clarity")

Note to myself: don't forget SHAP interaction arrays.

mayer79 commented 1 year ago

Implemented in https://github.com/ModelOriented/shapviz/pull/99