Microsoft.ML Adding Label when using LoadFromEnumerable

370 views Asked by At

There is something that is not working, I cannot figured out... I want to load data from a List. I have try several codes

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();

I am getting a problem about a "Label" This is the classes:

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; }
    }

Getting the error:

System.ArgumentOutOfRangeException: 'Member Label marked with KeyType attribute, but does not appear to be a valid kind of data for a key type (Parameter 'userType')'

Any idea? Thanks guys!

1

There are 1 answers

0
Alvin Chung On BEST ANSWER

Remove the [KeyType(count: 262111)] annotation for public float Label { get; set; } property

Label is not a key