假设我有这个样本数据:
Sample.csv:
Dog,25
Cat,23
Cat,20
Dog,0我希望将它加载到IDataView中,将其转换为为ML做好准备(没有字符串等等),然后将其再次保存为.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文件?
发布于 2019-06-05 17:12:53
您可以为输出数据创建一个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)));发布于 2021-06-28 08:29:15
我在自己的项目中使用以下代码创建.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() }));
}
}
}https://stackoverflow.com/questions/56464923
复制相似问题