首页 > 解决方案 > ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all

问题描述

嗨,我正在创建一个推荐系统程序,我想按升序对余弦分数进行排序。我正在使用来自数据集链接的数据集。错误是

  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-106-dff186a5ac63> in <module>
----> 1 get_recommendations('Manager').head(5)

<ipython-input-105-4cf231f5a42d> in get_recommendations(title, cosine_sim)
      8 
      9     # Sort the movies based on the similarity scores
---> 10     sim_scores= sim_scores.sort(key=lambda x:x[1],reverse=True)
     11     #sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
     12 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

该错误显示在下面的错误图片链接中。有人知道如何解决此错误吗?

这是我的代码

#Import TfIdfVectorizer from scikit-learn
from sklearn.feature_extraction.text import TfidfVectorizer

#Define a TF-IDF Vectorizer Object. Remove all english stop words such as 'the', 'a'
tfidf = TfidfVectorizer(stop_words='english')

#Replace NaN with an empty string
df['Skills'] = df['Skills'].fillna('')

#Construct the required TF-IDF matrix by fitting and transforming the data
tfidf_matrix = tfidf.fit_transform(df['Skills'])

# Import linear_kernel
from sklearn.metrics.pairwise import linear_kernel

# Compute the cosine similarity matrix
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
indices = pd.Series(df.index, index=df['Level']).drop_duplicates()

# Function that takes in job title as input and outputs most similar job
def get_recommendations(title, cosine_sim=cosine_sim):
    # Get the index of the job that matches the title
    idx = indices[title]

    # Get the pairwsie similarity scores of all job with that job
    sim_scores = list(enumerate(cosine_sim[idx]))

    # Sort the job based on the similarity scores
    sim_scores= sim_scores.sort(key=lambda x:x[1],reverse=True)
    #sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

# Get the job indices
job_indices = [i[0] for i in sim_scores]

# Return the similar job
return df['Title'].iloc[job_indices]

错误图片

标签: python-3.xsortingrecommendation-enginesortedlist

解决方案


推荐阅读