我正在通过以下代码创建数据集:
from lightfm.data import Dataset
from lightfm import LightFM
dataset = Dataset()
dataset.fit((row['id'] for row in user_queryset.values()),
(row['id'] for row in item_queryset.values()))
num_users, num_items = dataset.interactions_shape()
(interactions_sparse_matrix, weights) = dataset.build_interactions(
(
(
row['user_id']
,row['item_id']
,row['weight']
)
)
for row in queryset.values()
)
dataset.fit_partial(
items=(x['item_id'] for x in items_list),
item_features=(x['feature_id'] for x in item_features_list)
)
dataset.fit_partial(
users=(x['user_id'] for x in users_list),
user_features=(x['feature_id'] for x in user_features_list)
)
item_features = dataset.build_item_features(
((x['item_id'], [x['property_id']])
for x in item_features_list))
user_features = dataset.build_user_features(
((x['user_id'], [x['property_id']])
for x in user_features_list))
我通过以下方式生成火车模型:
model = LightFM(loss='bpr')
model.fit(
interactions_sparse_matrix
,item_features=item_features
,user_features=user_features
)
然后我使用cosine_similarity
方法sklearn
来获得相似之处:
from scipy import sparse
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
users_sparse_matrix = sparse.csr_matrix(users_embed)
similarities = cosine_similarity(users_sparse)
但是当打印similarities.shape
它的回报时:
(14, 14)
虽然我有 5 个用户并且我认为它必须是 (5,5) ,但我错了吗?像这样的矩阵:
1 0.2 0.8 0.4 0.6
0.2 1 ... ... ...
0.8 ... 1 ... ...
0.4 ... ... 1 ...
0.6 ... ... ... 1
如何让用户及其分数推荐给用户?谢谢
我的 LightFM 版本是:1.15
我使用python 3.6