Как убрать ошибку ключа из программы? - PullRequest
0 голосов
/ 17 марта 2020

На изображении вы можете видеть, что у меня есть идентификатор, который все еще получает ошибку ключа Я пытаюсь сделать алгоритм рекомендаций, поэтому я получил эту ошибку

 #the first argument in the below function to be passed is the id of the book, second argument is the number of books you want to be recommended#

    KeyError: <built-in function id>

Я делюсь ссылкой article https://towardsdatascience.com/recommender-engine-under-the-hood-7869d5eab072

      import pandas as pd
      from sklearn.feature_extraction.text import TfidfVectorizer
      from sklearn.metrics.pairwise import linear_kernel
      ds = pd.read_csv("test1.csv") #you can plug in your own list of products or movies or books here as csv file#
      tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=0, stop_words='english')

#ngram explanation begins#
#ngram (1,3) can be explained as follows#
#ngram(1,3) encompasses uni gram, bi gram and tri gram
#consider the sentence "The ball fell"
#ngram (1,3) would be the, ball, fell, the ball, ball fell, the ball fell
#ngram explanation ends#

      tfidf_matrix = tf.fit_transform(ds['Book Title'])
      cosine_similarities = linear_kernel(tfidf_matrix, tfidf_matrix)
      results = {} # dictionary created to store the result in a dictionary format (ID : 
      (Score,item_id))#
      for idx, row in ds.iterrows(): #iterates through all the rows

# the below code 'similar_indice' stores similar ids based on cosine similarity. sorts them in ascending 
      order. [:-5:-1] is then used so that the indices with most similarity are got. 0 means no similarity and 1 means perfect similarity#
      similar_indices = cosine_similarities[idx].argsort()[:-5:-1] 

    #stores 5 most similar books, you can change it as per your needs
     similar_items = [(cosine_similarities[idx][i], ds['ID'][i]) for i in similar_indices]
     results[row['ID']] = similar_items[1:]

#below code 'function item(id)' returns a row matching the id along with Book Title. Initially it is a dataframe, then we convert it to a list#
      def item(id):
         return ds.loc[ds['ID'] == id]['Book Title'].tolist()[0]
      def recommend(id, num):
          if (num == 0):
           print("Unable to recommend any book as you have not chosen the number of book to be 
    recommended")
        elif (num==1):
          print("Recommending " + str(num) + " book similar to " + item(id))

        else :
           print("Recommending " + str(num) + " books similar to " + item(id))

       print("----------------------------------------------------------")
          recs = results[id][:num]
             for rec in recs:
                print("You may also like to read: " + item(rec[1]) + " (score:" + str(rec[0]) + ")")

#the first argument in the below function to be passed is the id of the book, second argument is the number of books you want to be recommended#
        recommend(5,2)

Я попытался успешно запустить, пока переменная результатов не получила ошибку.

Ответы [ 2 ]

1 голос
/ 19 марта 2020

потому что python ключевое слово id по умолчанию вызывается, когда вы вызываете "def item (id):" вместо id, вы должны объявить другой идентификатор .... тогда я думаю, что это единственная причина для ключевой ошибки ..

0 голосов
/ 17 марта 2020

Поскольку ошибка предполагает, что id является встроенной функцией в python -3. Поэтому, если вы измените имя параметра id в def item(id) и def recommend(id, num) и все их ссылки, тогда код должен работать.

После изменения идентификатора и исправления отступа пример может выглядеть следующим образом:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel

ds = pd.read_csv("test1.csv")  # you can plug in your own list of products or movies or books here as csv file
tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=0, stop_words='english')

# ngram explanation begins#
# ngram (1,3) can be explained as follows#
# ngram(1,3) encompasses uni gram, bi gram and tri gram
# consider the sentence "The ball fell"
# ngram (1,3) would be the, ball, fell, the ball, ball fell, the ball fell
# ngram explanation ends#

tfidf_matrix = tf.fit_transform(ds['Book Title'])
cosine_similarities = linear_kernel(tfidf_matrix, tfidf_matrix)
results = {}  # dictionary created to store the result in a dictionary format (ID : (Score,item_id))

for idx, row in ds.iterrows():  # iterates through all the rows

    # the below code 'similar_indice' stores similar ids based on cosine similarity. sorts them in ascending 
    # order. [:-5:-1] is then used so that the indices with most similarity are got. 0 means no similarity and 
    # 1 means perfect similarity
    similar_indices = cosine_similarities[idx].argsort()[:-5:-1]

    # stores 5 most similar books, you can change it as per your needs
    similar_items = [(cosine_similarities[idx][i], ds['ID'][i]) for i in similar_indices]
    results[row['ID']] = similar_items[1:]


# below code 'function item(id)' returns a row matching the id along with Book Title. Initially it is a dataframe, 
# then we convert it to a list#
def item(ID):
    return ds.loc[ds['ID'] == ID]['Book Title'].tolist()[0]


def recommend(ID, num):
    if num == 0:
        print("Unable to recommend any book as you have not chosen the number of book to be recommended")
    elif num == 1:
        print("Recommending " + str(num) + " book similar to " + item(ID))

    else:
        print("Recommending " + str(num) + " books similar to " + item(ID))

    print("----------------------------------------------------------")
    recs = results[ID][:num]
    for rec in recs:
        print("You may also like to read: " + item(rec[1]) + " (score:" + str(rec[0]) + ")")


# the first argument in the below function to be passed is the id of the book, second argument is the number of books 
# you want to be recommended
recommend(5, 2)
...