Mojtaba Arvin
Mojtaba Arvin

Reputation: 739

How can I generate users to user recommandation via LightFM python package?

I'm creating a dataset by following codes :

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

and I generating a train model by :

model = LightFM(loss='bpr')
model.fit(
        interactions_sparse_matrix
        ,item_features=item_features
        ,user_features=user_features
        )

Then I use cosine_similarity method of sklearn to get similarities :

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)

But when print similarities.shape its return :

(14, 14)

While I have 5 users and I think its must be (5,5) , am I wrong? something like this matrix:

1    0.2   0.8    0.4    0.6
0.2   1    ...    ...    ...
0.8  ...    1     ...    ...
0.4  ...   ...     1     ...
0.6  ...   ...    ...     1

How can I get users and its scores to recommand to a user? thanks

My LightFM version is : 1.15

And I use python 3.6

Upvotes: 2

Views: 809

Answers (1)

mahtab
mahtab

Reputation: 46

The problem is not with your code. There is a misunderstanding with the concept of user_embedding. The user_embedding matrix is the matrix with the number of user features as row and the number of components as a column. when you have this matrix, for getting the similarities between each user with cosine similarity, you need to multiply a user_feature matrix with user_embedding, and finally compute the cosine similarity of the dot product of a user_feature matrix with a user_embedding matrix.

Upvotes: 3

Related Questions