Skip to content

xydrolase/shap4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shap4j

Build Status javadoc

Java interface for the SHAP (SHapley Additive exPlanations) library for tree ensembles (TreeExplainer). Note that shap4j is not a pure Java port of SHAP. Rather, it utilizes JavaCPP to provide a Java-Native Interface (JNI) on top of the fast C++ implementation of TreeExplainer. In this sense, shap4j leverages the same underlying native code that powers the Python version of TreeExplainer, to ensure validity and efficiency.

Current supported platforms:

  • macosx-x86_64
  • macosx-arm64
  • linux-x86_64

Use cases

shap4j enables lean SHAP integration in JVM projects, i.e. a project can import shap4j as the sole dependency, without having to depend on heavier third-party tree ensemble libraries, e.g. xgboost4j.

Tree ensemble model conversion

In order for shap4j to explain a tree ensemble model, that model must be provided in a .shap4j data file, which can be generated from model dumps (pickle files) of XGBoost/LightGBM/CatBoost/sklearn using the companion Python library shap4j-data-converter.

Usage

Maven

<dependency>
  <groupId>io.github.xydrolase</groupId>
  <artifactId>shap4j-platform</artifactId>
  <version>0.0.3</version>
</dependency>

SBT

libraryDependencies += "io.github.xydrolase" % "shap4j-platform" % "0.0.3"

Example usage

package examples;

import java.nio.file.Files;
import java.io.File;
import shap4j.TreeExplainer;

class ExampleApp {
    public static void main(String[] args) throws Exception {
        byte[] data = Files.readAllBytes(new File("boston.shap4j").toPath());
        TreeExplainer explainer = new TreeExplainer(data);
        double[] x = {
                6.320e-03, 1.800e+01, 2.310e+00, 0.000e+00, 5.380e-01, 6.575e+00,
                6.520e+01, 4.090e+00, 1.000e+00, 2.960e+02, 1.530e+01, 3.969e+02,
                4.980e+00
        };
        double[] shapValues = explainer.shapValues(x, false);

        System.out.println("SHAP values: " + Arrays.toString(shapValues));
    }
}

The data file boston.shap4j can be found here. Or, you can follow the examples in shap4j-data-converter to generate it yourself.

API Docs