---
title: "Run a LightGBM ONNX regressor in C#"
url: https://recursiveai.org/onemodel/cookbook/run-lightgbm-onnx-regressor/
description: "Use an onnxmltools LightGBM regressor export to predict numeric values in C# and read one float32 result per input row."
version: "1.5.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/run-lightgbm-onnx-regressor/run-lightgbm-onnx-regressor.zip)

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

```shell
dotnet add package RecursiveAI.OneModel
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemodel/cookbook/run-lightgbm-onnx-regressor/run-lightgbm-onnx-regressor.zip) 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.

```csharp
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:

```csharp
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")
```

## Related recipes

[Run a scikit-learn random forest with ONNX in C#](https://recursiveai.org/onemodel/cookbook/run-scikit-learn-onnx-classifier/) · [Run an XGBoost ONNX classifier in C#](https://recursiveai.org/onemodel/cookbook/run-xgboost-onnx-classifier/)

---

## 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](https://recursiveai.org/onemodel/docs/guide-sklearn-pipelines/) 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](https://onnx.ai/sklearn-onnx/auto_tutorial/plot_gexternal_lightgbm_reg.html) 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.
