如何使用 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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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