【问题标题】:Is it possible to featurize C# collections using ML.NET Preview (0.6)?是否可以使用 ML.NET 预览版 (0.6) 对 C# 集合进行特色化?
【发布时间】:2018-10-17 18:42:15
【问题描述】:

假设我有一个复杂的类型:

class Policy
{
    string Name { get; set; }
    DateTime InceptionDate { get; set; }
    DateTime ExpirationDate { get; set; }
    List<Location> Locations { get; set; }
}

class Location
{
    string Street { get; set; }
    string City { get; set; }
    string State { get; set; }
    string PostalCode { get; set; }
}

如何将Locations 的集合转换为特征列以供 ML.NET 理解?

【问题讨论】:

  • 我可能没有正确地问这个问题。我正在尝试将复杂的对象关系(具有集合)与将用于训练模型的实际数据文件(csv、json 等)联系起来。
  • 我相信我正在寻找使用新 API 的 Microsoft.ML.Legacy.Data.CollectionDataSource.Create 的等价物。任何帮助表示赞赏。谢谢。
  • 如果你的类不是一个简单的属性列表,你不能将这些对象的集合读入IDataView,而且永远也不能。您需要执行预处理,然后将表格数据提供给IDataView

标签: c# collections ml.net


【解决方案1】:

可以将 原始 类型的 Array 特征化。

如果你的班级看起来像这样:

class Policy
{
    string Name { get; set; }
    DateTime InceptionDate { get; set; }
    DateTime ExpirationDate { get; set; }
    float[] Locations { get; set; }
}

那么Locations 将转换为R4 类型的Vector(映射为float)。

然后你创建一个SchemaDefinition:

var env = new LocalEnvironment();
var schemaDef = SchemaDefinition.Create(typeof(Policy));

如果向量的大小在编译时未知,您还需要:

int vectorSize = 4
schemaDef["Locations"].ColumnType = new VectorType(NumberType.R4, vectorSize);

如果 Vector 的大小是固定的,您可以在属性上添加 VectorType 属性:

class Policy
{
    string Name { get; set; }
    DateTime InceptionDate { get; set; }
    DateTime ExpirationDate { get; set; }

    [VectorType(4)]
    float[] Locations { get; set; }
}

然后你创建DataView:

var data = new List<Policy>();
var dataView = env.CreateStreamingDataView(data, schemaDef);

在您的情况下,Locations 是一个类,因此我相信您首先需要通过连接以下示例中的值将其转换为原始数组:

public class IrisData
{
    public float Label;
    public float SepalLength;
    public float SepalWidth;
    public float PetalLength;
    public float PetalWidth;
}

public class IrisVectorData
{
    public float Label;
    public float[] Features;
}

static void Main(string[] args)
{
    // Here's a data array that we want to work on.
    var dataArray = new[] {
        new IrisData{Label=1, PetalLength=1, SepalLength=1, PetalWidth=1, SepalWidth=1},
        new IrisData{Label=0, PetalLength=2, SepalLength=2, PetalWidth=2, SepalWidth=2}
    };

    // Create the ML.NET environment.
    var env = new Microsoft.ML.Runtime.Data.TlcEnvironment();

    // Create the data view.
    // This method will use the definition of IrisData to understand what columns there are in the 
    // data view.
    var dv = env.CreateDataView<IrisData>(dataArray);

    // Now let's do something to the data view. For example, concatenate all four non-label columns
    // into 'Features' column.
    dv = new Microsoft.ML.Runtime.Data.ConcatTransform(env, dv, "Features", 
        "SepalLength", "SepalWidth", "PetalLength", "PetalWidth");

    // Read the data into an another array, this time we read the 'Features' and 'Label' columns
    // of the data, and ignore the rest.
    // This method will use the definition of IrisVectorData to understand which columns and of which types
    // are expected to be present in the input data.
    var arr = dv.AsEnumerable<IrisVectorData>(env, reuseRowObject: false)
        .ToArray();
}

但我还没有真正尝试过这个案例,所以我不能在这里提供更多帮助。

同时查看架构理解文档here

【讨论】:

    【解决方案2】:

    可以在here 中找到使用新 API 将数据从内存读取到 ML 管道的示例。复制相关代码,虽然链接有一些额外的有用的 cmets:

    var mlContext = new MLContext();
    
    IEnumerable<CustomerChurnInfo> churnData = GetChurnInfo();
    
    var trainData = mlContext.CreateStreamingDataView(churnData);
    
    var dynamicLearningPipeline = mlContext.Transforms.Categorical.OneHotEncoding("DemographicCategory")
        .Append(new ConcatEstimator(mlContext, "Features", "DemographicCategory", "LastVisits"))
        .Append(mlContext.BinaryClassification.Trainers.FastTree("HasChurned", "Features", numTrees: 20));
    
    var dynamicModel = dynamicLearningPipeline.Fit(trainData);
    

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多