ml.net train model input from string instead of a file

南楼画角 提交于 2019-12-02 02:31:07

You'll want to use a CollectionDataSource which was introduced in v0.2 of ML.NET. You can either grab fresh github bits or the nuget, and then you can use CollectionDataSource on top of an enumerable.

You can find a full example in its tests: https://github.com/dotnet/machinelearning/blob/6d5a41d39face9e98c242d3db3ff10ea8e233cc1/test/Microsoft.ML.Tests/CollectionDataSourceTests.cs

One example on Iris data:

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>();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!