Reputation: 13
def item(id):
return posts.loc[posts['post_id'] == id]['title'].tolist()[0]
def get_recommendations(postid, num, indices):
idx = indices[postid]
sim_scores = list(enumerate(cosine_similarities[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:num+1]
# print(sim_scores)
indices = [i[0] for i in sim_scores]
print("Recommending " + str(num) + " posts similar to \"" + item(postid) + "\" ...")
return posts.iloc[indices]
This is my code and when I run it I get the following error:
IndexError Traceback (most recent call last)
<ipython-input-58-54719cff7892> in <module>()
----> 1 show_recomm('5d6d39567fa40e1417a4931c', 10, indices)
1 frames
<ipython-input-45-dfd12eb7d387> in show_recomm(postid, num, indices)
6 print(similarity_scores)
7 indices = [i[0] for i in similarity_scores]
----> 8 print("Recommending " + str(num) + " posts similar to \"" + item(postid) + "\" ...")
9 return posts.iloc[indices, :-1]
<ipython-input-44-6739fecf9da1> in item(id)
1 def item(id):
----> 2 return posts.loc[data['post_id'] == id]['title'].tolist()[0]
IndexError: list index out of range
I get this error when I try the function with different post ids. For some, I get the output, for other ids I don't.
Upvotes: 0
Views: 369
Reputation: 536
Based on what I can see above. It seems it's due to
data['post_id'] == id
This would be either true or false. Did you mean
def item(id):
return posts.loc[id]['title'].tolist()[0]
Upvotes: 1