【问题标题】:ML.NET IDataView back to csvML.NET IDataView 返回 csv
【发布时间】:2019-06-05 16:57:15
【问题描述】:

假设我有这个示例数据:

Sample.csv:

Dog,25
Cat,23
Cat,20
Dog,0

我想将它加载到IDataView,将其转换为准备好进行机器学习(没有字符串等),然后再次将其保存为.csv,比如说用另一种工具或语言对其进行分析。

// Load data:
var sampleCsv = Path.Combine("Data", "Sample.csv");
var columns = new[]
{
    new TextLoader.Column("type", DataKind.String, 0),
    new TextLoader.Column("age", DataKind.Int16, 1),
};
var mlContext = new MLContext(seed: 0);
var dataView = mlContext.Data.LoadFromTextFile(sampleCsv, columns,',');

// Transform
var pipeline =
    mlContext.Transforms.Categorical.OneHotEncoding("type",
        // This outputKind will add just one column, while others will add some:
        outputKind: OneHotEncodingEstimator.OutputKind.Key);
var transformedDataView = pipeline.Fit(dataView).Transform(dataView);
//  transformedDataView:
//  Dog,1,25
//  Cat,2,23
//  Cat,2,20
//  Dog,1,0

如何获取两个数字列并将它们写入.csv文件?

【问题讨论】:

  • 你找到解决办法了吗?
  • 是的。移动到Python 中的pandas 包:-(
  • 没有答案,但你能分享一下你是如何在你的转换数据视图中得到“狗”和“猫”的吗?我目前正在处理类似的问题,我无法从转换后的数据视图中获取实际标签
  • 您可以查看here。也许 C# 不适合动态类型

标签: c# csv machine-learning ml.net


【解决方案1】:

您可以为输出数据创建class

class TempOutput
{
    // Note that the types should be the same from the DataView
    public UInt32 type { get; set; }
    public Int16 age { get; set; }
}

然后使用CreateEnumerable<> 读取DataView 中的所有行并将它们打印到`.csv。文件:

File.WriteAllLines(sampleCsv + ".output",
    mlContext.Data.CreateEnumerable<TempOutput>(transformedDataView, false)
    .Select(t => string.Join(',', t.type, t.age)));

【讨论】:

  • 我对这个答案不满意。交叉链接到Github Issue
【解决方案2】:

我在自己的项目中使用以下代码来创建一个 .csv 文件。希望这会有所帮助。

var predictions = mlContext.Data.CreateEnumerable<SpikePrediction>(transformedData, reuseRowObject: false);

SavePredictions(predictions.ToArray());

private void SavePredictions(SpikePrediction[] predictions) {
if (dict.Count() != predictions.Count()) {
    Console.WriteLine("> Cannot save predictions because it does not correspond with the dataset length");
    return;
}
List<string> predictionsCol = _dataCol.ToList();
predictionsCol.Add("Label");

var fullResultFilePath = Path.Combine(_dataPath, FileHandeling.resultFolder, $"{_modelName}.csv");
using (var stream = File.CreateText(fullResultFilePath)) {
    stream.WriteLine(string.Join(",", predictionsCol));
    for (int i = 0; i < predictions.Count(); i++) {
        var label = predictions[i];
        stream.WriteLine(string.Join(",", new string[] { dict[i].Item1.Split("T")[0].Substring(1), dict[i].Item2, label.Prediction[0].ToString() }));
    }
}
}

【讨论】:

  • 介意展示 SpikePrediction 的示例吗?你如何用 Vector 类型创建一个类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多