python-3.x - 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 - 如何在 python 中将 YAML 转换为 CSV
- arrays - `.fit()` 的输入应该有 4 级。得到的数组形状为:(777, 1, 256, 256, 3)
- scala - 如何确保内联类型匹配(scala 3)中的两个参数引用相同的类型
- python - 使用 Selenium Python 在 Whatsapp 网页中滚动到聊天列表的末尾
- python - 返回 DataFrame [Pandas] 中数据组的位置(行和列)
- elixir - 使用地图枚举中的新键创建地图
- node.js - node.js,猫鼬 - findOne({ _id: req.params.id }) 不起作用
- python - SyntaxError:f 字符串表达式部分不能包含反斜杠
- blazor - Blazor 组件生命周期 - 在设置参数后但在绑定之前如何在组件中执行异步操作
- python - Selenium 无效参数异常 - python