Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose shapley flag in mojo 2 #15695

Closed
NidhiMehta opened this issue Aug 15, 2023 · 8 comments
Closed

Expose shapley flag in mojo 2 #15695

NidhiMehta opened this issue Aug 15, 2023 · 8 comments
Assignees
Labels

Comments

@NidhiMehta
Copy link

Allow user to get shap predictions in from h2o-3 mojo model like we do in driverless(https://docs.h2o.ai/driverless-ai/1-10-lts/docs/userguide/scoring-mojo-scoring-pipeline.html?highlight=shapley%20mojo)

@NidhiMehta NidhiMehta changed the title Expose shapely flag in mojo 2 Expose shapley flag in mojo 2 Aug 15, 2023
@wendycwong wendycwong self-assigned this Sep 5, 2023
@wendycwong
Copy link
Contributor

The user already can get the shap contributions from h2o-3 mojo model. You just need to add --predictContributions to the original command you use to invoke H2O-3 mojo.

@NidhiMehta
Copy link
Author

@NidhiMehta NidhiMehta reopened this Sep 5, 2023
@NidhiMehta
Copy link
Author

@wendycwong
Copy link
Contributor

In h2o-3, first, use this to generate a mojo:


1. Open a terminal window and start python.

2. Run the following commands to build a simple GBM model.

The model, along with the h2o-genmodel.jar file will

then be downloaded to an experiment folder.

import h2o
from h2o.estimators.gbm import H2OGradientBoostingEstimator
h2o.init()
h2o_df = h2o.load_dataset("prostate.csv")
h2o_df["CAPSULE"] = h2o_df["CAPSULE"].asfactor()
model=H2OGradientBoostingEstimator(distribution="bernoulli",
ntrees=100,
max_depth=4,
learn_rate=0.1)
model.train(y="CAPSULE",
x=["AGE","RACE","PSA","GLEASON"],
training_frame=h2o_df)

Download the MOJO and the resulting h2o-genmodel.jar file

to a new experiment folder. Note that the h2o-genmodel.jar file

is a library that supports scoring and contains the required readers

and interpreters. This file is required when MOJO models are deployed

to production. Be sure to specify the entire path, not just the relative path.

modelfile = model.download_mojo(path="~/experiment/", get_genmodel_jar=True)
print("Model saved to " + modelfile)
Model saved to /Users/user/GBM_model_python_1475248925871_888.zip

@wendycwong
Copy link
Contributor

Next, generate a java class and called it main.java as follows:


import java.io.;
import hex.genmodel.easy.RowData;
import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.prediction.
;
import hex.genmodel.MojoModel;

public class main {
public static void main(String[] args) throws Exception {
EasyPredictModelWrapper.Config config = new EasyPredictModelWrapper.Config()
.setModel(MojoModel.load("/Users/user/GBM_model_python_1475248925871_888.zip"))
.setEnableLeafAssignment(true)
.setEnableContributions(true); // note this is set to true to enable shapley values to be returned.
EasyPredictModelWrapper model = new EasyPredictModelWrapper(config);

RowData row = new RowData();
row.put("AGE", "68");
row.put("RACE", "2");
row.put("DCAPS", "2");
row.put("VOL", "0");
row.put("GLEASON", "6");

BinomialModelPrediction p = model.predictBinomial(row);
System.out.println("Has penetrated the prostatic capsule (1=yes; 0=no): " + p.label);
System.out.print("Class probabilities: ");
for (int i = 0; i < p.classProbabilities.length; i++) {
  if (i > 0) {
    System.out.print(",");
  }
  System.out.print(p.classProbabilities[i]);
}

System.out.println("Leaf node assignments: ");
for (int i=0; i < p.leafNodeAssignments.length; i++) {
  if (i > 0) {
    System.out.print(p.leafNodeAssignments[i]);
  }
}
System.out.println("");

System.out.println("Shapley contributions: ");
for (int i=0; i < p.contributions.length; i++) {
  if (i > 0) {
    System.out.print(", ");
  }
  System.out.print(model.getContributionNames()[i] + ": " + p.contributions[i]);
}
System.out.println("");

}
}

@wendycwong
Copy link
Contributor

Next, compile the code as follows:

javac -cp /Users/wendycwong/h2o-3/h2o-web/src/main/resources/www/3/h2o-genmodel.jar -J-Xms2g main.java

then copy over the h2o-genmodel.jar into current directory (~/experiment)

cp /Users/wendycwong/h2o-3/h2o-web/src/main/resources/www/3/h2o-genmodel.jar .

then run the code to generate in this case both the leaf node assignment and the shapley values:

java -cp .:h2o-genmodel.jar main

the result looks like this:

image

@wendycwong
Copy link
Contributor

wendycwong commented Sep 5, 2023

Is this clear for you? @NidhiMehta

@NidhiMehta
Copy link
Author

yes thanks Wendy. @sh1ng will reach out if need more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants