Read each class’s probability from svc.onnx (in the zip). See how it was made.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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:
10: 0.8295 20: 0.0809 30: 0.0895
The full program
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:
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:
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 explains how to inspect input and output names and kinds. Use the listed names in your Add and output calls.
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
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, 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.
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
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 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 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 lists the accessors by output kind.