io.github.xydrolase:shap4j

Java wrapper of the SHAP library (https://github.com/slundberg/shap)

License

License

GroupId

GroupId

io.github.xydrolase
ArtifactId

ArtifactId

shap4j
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Java wrapper of the SHAP library (https://github.com/slundberg/shap)
Project Organization

Project Organization

io.github.xydrolase

Download shap4j

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.xydrolase/shap4j/ -->
<dependency>
    <groupId>io.github.xydrolase</groupId>
    <artifactId>shap4j</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.xydrolase/shap4j/
implementation 'io.github.xydrolase:shap4j:0.0.2'
// https://jarcasting.com/artifacts/io.github.xydrolase/shap4j/
implementation ("io.github.xydrolase:shap4j:0.0.2")
'io.github.xydrolase:shap4j:jar:0.0.2'
<dependency org="io.github.xydrolase" name="shap4j" rev="0.0.2">
  <artifact name="shap4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.xydrolase', module='shap4j', version='0.0.2')
)
libraryDependencies += "io.github.xydrolase" % "shap4j" % "0.0.2"
[io.github.xydrolase/shap4j "0.0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.bytedeco : javacpp jar 1.5.3
org.bytedeco : javacpp-platform jar 1.5.3

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.6.2
commons-io : commons-io jar 2.6

Project Modules

There are no modules declared in this project.

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
  • 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.2</version>
</dependency>

SBT

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

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

Versions

Version
0.0.2
0.0.1