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

model.similar_items but model.similar_users function not found #33

Closed
shuangshui opened this issue Jun 14, 2017 · 4 comments
Closed

model.similar_items but model.similar_users function not found #33

shuangshui opened this issue Jun 14, 2017 · 4 comments

Comments

@shuangshui
Copy link

I don't know if I get this right,
but since
related = model.similar_items(itemid,K)
return the best K similar itemID,
how can I find get the best K similar userID list given some userID?

looking forward to your reply

@benfred
Copy link
Owner

benfred commented Jun 14, 2017

There is no function call built into the models right now, but it should be easy to add (at least for the ALS code).

Calling this function with an AlternatingLeastSquares model should work (basically just taking cosine distance in the user_factors):

def similar_users(model, userid, N=10):
    # note: recalculating norms per call is maybe not the most efficient
    user_norms = np.linalg.norm(model.user_factors, axis=-1)

    scores = model.user_factors.dot(model.user_factors[userid]) / user_norms
    best = np.argpartition(scores, -N)[-N:]
    return sorted(zip(best, scores[best] / user_norms[itemid]), key=lambda x: -x[1])

@shuangshui
Copy link
Author

shuangshui commented Jun 15, 2017

thank you! And this solved my problem.
Another issue is how can I get the predicted value from a given user and item.
My code is like this:
t_pred=model.user_factors[t_user_index,:].dot(model.item_factors.transpose())[t_item_index]
then I tried t_pred=t_pred/model.item_norms[t_item_index]...but still not right.
Because the actual value is much bigger than I get.
Could you please give some ideas on this too!
thanks~~~

@benfred
Copy link
Owner

benfred commented Jun 15, 2017

for a single user/item pair - the predicted value is just the dot product of the user factors and the item factors: model.user_factors[userid].dot(model.item_factors[itemid])

@shuangshui
Copy link
Author

Thanks for your reply
I'll check my data further~~~

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