python - 字符串的余弦相似度作为运算符的输入 - FLOAT ERROR
问题描述
我想创建一种比较器,它在输入中给出一个句子,从一个运算符,程序通过余弦相似度评估,如果数据集中有类似的句子。到目前为止我所做的是:
text = pd.read_excel("Database1.xlsx", usecols='C'))
new_input = input('Insert the sentence: ')
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(analyzer='word', stop_words='english')
tfidf_matrix = tfidf_vectorizer.fit_transform(text)
print(tfidf_matrix.shape)
from sklearn.metrics.pairwise import cosine_similarity
a = cosine_similarity(new_input, tfidf_matrix)
print(a)
但我收到一条错误消息:
ValueError: could not convert string to float: 'hello world'
我尝试通过以下方式解决它:
new_input = float(input('Insert the sentence: '))
如果我正确理解,问题是由于存在空格而无法将输入字符串“hello world”转换为浮点数,但我需要在输入中存在空格。
我该如何解决这种情况?非常感谢您的帮助
解决方案
推荐阅读
- javascript - 如何在javascript中更改urlpath
- javascript - 从使用 Array.join(' ') 创建的字符串中删除元素。如何在开头保留空格?
- docker - “docker create”和“docker start”之间的“-a”和“-i”有什么区别
- android - 如何在片段管理器中使用扩展方法
- c - 修改linux中注册字符设备中的文件操作
- mysql - mysql错误操作数应该有1列存储过程
- powershell - 自定义徽标/壁纸更改
- javascript - 关于逻辑运算符&&的理解题
- matlab - 根据对象边界框裁剪旋转图像 - Matlab
- common-lisp - 如何将 LispWorks 的快速目录文件与 fdf-handle-* 函数一起使用?