Use Object_id as indices in python (Cosine Similarity)

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am using pandas data frame which have '_id' as object id of the News Article. The problem is that when i use '_id' as indices, it shows an error

**TypeError: 'str' object cannot be interpreted as an integer TypeError Traceback (most recent call last) C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) 3108 try: -> 3109 return libindex.get_value_box(s, key) 3110 except IndexError:

pandas_libs\index.pyx in pandas._libs.index.get_value_box()

pandas_libs\index.pyx in pandas._libs.index.get_value_box()** df.head() Result

Here is my code

indices = pd.Series(ds.index, index=ds['_id']).drop_duplicates()


   def get_recommendations(id, cosine_sim=cosine_sim):


    idx = indices[id]


    sim_scores = list(enumerate(cosine_sim[idx]))


    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)


    sim_scores = sim_scores[1:11]
    sc=[]
    for count,ele in sim_scores: 
       sc.append(ele)




    movie_indices = [i[0] for i in sim_scores]
    ff = pd.DataFrame(columns=['A','B','C'])
    ff['A']=ds['_id'].iloc[movie_indices]
    ff['B']=ds['title'].iloc[movie_indices]
    ff['C']=sc


   return ff

get_recommendations('4fd2b43e8eb7c8105d8a67e8')

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.