user_features 中的 Lightfm 特征数量不正确

Lightfm Incorrect number of features in user_features

我正在构建推荐系统 - 在 Lightfm 中混合。 我的数据有 39326 个唯一用户和 2569 个唯一游戏名称(项目)。 我的火车交互稀疏矩阵的形状为:<39326x2569 稀疏矩阵类型为“” 以压缩稀疏行格式存储了 758931 个元素> 我的测试交互稀疏矩阵的形状为:<39323x2569 类型为 '' 的稀疏矩阵 以压缩稀疏行格式存储了 194622 个元素>

我训练模型:model1 = LightFM(learning_rate=0.01, loss='warp') model1.fit(train_interactions,
时代= 20) 创建对象: 但是当我尝试通过以下方式检查准确性时: train_precision = precision_at_k(model1, train_interactions, k=10).mean() test_precision = precision_at_k(model1, test_interactions, k=10).mean()

我收到错误消息:user_features 中的要素数量不正确 为什么???显然形状兼容?我错过了什么?

您的测试稀疏矩阵的维度为 39323x2569,而您的训练稀疏矩阵的维度为 39326x2569。您的测试集中缺少 3 个用户。

我建议你使用 lightfm 内置的 train/test 分割函数来避免错误:https://making.lyst.com/lightfm/docs/cross_validation.html

如果你想按照自己的方式拆分数据,你也可以将你的user_id和item_id转换为从0开始的连续整数。然后使用这个:

from lightfm.data import Dataset
# Create your train and test set in the format [[user_id1, item_id1, score1], ..., [user_idn, item_idn, scoren]] 
# Your score can be just 1 for an implicit interaction
# user_id and item_id are integers

data = Dataset()
data.fit(unique_user_ids, # list from 0 to n_users 
         unique_item_ids # list from 0 to n_items
        )
train, weights_matrix = data.build_interactions([tuple(i) for i in train])
test, weights_matrix = data.build_interactions([tuple(i) for i in test])