【发布时间】:2018-05-23 21:16:07
【问题描述】:
我正在查看这里的 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
这是我的测试代码:
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();
}
您可以看到它与提供的示例非常相似,除了最后一列(“标签”)处理因为我需要预测一个数字而不是一个字符串。我尝试:
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 - 完整的代码示例
按照答案和其他建议我得到了正确的结果,如果你需要它,你可以找到完整的代码here。
【问题讨论】:
-
因为 ML.net 太新了,我会在 github 上为 ML.net 发布这个问题:github.com/dotnet/machinelearning/issues 如果可以的话,用“问题”标记它,当你得到答案时,把它贴在这里作为回答你自己的问题。我也想知道答案。它对你的预测正确吗?
-
@KyleB 感谢您的建议,我在这里提出:github.com/dotnet/machinelearning/issues/226
标签: c# machine-learning ml.net