lyst / lightfm

A Python implementation of LightFM, a hybrid recommendation algorithm.
Apache License 2.0
4.66k stars 679 forks source link

LightFM rerecommending items #664

Open jessicaeggen opened 1 year ago

jessicaeggen commented 1 year ago

I am implementing a recommender system (python 3.9) for an online supermarket (hence users can re-order products). All the data is in the correct format which led to an interaction matrix, a weights matrix (to show the importance of some of the interactions), user features and item features. To make sure I don't get the warning 'Test interactions matrix and train interactions matrix share .. interactions', I have excluded all the interactions from the test set. So if a user buys bananas every week, in the test set we don't have an interaction for bananas. However, we only use this for evaluating, so this should not have anything to do with the predictions.

Aside from the similar products, I was also wondering if LightFM can recommend previously bought items.

dataset = Dataset()
dataset.fit(
        user_ids_train_dict.keys(), # all the users
        item_ids_train_dict.keys(), # all the items
        item_features = item_features, # additional item features
        user_features = user_features # additional user features
)

 model.fit(
    interaction_matrix_train,
    epochs=100,
    user_features=user_features,
    item_features=item_features,
    sample_weight = weights_matrix_train,
    num_threads=2,
    verbose = True
)

# interaction matrix that only shows the items that weren't bought before    
interaction_matrix_val_new = interaction_matrix_val - interaction_matrix_train
interaction_matrix_val_new[interaction_matrix_val_new < 0 ] = 0

evaluator.evaluate(
    epochs,
    model,
    interaction_matrix_train,
    interaction_matrix_val_new,
    user_features,
    item_features)