Collaborative filtering predict

I have a Fast ai collaborative filtering model. I would like to predict on this model for a new tuple. I am having trouble with the predict function

From their documentation,

Signature: learn.predict(item, rm_type_tfms=None, with_input=False)
Docstring: Prediction on `item`, fully decoded, loss function decoded and probabilities
File:      ~/playground/virtualenv/lib/python3.8/site-packages/fastai/
Type:      method

How do I define the Item that I need to pass. Lets say for a movielens dataset, for a user already with in the dataset, we would like to recommend a set of movies, how do we pass the userID?

I have tried to follow somewhat of an answer here - Making predictions with collaborative filtering
and Collaborative filtering predictions

learn.predict( [np.array([3])] )

I seem to get an error: TypeError: list indices must be integers or slices, not list Appreciate any pointers.

Thanks, Shekar

Not familiar with fastai’s collaborative filtering modules, and I received the same error as you, but a hacky workaround is:

row = pd.DataFrame({'user': [user_id, 0],
                    'item': [item_id, 0]})
# Turn your data into a DataLoader. 
# Note how unlike in learn.predict, 'row' isn't placed in a list ([row])
row = learn.dls.test_dl(row)
# learn.get_preds returns a tuple of (predictions, labels)
# The first item in 'predictions' corresponds to our data point
pred = learn.get_preds(dl=row)[0][0]

I’m not sure whether this is a bug, or if learn.predict for collaborative models is meant to be used differently, but whichever the case, I think the code above should work fine for your use case.

Have a nice weekend!