Skip to content

On-premise users: click in-app to access the full platform documentation for your version of DataRobot.

Scoring Code usage examples

Availability information

Contact your DataRobot representative for information on enabling the Scoring Code feature.

Models displaying the SCORING CODE indicator on the Leaderboard support Scoring Code downloads. You can download Scoring Code JARs from the Leaderboard or from a deployment.

Note

The model JAR files require Java 8 or later.

See below for examples of:

  • Using the binary Scoring Code JAR to score a CSV file on the command line.

  • Using the downloaded JAR in a Java project.

For more information, see the Scoring Code considerations.

Command line interface example

The following example uses the binary scoring code JAR to score a CSV file. See Scoring with the embedded CLI for complete syntax.

java -Dlog4j2.formatMsgNoLookups=true -jar 5cd071deef881f011a334c2f.jar csv --input=Iris.csv --output=Isis_out.csv

Returns:

head Iris_out.csv
Iris-setosa,Iris-virginica,Iris-versicolor
0.9996371740832738,1.8977798830979584E-4,1.7304792841625776E-4
0.9996352462865297,1.9170611877686303E-4,1.730475946939417E-4
0.9996373523223016,1.8970270284380858E-4,1.729449748545291E-4

See also descriptions of command line parameters and increasing Java heap memory.

Java API example

To be used with the Java API, add the downloaded JAR file to the classpath of the Java project. This API has different output formats for regression and classification projects. Below is an example of both:

import com.datarobot.prediction.IClassificationPredictor;
import com.datarobot.prediction.IRegressionPredictor;
import com.datarobot.prediction.Predictors;

import java.util.HashMap;
import java.util.Map;

public class Main {

  public static void main(String[] args) {
    Map<String, Object> row = new HashMap<>();
    row.put("a", 1);
    row.put("b", "some string feature");
    row.put("c", 999);

    // below is an example of prediction of a single variable (regression)

    // get a regression predictor by model id
    IRegressionPredictor regressionPredictor = Predictors.getPredictor("5d2db3e5bad451002ac53318");
    double scored_value = regressionPredictor.score(row);
    System.out.println("The predicted variable: " + scored_value);

    // below is an example of prediction of class probabilities (classification)

    // get a regression predictor by model id
    IClassificationPredictor predictor = Predictors.getPredictor("5d36ee03962d7429f0a6be72");
    Map<String, Double> classProbabilities = predictor.score(row);
    for (String class_label : classProbabilities.keySet()) {
      System.out.printf("The probability of the row belonging to class %s is %f%n",
          class_label, classProbabilities.get(class_label));
    }
  }
}

See also a backward-compatibility example for use when models are generated by different versions of DataRobot.

Load models with a separate class loader

Including model JAR files in the Java class path can result in conflicts between the dependencies in the model JAR file and those in the application code or other model JAR files. To avoid these conflicts, you can load models from the filesystem at runtime using Predictors.getPredictorFromJarFile():

IRegressionPredictor predictor = Predictors.getPredictorFromJarFile("/path/to/model.jar");

Java Prediction Explanation examples

When you download a Scoring Code JAR from the Leaderboard or from a deployment with Include Prediction Explanations enabled, you can calculate Prediction Explanations in your Java code.

Note

For availability information, see the Prediction Explanations for Scoring Code considerations.

The following examples calculate Prediction Explanations with the default parameters:

IRegressionPredictor predictor = Predictors.getPredictor();   
Score<Double> score = predictor.scoreWithExplanations(featureValues);
List<Explanation> explanations = score.getPredictionExplanation();
IClassificationPredictor predictor = Predictors.getPredictor();   
Score<Map<String, Double>> score = predictor.scoreWithExplanations(featureValues);
List<Explanation> explanations = score.getPredictionExplanation();

The following examples calculate Prediction Explanations with custom parameters:

IRegressionPredictor predictor = Predictors.getPredictor();
ExplanationParams parameters = predictor.getDefaultPredictionExplanationParams();
parameters = parameters
    .withMaxCodes(10)
    .withThresholdHigh(0.8)
    .withThresholdLow(0.3);
Score<Double> score = predictor.scoreWithExplanations(featureValues, parameters);
List<Explanation> explanations = score.getPredictionExplanation();
IClassificationPredictor predictor = Predictors.getPredictor();
ExplanationParams defaultParameters = predictor.getDefaultPredictionExplanationParams();
defaultParameters = defaultParameters
    .withMaxCodes(10)
    .withThresholdHigh(0.8)
    .withThresholdLow(0.3);
Score<Map<String, Double>> score = predictor.scoreWithExplanations(featureValues, defaultParameters);
List<Explanation> explanations = score.getPredictionExplanation();

Updated September 25, 2023