Git Product home page Git Product logo

shap4j's Introduction

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

shap4j's People

Contributors

dependabot[bot] avatar xydrolase avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

shap4j's Issues

Getting UnsatisfiedLinkError while trying to run TreeExplainer on LightGBM model

I have converted LIghtGBM model into .shap4j format and trying to execute TreeExplainer using the example shared in the repo.
But I am getting following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniTreeEnsemble in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib] at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2673) at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830) at java.base/java.lang.System.loadLibrary(System.java:1873) at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1832) at org.bytedeco.javacpp.Loader.load(Loader.java:1423) at org.bytedeco.javacpp.Loader.load(Loader.java:1234) at org.bytedeco.javacpp.Loader.load(Loader.java:1210) at shap4j.shap.TreeEnsemble.<clinit>(TreeEnsemble.java:16) at shap4j.TreeExplainer.<init>(TreeExplainer.java:36) at javashap.JavaShap.main(JavaShap.java:15) Caused by: java.lang.UnsatisfiedLinkError: Could not find jniTreeEnsemble in class, module, and library paths. at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1799) ... 6 more

I am not able to figure out what is going wrong here. Kindly help.
I am attaching the model for reproducing this issue. This model needs a feature array with 2020 features.
model_shap4j.txt
(Modified name because it is not allowing to attach file with .shap4j extension. The file name is model.shap4j)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.