Recursive AI

Run ONNX DictVectorizer with Model.RunMap 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.

Turn one dictionary into a feature row with features.onnx (in the zip). The zip includes export.py; see how to recreate the model.

Terminal
dotnet add package RecursiveAI.OneModel

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneModel;
 
using var model = Model.Load("features.onnx");
var basket = new Dictionary<string, float>
{
    ["pears"] = 4f,
    ["apples"] = 2f,
};
var row = model.RunMap<string, float, float>(
    basket);
Console.WriteLine(string.Join(", ", row));

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

If you’re pasting the snippet into a new project, copy features.onnx from the zip into that project folder before running it.

Prints:

Text
2, 0, 4

The vocabulary is apples, bananas, pears. Even though the input lists pears first, the row starts with the apple count and leaves zero for bananas.

If the graph declares more than one output or doesn’t take a single map, RunMap throws SessionException.

Variations

To get the output shape as well as its values, replace the RunMap call with the overload that takes an out parameter. This model returns one row with three columns.

C#
var row = model.RunMap<string, float, float>(
    basket, out var shape);
Console.WriteLine(shape);

ONNX with multiple inputs and outputs · Run a scikit-learn random forest with ONNX · Run an ONNX ColumnTransformer


The short answer

Use model.RunMap<string, float, float>(dictionary) when the ONNX graph takes one map(string, float) and returns one float tensor. The model supplies the feature order; you don’t sort or pad the dictionary in C#.

How do I export a scikit-learn DictVectorizer to ONNX?

Fit the vectorizer on representative dictionaries. The sample uses DictVectorizer(dtype=np.float32, sparse=False), fitted on {"apples": 1, "bananas": 2} and {"pears": 4}. In skl2onnx, declare the input as a DictionaryType with matching key and value types. This example exports one map input named basket.

To recreate the model, install the export dependencies in a Python environment:

Terminal
python -m pip install scikit-learn==1.9.1
python -m pip install skl2onnx==1.20.0
python -m pip install numpy==2.5.5 onnx==1.23.2

Save this as export.py and run python export.py in the project folder. It replaces the sample .onnx file. You don’t need Python to run the C# example with the supplied model.

PYTHON
import numpy as np
from sklearn.feature_extraction import DictVectorizer
from skl2onnx import to_onnx
from skl2onnx.common import data_types
 
vectorizer = DictVectorizer(
    dtype=np.float32, sparse=False
).fit([
    {"apples": 1, "bananas": 2},
    {"pears": 4},
])
map_type = data_types.DictionaryType(
    data_types.StringTensorType([1]),
    data_types.FloatTensorType([1]),
)
graph = to_onnx(
    vectorizer, initial_types=[("basket", map_type)],
    target_opset=17,
)
with open("features.onnx", "wb") as file:
    file.write(graph.SerializeToString())

The map is one record, not a tensor of field names or an array of dictionaries. Run one call per record with this model. The C# type parameters must match the key and value types declared at export (string and float here). In RunMap<TKey, TValue, TOut>, the last type parameter is the element type of the output tensor, not a collection type.

In what order does DictVectorizer return features?

The operator maps each key to the corresponding slot of the fitted vocabulary. A key you leave out of the input map results in a zero in that slot. Here bananas is missing, but the second slot is still there. OneModel ignores keys outside the graph’s vocabulary; adding a new fruit won’t expand the row. See the OneModel operator reference and the ONNX DictVectorizer specification for the vocabulary and key type rules. To interpret the columns later, keep the fitted vectorizer’s feature names with the exported model.

Can I use RunMap with a classifier after DictVectorizer?

Only if the exported graph still has exactly one map input and one supported tensor output. A standard classifier export may declare both a label and probabilities, in which case RunMap is the wrong API even though the input is still a map. For that graph, use a named session input built with ModelValues.Map and read the declared outputs by name. The OneModel pipeline guide distinguishes the single-input, single-output helper from the named session interface.

All onemodel recipes