benfred / implicit

Fast Python Collaborative Filtering for Implicit Feedback Datasets
https://benfred.github.io/implicit/
MIT License
3.57k stars 612 forks source link

Python issue #705

Open tangbufanwei opened 10 months ago

tangbufanwei commented 10 months ago

Hi guys i met trouble when i was trying to create a recommender system in python by als(alternating least square). Here is the code. user_item = df_no_duplicates.pivot(index='userId', columns='productId', values='rating').fillna(0) print('Shape of User-Item sparse matrix:', user_item.shape) user_item.head() model1 = implicit.als.AlternatingLeastSquares(factors=20, regularization=0.1, iterations=20) user_item_sparse1 = sparse.csr_matrix(user_item) alpha_val = 40 data_conf = (item_user_sparse1 * alpha_val).astype('double') model1.fit(data_conf) model1.recommend(0,user_item_sparse1[0]) and i got the error index 945 is out of bounds for axis 1 with size 240 in method i found on github, if i use model1.recommend(0,user_item_sparse1) it will turn out to be user_items must contain 1 row for every user in userids i checked the columns of the sparse matrix and the origin one they are exactly the same, so can anyone help me regarding this? THANKS A LOT!

tangbufanwei commented 10 months ago

model1.recommend(0,user_item_sparse1[0]) THIS IS THE METHOD I FOUND ON GITHUB BUT SEEMS ITS NOT WORKING ON MY SIDE AS WELL.

benfred commented 10 months ago

I think your issue is here:

data_conf = (item_user_sparse1 * alpha_val).astype('double')
model1.fit(data_conf)
model1.recommend(0,user_item_sparse1[0])

The fit methods take a user_item sparse matrix , https://benfred.github.io/implicit/api/models/cpu/als.html#implicit.cpu.als.AlternatingLeastSquares.fit instead of the item_user sparse matrix that you are passing it.

Does calling the fit function with the correct argument work for you?

model1.fit(user_item_sparse1)
model1.recommend(0,user_item_sparse1[0])
tangbufanwei commented 10 months ago

Thank for your reply but i got image several weird arrays, i am not sure if it's correct, but finally it's not red anymore.

tangbufanwei commented 10 months ago

and my sparse matrix looks like this image