dotnet / dotnet/machinelearning

How to make fast LightGBM Predictions in C# with a model from LightGBM c++/cli file

Open
#6,840 1 comment 0 reactions 0 assignees View on GitHub
area-Trees enhancement untriaged
Dominant language
C#
Stars
9.4k
Forks
2k
Avg merge
2d 20h
Merged PRs (30d)
11

Description

Hello,

I've successfully created a model using LightGBM CLI, specifically the C/C++ version. Now, I'm facing the task of performing predictions from a C# application. I've written the following code, but unfortunately, I'm encountering difficulties in getting it to work.

I've attempted various approaches, but having a simple example would be incredibly helpful.

Thank you in advance for your assistance.

Best regards,
Wil

```

using Microsoft.ML;
using System;
using System.Collections.Generic;
using System.Linq;

class ProgramWithMLNet
{
static void Main()
{
var mlContext = new MLContext();

// Load the trained model
var model = mlContext.Model.Load(@"Path to a model that has been created with LightGBM CLI", out var modelSchema);

// Prepare input data for prediction
var input = new MyInputDataClass // Please replace this with your input data class
{
_data = new double[] { 0.1, .2, .3 }
};

// Make a prediction
var predictionEngine = mlContext.Model.CreatePredictionEngine(model);
var prediction = predictionEngine.Predict(input);

// Display the prediction result
Console.WriteLine("Prediction: " + prediction);
}
}

class MyInputDataClass
{
public double[] _data = null;
}

class MyOutputDataClass
{
double _result = 0;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.