【问题标题】:How to add multiple custom columns to IDataView in ML.NET如何在 ML.NET 中向 IDataView 添加多个自定义列
【发布时间】:2021-09-10 17:59:39
【问题描述】:

从文件加载我的 IDataView 后,我想添加两个自定义列。在每一行中,新列的值应该是值的自然对数。

我的环境是:

  • Windows 10 专业版,版本 10.0.19043 Build 19043
  • ML.NET 1.6.0 版
  • Microsoft Visual Studio Professional 2022 预览版(64 位),版本 17.0.0 预览版 3.1

这是我到目前为止所做的。这是一个独立的、可重现的程序来演示问题。

using Microsoft.ML;
using Microsoft.ML.Transforms;

namespace TestLog {
  public static class Program {
    private class InputData {
      public double Velocity { get; set; }
      public double Thrust { get; set; }
    }

    private class CustomMappingOutput {
      public double LogVelocity { get; set; }
      public double LogThrust { get; set; }
    }

    private class TransformedData : InputData {
      public double LogVelocity { get; set; }
      public double LogThrust { get; set; }
    }

    [CustomMappingFactoryAttribute("LogVelocity")]
    private class LogVelocityCustomAction : CustomMappingFactory<InputData, CustomMappingOutput> {
      public static void CustomAction(InputData input, CustomMappingOutput output)
        => output.LogVelocity = (float) Math.Log(input.Velocity);
      public override Action<InputData, CustomMappingOutput> GetMapping() => CustomAction;
    }

    [CustomMappingFactoryAttribute("LogThrust")]
    private class LogThrustCustomAction : CustomMappingFactory<InputData, CustomMappingOutput> {
      public static void CustomAction(InputData input, CustomMappingOutput output)
        => output.LogThrust = (float)Math.Log(input.Thrust);
      public override Action<InputData, CustomMappingOutput> GetMapping() => CustomAction;
    }

    public static void Run() {
      var mlContext = new MLContext();

      var samples = new List<InputData> {
          new InputData { Velocity= 0.006467, Thrust = 1.614237 },
          new InputData { Velocity= 0.53451,  Thrust = 1.068356 },
          new InputData { Velocity= 0.278578, Thrust = 0.216861 },
          new InputData { Velocity= 0.014179, Thrust = 0.119712 },
          new InputData { Velocity= 0.392814, Thrust = 3.915486 }
      };

      var data = mlContext.Data.LoadFromEnumerable(samples);

      var pipeline = mlContext.Transforms.CustomMapping(new LogVelocityCustomAction().GetMapping(),
                                                        contractName: "LogVelocity")
             .Append(mlContext.Transforms.CustomMapping(new LogThrustCustomAction().GetMapping(),
                                                        contractName: "LogThrust"));

      var transformer = pipeline.Fit(data);

      // Now save the transform pipeline so that it can be reloaded by another process.
      mlContext.Model.Save(transformer, data.Schema, "customTransform.zip");


      // We load the saved transform and use it, as if it was in another program.
      var loadedTransform = mlContext.Model.Load("customTransform.zip", out _);

      // Now we can transform the data.
      var transformedIDataView = loadedTransform.Transform(data);

      var newDataEnumerable = mlContext.Data.CreateEnumerable<TransformedData>(transformedIDataView,
                                                                               reuseRowObject: true);
      var newIDataView = mlContext.Data.LoadFromEnumerable(newDataEnumerable);

      // Save newIDataView as a CSV file so we can verify the transformations.
      var path = @"../../../transformed.csv";
      var mlContextSave = new MLContext();
      using var stream = File.Create(path);
      mlContextSave.Data.SaveAsText(newIDataView,
                                    stream,
                                    separatorChar: ',',
                                    headerRow: true,
                                    schema: false);
    }

    static void Main() {
      Program.Run();
      Console.WriteLine("Hit return to exit");
      Console.ReadLine();
    }
  }
}

在程序结束时,转换后的 IDataView 将保存为“.csv”文件。 它看起来像:

如您所见,“Velocity”列没有被转换。

任何帮助或建议将不胜感激。

查尔斯

【问题讨论】:

    标签: ml.net


    【解决方案1】:

    您将日志转换为float,然后将其分配给double 类型的变量。您是否尝试过投射到double

    我不确定为什么它适用于推力而不适用于速度。也许第一行速度中非常小的数字导致了异常。

    【讨论】:

    • 我移除了演员以浮动。那没有帮助。结果是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2016-11-05
    • 2018-10-22
    • 2022-11-03
    • 2013-11-10
    • 1970-01-01
    相关资源
    最近更新 更多