---
title: "Read scikit-learn SVC probabilities from ONNX in C#"
url: https://recursiveai.org/onemodel/cookbook/read-svc-class-probabilities-from-onnx/
description: "Run an exported scikit-learn SVC in C# and read the probability for each class from its ONNX ZipMap output. The download includes a fitted model."
version: "1.5.0"
---

# Read scikit-learn SVC probabilities from ONNX in C#

**OneModel** is a free, open-source .NET library from Recursive AI for running ONNX models, including scikit-learn pipelines and XGBoost and LightGBM models. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/read-svc-class-probabilities-from-onnx/read-svc-class-probabilities-from-onnx.zip)

Read each class’s probability from `svc.onnx` (in the zip). [See how it was made](#what-does-predict_proba-return-for-an-svc).

```shell
dotnet add package RecursiveAI.OneModel
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneModel;
using static System.FormattableString;
using var model = Model.Load("svc.onnx");
var rows = model.RunInt64Maps<float>(
    [0.25f, 0.5f], [1, 2],
    "output_probability");
foreach (var (id, p) in
         rows[0].OrderBy(x => x.Key))
    Console.WriteLine(
        Invariant($"{id}: {p:F4}"));
```

[The example](https://recursiveai.org/onemodel/cookbook/read-svc-class-probabilities-from-onnx/read-svc-class-probabilities-from-onnx.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

If you paste the snippet, copy `svc.onnx` from the zip next to `Program.cs`.

Prints:

```text
10: 0.8295
20: 0.0809
30: 0.0895
```

## The full program

```csharp
using RecursiveAI.OneModel;
using static System.FormattableString;

using var model = Model.Load("svc.onnx");
using var inputs = new ModelInputs();
inputs.Add<float>("X", [0.25f, 0.5f], [1, 2]);
using var result = model.Run(inputs);
var labels = result.Int64s("output_label");
var probabilities =
    result.Int64Maps("output_probability");
Console.WriteLine(
    $"Predicted class: {labels[0]}");
var row = probabilities[0];
foreach (var (id, p) in
         row.OrderBy(x => x.Key))
    Console.WriteLine(Invariant($"{id}: {p:F4}"));
```

`dotnet run` prints:

```text
Predicted class: 10
10: 0.8295
20: 0.0809
30: 0.0895
```

The input has shape `[1, 2]`: one sample, two `float` features. The full program reads both graph outputs from the same run. `output_label` holds the chosen class; `output_probability` holds one dictionary per sample. Use the class ID as the key, not an array index.

If your export returns a probability tensor rather than a dictionary for each row, `Int64Maps` throws `SessionException`; inspect the output kind and use `Floats` for a float tensor.

## Variations

Read just the probability for class `20` by replacing the `foreach` in the snippet at the top:

```csharp
Console.WriteLine(
    Invariant($"{rows[0][20]:F4}"));
```

This prints `0.0809`. With labels you don’t control, use `TryGetValue` rather than assuming a particular key exists.

To inspect your own export, replace `"svc.onnx"` with your model’s path. [OneModel’s pipeline guide](https://recursiveai.org/onemodel/docs/guide-sklearn-pipelines/#export) explains how to inspect input and output names and kinds. Use the listed names in your `Add` and output calls.

```csharp
using RecursiveAI.OneModel;

using var model = Model.Load("svc.onnx");
foreach (var input in model.Session.Inputs)
    Console.WriteLine(
        $"In: {input.Name} ({input.Kind})");
foreach (var output in model.Session.Outputs)
    Console.WriteLine(
        $"Out: {output.Name} ({output.Kind})");
```

## Related recipes

- [Run a scikit-learn random forest with ONNX](https://recursiveai.org/onemodel/cookbook/run-scikit-learn-onnx-classifier/)
- [Run an ONNX StandardScaler and Ridge pipeline](https://recursiveai.org/onemodel/cookbook/run-onnx-standardscaler-ridge-regression/)

## The short answer

Fit a probability-enabled scikit-learn `SVC`, export it with `skl2onnx`, and read `output_probability` in C#. With the default export in this example, it’s a `ZipMap`: a sequence of dictionaries, with one dictionary for each input row. In the row above, the key `10` means class `10`, regardless of the dictionary’s iteration order.

## What does `predict_proba()` return for an SVC?

[In scikit-learn 1.8](https://scikit-learn.org/1.8/modules/generated/sklearn.svm.SVC.html), `predict_proba()` returns a probability table with one row per sample and columns in `classes_` order. `predict()` returns the chosen class instead. These come from different calculations, so the class with the highest probability can differ from the predicted label. Read the graph’s label output when you want its decision; use the probability output when you need the individual estimates. `LinearSVC` is a different estimator and isn’t the probability-enabled `SVC` used here.

The script fits an SVC on three groups of synthetic points, with class IDs `10`, `20`, and `30`. Save it as `export.py` or use the copy in the zip. To rebuild `svc.onnx`, run these commands in a Python environment from the folder containing the script. The C# program uses the supplied model and doesn’t need Python.

```shell
python -m pip install scikit-learn==1.8.0
python -m pip install skl2onnx==1.20.0
python -m pip install onnx==1.23.2
python export.py
```

```python
import numpy as np
import onnx
from sklearn.svm import SVC
from skl2onnx import to_onnx

classes = [10, 20, 30]
centers = np.array(
    [[0, 0], [4, 0], [0, 4]],
    dtype=np.float32,
)
steps = [-0.5, 0, 0.5]
offsets = [[x, y] for x in steps
           for y in steps]
points = centers[:, None, :] + offsets
X = points.reshape(-1, 2)
X = X.astype(np.float32)
y = np.repeat(classes, len(offsets))
svc = SVC(probability=True, random_state=7)
svc.fit(X, y)
model = to_onnx(svc, X[:1], target_opset=20)
onnx.save(model, "svc.onnx")
```

The synthetic model illustrates the output layout, not the quality of the probability estimates.

## How does ZipMap store class probabilities in ONNX?

[ZipMap](https://onnx.ai/onnx/operators/onnx_aionnxml_ZipMap.html) associates the probability values with the original class labels. If you train with integer IDs, read the sequence with `Int64Maps`; if you train with string labels, use `StringMaps`. The sample’s numeric keys aren’t consecutive: class `30` is the third class, not element `30` of a vector.

[`skl2onnx` can also export probabilities as a tensor](https://onnx.ai/sklearn-onnx/auto_examples/plot_convert_zipmap.html) with `zipmap=False`. Then you need the training class order to interpret the columns; a tensor element doesn’t carry its label. The [OneModel pipeline guide](https://recursiveai.org/onemodel/docs/guide-sklearn-pipelines/) lists the accessors by output kind.
