Read a label and its probability from forest.onnx (in the zip). skl2onnx exports probabilities as a dictionary keyed by class name (ZipMap).
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel; using var model = Model.Load("forest.onnx"); float[] row = [-1, -1, 0, 0, 0, 0]; var label = model.RunStrings<float>( row, [1, 6], "output_label")[0]; var map = model.RunStringMaps<float>( row, [1, 6], "output_probability")[0]; Console.WriteLine(FormattableString.Invariant( $"{label}: {map[label]: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, add the package, copy forest.onnx from the zip into the project folder, and run dotnet run.
Prints:
setosa: 0.8318Full program
The zip’s synthetic forest takes six float32 features in order. The program sends two rows, [-1, -1, 0, 0, 0, 0] and [1, 1, 0, 0, 0, 0], and prints every probability with its class name.
using RecursiveAI.OneModel; using System.Globalization; CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; using var model = Model.Load("forest.onnx"); float[] rows = [-1, -1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]; var labels = model.RunStrings<float>(rows, [2, 6], "output_label"); var maps = model.RunStringMaps<float>(rows, [2, 6], "output_probability"); for (int row = 0; row < labels.Length; row++) { Console.WriteLine($"Row {row + 1}: {labels[row]}"); foreach (var (label, probability) in maps[row]) Console.WriteLine($" {label}: {probability:F4}"); }
dotnet run prints:
Row 1: setosa setosa: 0.8318 versicolor: 0.1682 virginica: 0.0000 Row 2: virginica setosa: 0.0000 versicolor: 0.1225 virginica: 0.8775
Using a numeric tensor accessor for either of these outputs throws SessionException; the error names the accessor to use.
Variations
Read the output names and kinds when you swap in another export:
using RecursiveAI.OneModel; using var model = Model.Load("forest.onnx"); foreach (var output in model.Session.Outputs) Console.WriteLine($"{output.Name}: {output.Kind}");
Prints:
output_label: StringTensor output_probability: SequenceOfMaps
Using scikit-learn 1.9.1 and skl2onnx 1.20.0, export a fitted RandomForestClassifier with string labels. X_train is its float32 training matrix.
from pathlib import Path from skl2onnx import to_onnx export = to_onnx(forest, X_train[:1]) Path("forest.onnx").write_bytes(export.SerializeToString())
Related recipes
Run an XGBoost ONNX classifier in C# · Run a LightGBM ONNX regressor in C#
The short answer
Use Model.Load to open the .onnx file exported by skl2onnx. Call RunStrings<float> for the string labels and RunStringMaps<float> for the ZipMap probabilities. Pass each output’s graph name, not the class name. The class name is a key in the returned dictionary, so map[label] gives you the probability for the class the model predicted.
How do I convert a scikit-learn random forest to ONNX?
Export the fitted RandomForestClassifier with skl2onnx.to_onnx, supplying a sample input row with the feature count and type you plan to use at inference. The tree-model guide shows the default export path. The example file uses float32 with six features and retains ZipMap, the default. Python is needed for the export, not for the C# program.
If the forest was trained on already transformed data, pass features in that transformed form. An exported pipeline is a different graph: from OneModel 1.5.0 a pipeline with a scaler, one-hot encoder, LabelEncoder or text step in front of the forest runs as it is, with one named input per column; the scikit-learn pipelines guide shows how to pass them. The operator reference lists what the runtime accepts.
How do I get class probabilities from a random forest ONNX model?
With ZipMap, maps[0] is the first sample’s probability dictionary, not the probability of class zero. The full program reads it alongside labels[0] and prints each key and value. Its flower names are just labels assigned to synthetic rows, not predictions about real flowers. Use the actual strings returned from the model, including their original case, rather than translating a guessed column number to a class.
The skl2onnx ZipMap documentation also describes exporting probabilities as a plain tensor. If you disabled ZipMap, use the numeric Run<float, float> overload, and preserve the exported class order when interpreting columns. If ZipMap is present but the keys are integer classes rather than strings, use RunInt64Maps<float> instead. SequenceOfMaps tells you that the output is a map sequence; you still need to know whether the exporter used string or integer keys.