Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove users/items ?? #5

Open
johannbuscail opened this issue Nov 28, 2020 · 0 comments
Open

Remove users/items ?? #5

johannbuscail opened this issue Nov 28, 2020 · 0 comments

Comments

@johannbuscail
Copy link

Hey,
First, thanks for providing this amazing files that helped me a lot.
I'm trying to create a recommendation system, and I was wondering how to change/remove user/items/ratings.
I've created this function to add stuff (they work):

# add a user
def addUsers(interactions, model, users=[], user_dict={}, epoch=5, n_jobs=4):
    '''
    Adds ratings for a user
    Required Input -
        - interactions: the matrix containg each ratings for a user/item
        - model: the lightfm model
        - users: All the user ids of the new users
        - user_dict: Dictionary type output containing item_id as key and item_name as value
        - epoch = number of epochs to run
        - n_jobs = number of cores used for execution

    Expected Output -
        - interactions: The new matrix containing the new user
        - model: The new model trained with the new user
    '''
    for user_id in users:
        interactions.loc[user_id] = 0
        user_dict[user_id] = interactions.shape[0] - 1
    val = [[0 for y in range(interactions.shape[1])]
           for x in range(interactions.shape[0])]
    x = sparse.csr_matrix(val)
    model.fit_partial(interactions=x, epochs=epoch, num_threads=n_jobs)
    return interactions, user_dict, model

#add an item
def addItems(interactions, model, items=[], items_dict={}, epoch=5, n_jobs=4):
    for item_id, title in items:
        interactions[item_id] = 0
        items_dict[item_id] = title
    val = [[0 for y in range(interactions.shape[1])]
           for x in range(interactions.shape[0])]
    x = sparse.csr_matrix(val)
    model.fit_partial(interactions=x, epochs=epoch, num_threads=n_jobs)
    return interactions, items_dict, model

#add a rating
def addRatings(interactions,
               model,
               user_dict={},
               user=0,
               items=[0],
               ratings=[0],
               epochs=30,
               n_jobs=4):
    '''
    Adds ratings for a user
    Required Input -
        - interactions: the matrix containg each ratings for a user/item
        - model: the lightfm model
        - user_dict: Dictionary type output containing item_id as key and item_name as value
        - user: The user id
        - ratings: Array of ratings for each item -> [[ item, rating ]]
        - epoch = number of epochs to run 
        - n_jobs = number of cores used for execution

    Expected Output -
        - interactions: The new matrix containing new ratings
        - model: The new model trained with new ratings
    '''
    for item, rating in ratings:
        interactions[item][user] = rating
        uindex = user_dict[user]
        val = []
        for user_index in range(interactions.shape[0]):
            val.append(interactions.loc[user].values if user_index ==
                       uindex else [0 for x in range(interactions.shape[1])])
        x = sparse.csr_matrix(val)
        model.fit_partial(interactions=x, epochs=epochs, num_threads=n_jobs)
    return interactions, model

All of them work !
But how can I do to remove a rating or a user.
I don't know how to do that because fit_partial method is made to add data and not to remove
And finally, I was asking myself if setting a rating to 0 would be the same as just removing it
Thanks in advance !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant