c# - 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')'
任何想法?多谢你们!
解决方案
删除属性的[KeyType(count: 262111)]
注释public float Label { get; set; }
Label
不是钥匙
推荐阅读
- angular - CORS 发布 Ionic 4、WordPress 5.2 和 JWT 身份验证
- javascript - JavaScript new Function() this 绑定
- java - AcceptHeaderLocaleResolver 的 defaultLocale 不起作用
- html - CSS 图像在我的代码中显得非常小
- c++ - Visual Studio 2019 不打开带有源文件的窗口并且不定位到错误
- java - 如何使用 add 将字符添加到数组中?
- python - 为什么 import scrapy.utils.project 可以加载设置?
- intellij-idea - 如何从 IDEA 的“TODO”扫描中排除 node_modules?
- python - 如何在没有附加模块的情况下在 python 中读取 RGB 图像(JPG)
- rust - 如何从字符串创建响应?(Rust-Rocket)