# Scoring Code usage examples
URL: https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/quickstart-api.html

> Learn how to use DataRobot's Scoring Code feature.

# Scoring Code usage examples

> [!NOTE] Availability information
> Contact your DataRobot representative for information on enabling the Scoring Code feature.

Models displaying the SCORING CODE [indicator](https://docs.datarobot.com/en/docs/reference/pred-ai-ref/leaderboard-ref.html#tags-and-indicators) on the Leaderboard support Scoring Code downloads. You can download Scoring Code JARs from the [Leaderboard](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/sc-download-leaderboard.html) or from a [deployment](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/sc-download-deployment.html).

> [!NOTE] Java requirement
> The MLOps monitoring library requires Java 11 or higher. Without monitoring, a model's Scoring Code JAR file requires Java 8 or higher; however, when using the MLOps library to instrument monitoring, a model's Scoring Code JAR file requires Java 11 or higher.For Self-managed AI platform installations, the Java 11 requirement applies to DataRobot v11.0 and higher.

See below for examples of:

- Using the binary Scoring Code JAR to score a CSV file on thecommand line.
- Using the downloaded JAR in aJava project.

For more information, see the Scoring Code [considerations](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/index.html#feature-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](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/scoring-cli.html#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](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/scoring-cli.html#command-line-parameters) and increasing [Java heap memory](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/scoring-cli.html#increase-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](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/java-back-compat.html) 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()](https://javadoc.io/static/com.datarobot/datarobot-prediction/2.2.4/com/datarobot/prediction/Predictors.html#getPredictorFromJarFile(java.lang.String%29):

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

### Java Prediction Explanation examples

When you download a Scoring Code JAR from the [Leaderboard](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/sc-download-leaderboard.html) or from a [deployment](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/sc-download-deployment.html) with Include Prediction Explanations enabled, you can calculate [Prediction Explanations](https://docs.datarobot.com/en/docs/classic-ui/modeling/analyze-models/understand/pred-explain/index.html) in your Java code.

> [!NOTE] Note
> For availability information, see the [Prediction Explanations for Scoring Code considerations](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/index.html#prediction-explanations-support).

The following examples calculate Prediction Explanations with the default parameters:

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

**Binary Classification:**
```
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:**
```
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:**
```
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();
```

---
