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

Row*_*ish 7 c# machine-learning ml.net

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它与提供的示例非常相似,除了最后一列("Label")处理因为我需要预测数字而不是字符串.我试着用:

pipeline.Add(new ColumnConcatenator("Label", "DigitValue"));
Run Code Online (Sandbox Code Playgroud)

但它不起作用,例外:

Training label column 'Label' type is not valid for multi-class: Vec<R4, 1>. Type must be R4 or R8.
Run Code Online (Sandbox Code Playgroud)

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

UPDATE

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

[Column("7")] [ColumnName("Label")] public float Label;
Run Code Online (Sandbox Code Playgroud)

DigitPredictionScore列,如:

public class DigitPrediction
{
    [ColumnName("Score")] public float[] Score;
}
Run Code Online (Sandbox Code Playgroud)

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

更新2 - 完整的代码示例

根据答案和其他建议,我得到了正确的结果,如果您需要它,您可以在这里找到完整的代码.

Kyl*_*e B 4

看起来您需要将此字段添加到您的类中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
}
Run Code Online (Sandbox Code Playgroud)

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

    Console.WriteLine($"Predicted digit is: {prediction.ExpectedDigit}");
Run Code Online (Sandbox Code Playgroud)

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