Recursive AI

Run a LightGBM ONNX regressor 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.

Predict one number per row from regressor.onnx (in the zip).

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("regressor.onnx");
float[] row = [-1, -1, 0, 0, 0, 0];
float[] values = model.Run(row, [1, 6]);
Console.WriteLine(FormattableString.Invariant(
    $"{values[0]: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 regressor.onnx from the zip into the project folder, and run dotnet run.

Prints:

Text
-1.2145

Full program

The synthetic LightGBM model takes six float32 features. The full program runs [-1, -1, 0, 0, 0, 0] and [1, 1, 0, 0, 0, 0] together and prints the predictions in row order.

C#
using RecursiveAI.OneModel;
using System.Globalization;
 
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
using var model = Model.Load("regressor.onnx");
float[] rows = [-1, -1, 0, 0, 0, 0,
                 1,  1, 0, 0, 0, 0];
var values = model.Run<float, float>(rows, [2, 6], out var shape);
Console.WriteLine($"Output shape: [{shape[0]}, {shape[1]}]");
for (int row = 0; row < values.Length; row++)
    Console.WriteLine($"Row {row + 1}: {values[row]:F4}");

dotnet run prints:

Text
Output shape: [2, 1]
Row 1: -1.2145
Row 2: 0.6714

If a replacement file includes an unsupported operator, loading the model fails before it can score any rows.

Variations

To get the output name for use with model.Session, print it:

C#
using RecursiveAI.OneModel;
using var model = Model.Load("regressor.onnx");
Console.WriteLine(model.Session.Outputs[0].Name);

Prints:

Text
variable

Using LightGBM 4.7.0 and onnxmltools 1.16.0, export a fitted LGBMRegressor named regressor with six float32 features per row. Install packaging in the Python environment as well.

PYTHON
from onnxmltools import convert_lightgbm, save_model
from onnxmltools.convert.common.data_types import FloatTensorType
 
inputs = [("input", FloatTensorType([None, 6]))]
export = convert_lightgbm(regressor, initial_types=inputs, target_opset=15)
save_model(export, "regressor.onnx")

Run a scikit-learn random forest with ONNX in C# · Run an XGBoost ONNX classifier in C#


The short answer

Open the onnxmltools export with Model.Load and pass the feature values to Run. This is a regressor, so the model returns a numeric estimate for each row, not a class label or a probability. The snippet passes one row of six float32 values and reads values[0]. The batch has shape [2, 6] on input and [2, 1] on output: two samples, with one predicted value each.

How do I convert a LightGBM regressor to ONNX?

For a fitted LGBMRegressor, export the estimator with onnxmltools.convert_lightgbm and describe the input with FloatTensorType using the feature count used at training. Save the resulting ONNX graph and load that in C#; changing the extension on a native LightGBM model won’t convert it. The zip bundles a standalone export, so no export step is needed to run the sample.

A pipeline with preprocessing is not the same as a standalone tree: it is one graph whose inputs are your raw columns. From OneModel 1.5.0 the whole ai.onnx.ml domain runs, so a scikit-learn pipeline with a LightGBM step, exported by skl2onnx and onnxmltools with their default options, loads as it is. The scikit-learn pipelines guide shows how to pass its named inputs.

How do I use a LightGBM regressor for predictions in C#?

Keep feature order, units and any fitted transformations the same as at training. The model expects six values per row; the runtime can reject a wrong shape, but it cannot tell if you accidentally put a valid number in the wrong column. Flatten a batch row by row. Read the regression output in that same order. -1.2145 is a valid value for the synthetic target in this example, not an error code or a probability to clamp to zero.

When changing exporters or export settings, check the ONNX predictions against the fitted Python model on representative rows before using the C# results. The exporter’s LightGBM regressor guide explains how converting tree values to float can change the sums. Choose a prediction tolerance that fits the application instead of assuming exact equality with native LightGBM. The program prints four decimal places for readability; it does not round the returned array.

All onemodel recipes