implicit icon indicating copy to clipboard operation
implicit copied to clipboard

Out of Bounds Index Error when using Evaluation functions

Open murphystout opened this issue 6 years ago • 4 comments

Hi All, First off, thanks so much for all the work in this great library. I've learned so much and its truly a remarkable feat.

Now my issue:

I'm able to successfully train a model and interface with it using the RecommenderBase functions. However when I doubled back to do some evaluation of the model performance I immediate ran into an annoying index error.

Here is a reproducible example (from here: https://github.com/murphystout/implicit-feedback-ml/tree/main)

import numpy as np import pandas as pd import implicit from implicit import evaluation as ev import time import scipy import random from sklearn import metrics import scipy.sparse as sparse

df_wide = pd.read_csv("https://raw.githubusercontent.com/murphystout/implicit-feedback-ml/main/df_wide.csv") csr = scipy.sparse.csr_matrix(df_wide.values) train, test = ev.train_test_split(csr) model = implicit.als.AlternatingLeastSquares()

model.fit(train) ev.ranking_metrics_at_k(model = model, train_user_items = train, test_user_items = test)

It leads to the following error:

File "", line 17, in ev.ranking_metrics_at_k(model = model, train_user_items = train, test_user_items = test)

File "implicit/evaluation.pyx", line 251, in implicit.evaluation.ranking_metrics_at_k

File "implicit/recommender_base.pyx", line 170, in implicit.recommender_base.MatrixFactorizationBase.recommend

File "implicit/recommender_base.pyx", line 320, in implicit.recommender_base.MatrixFactorizationBase._user_factor

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

Any help is greatly appreciated!

murphystout avatar Oct 30 '20 01:10 murphystout

@murphystout I think I have the same issue at the moment, did you find a solution?

nicolaneo avatar Nov 19 '20 12:11 nicolaneo

Nope, unfortunately not.

On Nov 19, 2020, at 7:05 AM, Nicola Neophytou [email protected] wrote:

 @murphystout I think I have the same issue at the moment, did you find a solution?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

murphystout avatar Nov 19 '20 13:11 murphystout

If I'm not wrong, when training with fit() method you have to pass a matrix of (rows: items, cols: users, values: ratings), but when I use the method recommend() I have to pass a matrix of (rows: users, cols: items, values: ratings).
Seems that ranking_metrics_at_k() calls recommend() method, so I think you have to transpose the matrix when evaluating or recommending.

igorkf avatar Nov 26 '20 12:11 igorkf

Works! Thank you!

Solution is also mentioned in #433 by Lorenzo. Simply using the .transpose() method on each of the matrices you pass to the evaluation functions.

murphystout avatar Mar 10 '21 23:03 murphystout