首页 > 解决方案 > Microsoft.ML 在使用 LoadFromEnumerable 时添加标签

问题描述

有些东西不起作用,我想不通......我想从列表中加载数据。我尝试了几个代码

MLContext mlContext = new MLContext();

        var listData = new List<ProductEntry>
        {
            new ProductEntry {CoPurchaseProductID = 12, ProductID = 4},
            new ProductEntry {CoPurchaseProductID = 12, ProductID = 3},
            new ProductEntry {CoPurchaseProductID = 11, ProductID = 5},
            new ProductEntry {CoPurchaseProductID = 11, ProductID = 3}
        };
        var data = mlContext.Data.LoadFromEnumerable(listData);
        var options = new MatrixFactorizationTrainer.Options
        {
            MatrixColumnIndexColumnName = nameof(ProductEntry.ProductID),
            MatrixRowIndexColumnName = nameof(ProductEntry.CoPurchaseProductID),
            LabelColumnName = "Label",
            LossFunction = MatrixFactorizationTrainer.LossFunctionType.SquareLossOneClass,
            Alpha = 0.01,
            Lambda = 0.025,
            Quiet = false,
            C = 0.00001,
            ApproximationRank = 10,
            NumberOfIterations = 20
        };

        var est = mlContext.Recommendation().Trainers.MatrixFactorization(options);
        ITransformer model = est.Fit(data);

        var predictionengine = mlContext.Model.CreatePredictionEngine<ProductEntry, Copurchase_prediction>(model);

        Console.WriteLine("Calculating the top 3 products for product 3...");
        var top5 = (from m in Enumerable.Range(1, 262111)
                    let p = predictionengine.Predict(
                        new ProductEntry()
                        {
                            ProductID = 3,
                            CoPurchaseProductID = (uint)m
                        })
                    orderby p.Score descending
                    select (ProductID: m, Score: p.Score)).Take(10);
        foreach (var t in top5)
            Console.WriteLine($"  Score:{t.Score}\tProduct: {t.ProductID}");

        Console.ReadKey();

我遇到了关于“标签”的问题这是类:

public class Copurchase_prediction
    {
        public float Score { get; set; }
    }

    public class ProductEntry
    {
        [KeyType(count: 262111)]
        public float Label { get; set; }

        [KeyType(count: 262111)]
        public uint ProductID { get; set; }

        [KeyType(count: 262111)]
        public uint CoPurchaseProductID { get; set; }
    }

得到错误:

System.ArgumentOutOfRangeException: '用 KeyType 属性标记的成员标签,但似乎不是键类型的有效数据类型(参数'userType')'

任何想法?多谢你们!

标签: c#machine-learningmicrosoft.ml

解决方案


删除属性的[KeyType(count: 262111)]注释public float Label { get; set; }

Label不是钥匙


推荐阅读