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

Python issue #705

Open
tangbufanwei opened this issue Jan 18, 2024 · 4 comments
Open

Python issue #705

tangbufanwei opened this issue Jan 18, 2024 · 4 comments

Comments

@tangbufanwei
Copy link

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
Copy link
Author

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
Copy link
Owner

benfred commented Jan 18, 2024

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
Copy link
Author

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
Copy link
Author

and my sparse matrix looks like this
image

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

2 participants