dotnet / dotnet/machinelearning

Input column 'PredictedLabel' doesn't contain key values metadata

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

Description

**System Information (please complete the following information):**
- OS & Version: Windows 11
- ML.NET Version:
- .NET Version: .NET 7

**Describe the bug**

When loading a LightGBM model for scoring, I get the following error:

```text
Input column 'PredictedLabel' doesn't contain key values metadata (Parameter 'inputSchema')
```

**Screenshots, Code, Sample Projects**

Stack Trace

```text
at Microsoft.ML.Transforms.KeyToValueMappingEstimator.GetOutputSchema(SchemaShape inputSchema)
at Microsoft.ML.Data.EstimatorChain`1.GetOutputSchema(SchemaShape inputSchema)
at Microsoft.ML.Data.EstimatorChain`1.Fit(IDataView input)
at Program.$(String[] args) in C:\Dev\LGBMLoadingSample\LGBMLoadingSample\Program.cs:line 22
```

Code that throws error:

```csharp
using Microsoft.ML;
using Microsoft.ML.Data;

var ctx = new MLContext();

var modelPath = "iris-lgbm.txt";

var data = new[]
{
new {SepalLength=5.1f, SepalWidth=3.5f,PetalLength=1.4f, PetalWidth=0.2f}
};

var dv = ctx.Data.LoadFromEnumerable(data);

using(var modelStream = new FileStream(modelPath,FileMode.Open))
{
var pipeline =
ctx.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(ctx.MulticlassClassification.Trainers.LightGbm(modelStream), TransformerScope.Scoring)
.Append(ctx.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

var model = pipeline.Fit(dv);

var predictions = model.Transform(dv);

var predictionCol = predictions.GetColumn("PredictedLabel");

foreach(var pred in predictionCol)
{
Console.WriteLine(pred);
}
};
```

Working code:

```csharp
using(var modelStream = new FileStream(modelPath,FileMode.Open))
{
var pipeline =
ctx.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(ctx.MulticlassClassification.Trainers.LightGbm(modelStream), TransformerScope.Scoring);

var model = pipeline.Fit(dv);

var predictions = model.Transform(dv);

var predictionCol = predictions.GetColumn("PredictedLabel");

foreach(var pred in predictionCol)
{
Console.WriteLine(pred);
}
};
```

Model:

[iris-lgbm.txt](https://github.com/dotnet/machinelearning/files/10873652/iris-lgbm.txt)

Output Schema:

![image](https://user-images.githubusercontent.com/46974588/222494779-db8ebcf3-c241-42c2-922e-95af27aa5682.png)

**Additional context**
Add any other context about the problem here.

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.