如何使用 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,00,0,0,0,0,1,1,11,1,1,0,1,1,0,21,1,1,0,0,1,1,30,1,0,1,0,1,1,41,1,1,1,0,0,1,51,1,1,1,1,0,1,61,0,0,0,0,1,1,71,1,1,1,1,1,1,81,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;}

现在系统“工作”了,我得到的prediction.Score是一个Single[]值,其中与最高值关联的索引就是预测值。这是否是正确的方法?

更新 2 – 完整代码示例

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


回答:

看起来你需要在你的DigitPrediction类中添加这个字段:

public class DigitPrediction{    [ColumnName("PredicatedLabel")]    public uuint ExpectedDigit; // <-- 这是预测的值    [ColumnName("Score")]    public float[] Score; // <-- 这是预测值为正确分类的概率}

我想你需要更改写入结果的行如下:

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

还有一件事,看起来 API 中有一个错误,预测的数字会偏差一个单位,但如果你通过将预测值加 1 来调整,它就会是正确的值。我希望他们将来会修复这个问题,这里有一个相关的问题:(https://github.com/dotnet/machinelearning/issues/235)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注