如何使用 ML.NET 预测整数值?

How to predict integer values using ML.NET?

我正在查看这里的 cs 文件:https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows 一切正常。

现在我想改进这个例子:我想预测一个纯数字数据集而不是一个数字字符串数据集,例如预测七段显示的输出。

这是我的超简单数据集,最后一列是我要预测的整数:

1,0,1,1,1,1,1,0
0,0,0,0,0,1,1,1
1,1,1,0,1,1,0,2
1,1,1,0,0,1,1,3
0,1,0,1,0,1,1,4
1,1,1,1,0,0,1,5
1,1,1,1,1,0,1,6
1,0,0,0,0,1,1,7
1,1,1,1,1,1,1,8
1,1,1,1,0,1,1,9

这是我的测试代码:

public class Digit
{
    [Column("0")] public float Up;

    [Column("1")] public float Middle;

    [Column("2")] public float Bottom;

    [Column("3")] public float UpLeft;
    [Column("4")] public float BottomLeft;
    [Column("5")] public float TopRight;
    [Column("6")] public float BottomRight;

    [Column("7")] [ColumnName("DigitValue")]
    public float DigitValue;
}

public class DigitPrediction
{
    [ColumnName("PredictedDigits")] public float PredictedDigits;
}

public PredictDigit()
{
    var pipeline = new LearningPipeline();
    var dataPath = Path.Combine("Segmenti", "segments.txt");
    pipeline.Add(new TextLoader<Digit>(dataPath, false, ","));
    pipeline.Add(new ColumnConcatenator("Label", "DigitValue"));
    pipeline.Add(new ColumnConcatenator("Features", "Up", "Middle", "Bottom", "UpLeft", "BottomLeft", "TopRight", "BottomRight"));
    pipeline.Add(new StochasticDualCoordinateAscentClassifier());
    var model = pipeline.Train<Digit, DigitPrediction>();
    var prediction = model.Predict(new Digit
    {
        Up = 1,
        Middle = 1,
        Bottom = 1,
        UpLeft = 1,
        BottomLeft = 1,
        TopRight = 1,
        BottomRight = 1,
    });

    Console.WriteLine($"Predicted digit is: {prediction.PredictedDigits}");
    Console.ReadLine();
}

如您所见,除了最后一列 ("Label") 处理之外,它与提供的示例非常相似,因为我需要预测一个数字而不是一个字符串。我尝试:

pipeline.Add(new ColumnConcatenator("Label", "DigitValue"));

但它不起作用,异常:

Training label column 'Label' type is not valid for multi-class: Vec<R4, 1>. Type must be R4 or R8.

我确定我错过了什么,但实际上我在互联网上找不到任何可以帮助我解决这个问题的东西。

更新

我发现数据集 必须 有这样一个 Label 列:

[Column("7")] [ColumnName("Label")] public float Label;

DigitPrediction 一个 Score 列,如:

public class DigitPrediction
{
    [ColumnName("Score")] public float[] Score;
}

现在系统 "works" 我得到了 prediction.Score 一个 Single[] 值,其中与较高值关联的索引是预测值。 这是正确的方法吗?

更新 2 - 完整代码示例

按照答案和其他建议我得到了正确的结果,如果你需要它你可以找到完整的代码here

您还可以尝试在流水线中将 ColumnConcatenator 与 ColumnCopier 交换用于 Label 列。

pipeline.Add(new ColumnCopier ("Label", "DigitValue"));

这将指示流水线哪一列是 Label,但 ColumnCopier 的输出将不是向量,这与 ColumnConcatenator 的输出不同。

并且也可以类似地添加分数列。

看起来您需要将此字段添加到您的 class DigitPrediction:

public class DigitPrediction
{
    [ColumnName("PredicatedLabel")]
    public uuint ExpectedDigit; // <-- This is the predicted value

    [ColumnName("Score")]
    public float[] Score; // <-- This is the probability that the predicted value is the right classification
}

而且我认为您需要将写入结果的行更改为:

    Console.WriteLine($"Predicted digit is: {prediction.ExpectedDigit}");

还有一件事,API 中似乎有一个错误,其中预期数字将偏离一个,但如果您通过将 +1 添加到预测值来移动它,它将是正确的价值。我希望他们将来能解决这个问题,但有一个问题:(https://github.com/dotnet/machinelearning/issues/235)

现在,必须遵循以下模式:

  • 列特征(所有特征 - 它们必须具有相同的类型)

  • 列标签(您的 "answers")

如果原始数据集有另一个答案栏使用:

pipeline.Add(new ColumnCopier(("DigitValue", "Label")));

第一个是源,第二个是目的地。如我所见,需要双'('。