I am training an ALS model using Implicit library to generate recommendations.
First, I prepare the data as follows, with interactions_data as a dataframe of 3 columns [user_id, item_id, interaction_strength]:
import implicit
from scipy.sparse import csr_matrix
item_user_interactions = pd.pivot_table(interactions_data, index='item_id', columns='user_id', values='interaction_strength', fill_value=0)
item_user_interactions = csr_matrix(item_user_interactions.values)
from implicit.nearest_neighbours import bm25_weight
# weight the matrix, to reduce impact of users that have purchased the same item thousands of times and to reduce the weight given to popular items
item_user_interactions = bm25_weight(item_user_interactions, K1=100, B=0.8)
# get the transpose since the most of the functions in implicit expect (user, item) sparse matrices instead of (item, user)
user_interactions = item_user_interactions.T.tocsr()
The I train the ALS model
from implicit.als import AlternatingLeastSquares
model = AlternatingLeastSquares(factors=64, regularization=0.05, alpha=2.0)
model.fit(user_interactions)
Finally, I generate recommendations
userid = 0
ids, scores = model.recommend(userid, user_interactions[userid], N=10, filter_already_liked_items=False)
The problem is I don’t know which user_id does this index (userid) refer to since the model deals with indices of the sparse matrix rather than real IDs inserted in the dataset. How can I map the generated indices to the original user_ids and item_ids in my dataset?