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

前端 未结 1 559
谎友^
谎友^ 2021-01-14 12:14

I\'ve been using the following line of code to load text data:

pipeline.Add(new TextLoader(dataPath).CreateFrom(separator: \',\'));


        
相关标签:
1条回答
  • 2021-01-14 13:03

    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>();
    
    0 讨论(0)
提交回复
热议问题