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

[ASK] How to rank given items? #361

Closed
wahyudierwin opened this issue Oct 9, 2020 · 7 comments
Closed

[ASK] How to rank given items? #361

wahyudierwin opened this issue Oct 9, 2020 · 7 comments

Comments

@wahyudierwin
Copy link

Description

Hi, hope that you are doing well.

I want to ask about ranking. Suppose that there are 100 items in the training set and we want to rank items 0, 12, 23, and 79. How can I do that?

Thank you.

Other Comments

@saghiles
Copy link
Member

saghiles commented Oct 9, 2020

Hi, you can specify the list of items to rank in the Recommender.rank() function.

@wahyudierwin
Copy link
Author

wahyudierwin commented Oct 9, 2020

When I run,

r = model.rank(user_idx=1,item_indices=[0,12,23,79])

it results in

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-247-37619b116839> in <module>
----> 1 r = model.rank(user_idx=1,item_indices=[0,12,23,79])

~/.local/lib/python3.6/site-packages/cornac/models/recommender.py in rank(self, user_idx, item_indices)
    280             item_scores = all_item_scores[: len(item_indices)]
    281             item_rank = item_scores.argsort()[::-1]
--> 282             item_scores = item_scores[item_indices]
    283 
    284         return item_rank, item_scores

IndexError: index 12 is out of bounds for axis 0 with size 4

.
Am I missing something?

@saghiles
Copy link
Member

saghiles commented Oct 11, 2020

Thanks for raising this issue. The current assumption with '''item_indices''' is that the list of item indices are continuous (no missing indices) from 0 to len(item_indices). This assumption is restrictive and it would not allow you to solve your problem. We are working on improving it.

In the mean time you can rank all items and post-process the ranked list to keep only the items of interest.

Btw Cornac uses its own indices, please take a look at the Datasets documentation if you want to map back to original ids or vice versa

@tqtg
Copy link
Member

tqtg commented Oct 11, 2020

When I run,

r = model.rank(user_idx=1,item_indices=[0,12,23,79])

it results in

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-247-37619b116839> in <module>
----> 1 r = model.rank(user_idx=1,item_indices=[0,12,23,79])

~/.local/lib/python3.6/site-packages/cornac/models/recommender.py in rank(self, user_idx, item_indices)
    280             item_scores = all_item_scores[: len(item_indices)]
    281             item_rank = item_scores.argsort()[::-1]
--> 282             item_scores = item_scores[item_indices]
    283 
    284         return item_rank, item_scores

IndexError: index 12 is out of bounds for axis 0 with size 4

.
Am I missing something?

@wahyudierwin it seems that your training data only has 4 items. Could you share your code?

@wahyudierwin
Copy link
Author

Sorry for the late reply.

In the mean time you can rank all items and preprocess the ranked list to keep only the items of interest.

@saghiles thanks for your suggestion, probably I will do as you suggest.

@tqtg I'm sure that the training data has more than 4 items. This is the code, and I'm afraid I cannot share the data because of confidentiality.

train_set = cornac.data.Dataset.from_uir(data.itertuples(index=False), seed=42)
model = cornac.models.BPR(k=32,
                            max_iter=100,
                            learning_rate=0.01,
                            lambda_reg=0.001,
                            verbose=True,
                            seed=42)
model.fit(train_set)

r = model.rank(user_idx=1,item_indices=[0,12,23,79])

@tqtg
Copy link
Member

tqtg commented Oct 15, 2020

@wahyudierwin thank you for providing the details. We have now fixed the rank() function to work with an arbitrary list of item_indices, in the latest release version 1.8.0. You can give it a try and see if it solves your problem. Thanks!

@wahyudierwin
Copy link
Author

@tqtg very glad to hear that! I will give it a try as soon as possible. Thank you very much!

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

3 participants