Classifiers and regressors trained with scikit-learn, XGBoost or LightGBM and exported to ONNX run as they are, with the exporters' default options: the ai.onnx.ml operators TreeEnsembleClassifier, TreeEnsembleRegressor and TreeEnsemble, ZipMap, and string class labels are all supported. The operator rules and their differences from ONNX Runtime are in ops-ml.md.
A scikit-learn classifier
Export it as you normally would (skl2onnx.to_onnx(model, X[:1])) and load the file. A classifier has two outputs, the predicted labels and the class probabilities; read each by name with the accessor for its kind. With string class labels the labels are string[], and the default ZipMap turns the probabilities into one dictionary per row, keyed by class in the order of classes_:
using var forest = Model.Load(scikitLearnModel); // skl2onnx RandomForestClassifier, string labels, default ZipMap string labelOutput = forest.Session.Outputs[0].Name, probabilityOutput = forest.Session.Outputs[1].Name; string[] labels = forest.RunStrings<float>(rows, [2, 6], labelOutput); IReadOnlyList<IReadOnlyDictionary<string, float>> probabilities = forest.RunStringMaps<float>(rows, [2, 6], probabilityOutput); Console.WriteLine($"{labels[0]}: {probabilities[0][labels[0]]:P0}"); // one dictionary per row, keyed by class
Which accessor to use follows from the output's TensorInfo.Kind:
| Output | Kind | Read with |
|---|---|---|
| Integer labels, probabilities exported without ZipMap, regression values | Tensor | Run<TIn, TOut>(input, shape, name) |
| String labels | StringTensor | RunStrings<TIn>(input, shape, name) |
| ZipMap with integer class labels | SequenceOfMaps | RunInt64Maps<TIn>(input, shape, name) |
| ZipMap with string class labels | SequenceOfMaps | RunStringMaps<TIn>(input, shape, name) |
Asking for the wrong kind throws a SessionException that names the right method.
An XGBoost classifier
onnxmltools writes no ZipMap for XGBoost: the labels are an int64 tensor and the probabilities a float32 tensor [rows, classes].
using var booster = Model.Load(xgboostModel); // onnxmltools XGBClassifier: labels and probabilities are plain tensors long[] predicted = booster.Run<float, long>(rows, [2, 6], booster.Session.Outputs[0].Name); float[] scores = booster.Run<float, float>(rows, [2, 6], booster.Session.Outputs[1].Name, out Shape shape); // [rows, classes]
A missing value (float.NaN) in a row follows the branch the model was trained to send it down, as in the exporters' own runtimes.
With sessions
The graph layer returns the values as tensors; ModelValues reads the string and map ones:
using Session session = Session.Load(scikitLearnModel); using Tensor input = Tensor.FromSpan<float>(rows, Shape.Create(2, 6)); using SessionOutputs outputs = session.Run(new Dictionary<string, Tensor> { [session.Inputs[0].Name] = input }); foreach (TensorInfo info in session.Outputs) { Tensor value = outputs[info.Name]; if (info.Kind == TensorInfoKind.StringTensor) { Console.WriteLine(string.Join(", ", ModelValues.ReadStrings(value))); } else if (info.Kind == TensorInfoKind.SequenceOfMaps) { Console.WriteLine(ModelValues.ReadStringMaps(value).Count + " maps"); // ReadInt64Maps for integer labels } }
A string input is made with ModelValues.Strings(...). Strings and maps only pass through a graph: the operators that read them are the tree classifier (which writes string labels) and ZipMap; every other operator refuses them when the session is created.
Good to know
- Fast and exact mode give the same bits for the tree operators; exact mode only refuses
PROBITpost transforms and the post transforms of the opset 5TreeEnsemble. - A
Device = Gpusession keeps these operators on the CPU. - Models that need other
ai.onnx.mloperators (a scaler, a one-hot encoder,LabelEncoder) in front of the trees are not covered; see ops-ml.md.