dotnet / dotnet/machinelearning

SlotNames behave differently based on column type

Open
#6,087 3 comments 0 reactions 0 assignees View on GitHub
area-Core bug Priority:2
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: 1.7.0
- .NET Version: .NET 6.0

**Describe the bug**

For multiclass classification problems, SlotNames are only available when the columns are of type `string`. Even though, the value and meaning of that value are the same, the SlotName behavior is different based on the data type.

**To Reproduce**
Steps to reproduce the behavior:
1. Train a multiclass classification model
2. Map labels to scores. You can do it with code similar to the following:

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

// Add input data
var sampleData = new SentimentModel.ModelInput()
{
Col0 = "This restaurant was wonderful."
};

// Load model and predict output of sample data
var result = SentimentModel.Predict(sampleData);

// If PredictedLabel is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"
string sentiment = result.PredictedLabel == "1" ? "Positive" : "Negative";
Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");

var sortedLabels = GetScoresWithLabelsSorted(SentimentModel.PredictEngine.Value.OutputSchema, nameof(result.Score), result.Score);

foreach(var (k,v) in sortedLabels)
{
Console.WriteLine($"{k}: {v}");
}

static Dictionary GetScoresWithLabelsSorted(DataViewSchema schema, string name, float[] scores)
{
Dictionary result = new Dictionary();

var column = schema.GetColumnOrNull(name);

var slotNames = new VBuffer>();
column.Value.GetSlotNames(ref slotNames);
var names = new string[slotNames.Length];
var num = 0;
foreach (var denseValue in slotNames.DenseValues())
{
result.Add(denseValue.ToString(), scores[num++]);
}

return result.OrderByDescending(c => c.Value).ToDictionary(i => i.Key, i => i.Value);
}
```

**Expected behavior**

Not sure? I would think that if the value and "meaning" of that value are the same, the type shouldn't matter and SlotNames are made available.

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.