【问题标题】:ml.net train model input from string instead of a fileml.net 从字符串而不是文件训练模型输入
【发布时间】:2018-06-11 08:42:29
【问题描述】:

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

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

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

目前的文档确实很差,但它也是微软给我们的一个闪亮的新工具。

谢谢

【问题讨论】:

    标签: c# ml.net


    【解决方案1】:

    您需要使用在 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>();
    

    【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多