lightfm icon indicating copy to clipboard operation
lightfm copied to clipboard

Cannot run predictions in batches

Open maffka123 opened this issue 4 years ago • 2 comments

Hi ,

I am trying now to predict for batch users, so I am using your suggestion from issue #240. I am getting an error after first batch. Do I understand correctly, that in liпhtfm.py on the row 814: n_users = user_ids.max() + 1 , meaning that if I will send users_ids for the first batch, it will work, but starting from second not, because user_ids=[7,7,7,8,8,8] and features [1,2,3,1,2,3] will give n_users=9 and user_features.shape[0]=6 and throught an error "Number of user feature rows does not equal the number of users" What am doing wrong?

Originally posted by @maffka123 in https://github.com/lyst/lightfm/issues/240#issuecomment-878569973

maffka123 avatar Jul 15 '21 19:07 maffka123

I am a novice at LightFM so I advise you to look at the code yourself about what I am saying. But for your question:

Within predict() method, the parameter "user_ids" we use as the representation of users isn't the same user IDs we have fitted our model with, the predict parameter IDs does not point to our initial user_ids list, they are independent of each other. Instead, these ids within predict method work as the indices of user_features within the predict method parameters.

In other words when you try to use user_ids=[7,7,7,8,8,8], the predict method tries to find the 8th indexed element of user_features you put inside predict parameters which doesn't exist. Instead of that, you should simply change [7,7,7,8,8,8] into [0,0,0,1,1,1]. This wouldn't cause a problem since user_ids in this context simply point at user_features[0] or user_features[1] depending on their ids.

This fact essentially let's us predict the recommendations of unseen users with the help of their known features without having to add them into our datasets.

ilkeremrekoc avatar Nov 18 '21 10:11 ilkeremrekoc

@ilkeremrekoc Thanks a lot, I was getting the same error. Now it works!!

rodfloripa avatar Jan 25 '22 16:01 rodfloripa