ml.net训练模型从字符串而不是文件输入

时间:2018-06-11 08:42:30

标签: c# ml.net

我一直在使用以下代码行来加载文本数据:

pipeline.Add(new TextLoader(dataPath).CreateFrom<SentimentData>(separator: ','));

但有没有办法将字符串作为数据注入?假设我们想要从数据库中获取模型,我不必先将字符串保存到文件中,还是我呢?

这个日期的文档真的很差,但它也是微软给我们的一个闪亮的新工具。

由于

1 个答案:

答案 0 :(得分:4)

您想要使用ML.NET v0.2中引入的CollectionDataSource。您可以获取新的github位或nuget,然后您可以在可枚举的顶部使用CollectionDataSource。

您可以在测试中找到完整的示例: https://github.com/dotnet/machinelearning/blob/6d5a41d39face9e98c242d3db3ff10ea8e233cc1/test/Microsoft.ML.Tests/CollectionDataSourceTests.cs

关于虹膜数据的一个例子:

var data = new List<IrisData>() {
    new IrisData { SepalLength = 1f, SepalWidth = 1f ,PetalLength=0.3f, PetalWidth=5.1f, Label=1},
    new IrisData { SepalLength = 1f, SepalWidth = 1f ,PetalLength=0.3f, PetalWidth=5.1f, Label=1},
    new IrisData { SepalLength = 1.2f, SepalWidth = 0.5f ,PetalLength=0.3f, PetalWidth=5.1f, Label=0}
};
var collection = CollectionDataSource.Create(data);

pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
    "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>();

IrisPrediction prediction = model.Predict(new IrisData()
{
    SepalLength = 3.3f,
    SepalWidth = 1.6f,
    PetalLength = 0.2f,
    PetalWidth = 5.1f,
});

pipeline = new LearningPipeline();
collection = CollectionDataSource.Create(data.AsEnumerable());
pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
    "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
model = pipeline.Train<IrisData, IrisPrediction>();
相关问题