---
title: Scoring Code usage examples
description: Learn how to use DataRobot's Scoring Code feature.
---

# Scoring Code usage examples {: #scoring-code-usage-examples }

!!! info "Availability information"
     Contact your DataRobot representative for information on enabling the Scoring Code feature.

Models displaying the SCORING CODE [indicator](leaderboard-ref#tags-and-indicators) on the Leaderboard support Scoring Code downloads. You can download Scoring Code JARs from the [Leaderboard](sc-download-leaderboard) or from a [deployment](sc-download-deployment).

![](images/sc-quickstart-portable-predictions.png)

!!! 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](#command-line-interface-example).

* Using the downloaded JAR in a [Java project](#java-api-example).


For more information, see the Scoring Code [considerations](scoring-code/index#feature-considerations).

## Command line interface example {: #command-line-interface-example }

The following example uses the binary scoring code JAR to score a CSV file. See [Scoring with the embedded CLI](scoring-cli#scoring-with-the-embedded-cli) for complete syntax.

``` bash
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](scoring-cli#command-line-parameters) and increasing [Java heap memory](scoring-cli#increase-java-heap-memory).

## Java API example {: #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:


``` java
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](java-back-compat) example for use when models are generated by different versions of DataRobot.

## Load models with a separate class loader {: #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()](https://javadoc.io/static/com.datarobot/datarobot-prediction/2.2.4/com/datarobot/prediction/Predictors.html#getPredictorFromJarFile(java.lang.String)){ target=_blank }:

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

### Java Prediction Explanation examples {: #java-prediction-explanation-examples }

When you download a Scoring Code JAR from the [Leaderboard](sc-download-leaderboard) or from a [deployment](sc-download-deployment) with **Include Prediction Explanations** enabled, you can calculate [Prediction Explanations](pred-explain/index) in your Java code. 

!!! note
    For availability information, see the [Prediction Explanations for Scoring Code considerations](scoring-code/index#prediction-explanations-support). 

The following examples calculate Prediction Explanations with the _default_ parameters:

=== "Regression"

    ``` java
    IRegressionPredictor predictor = Predictors.getPredictor();   
    Score<Double> score = predictor.scoreWithExplanations(featureValues);
    List<Explanation> explanations = score.getPredictionExplanation();
    ```

=== "Binary Classification"

    ``` java
    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:

=== "Regression"

    ``` java
    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();
    ```

=== "Binary Classification"

    ``` java
    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();
    ```

