- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要计算具有已计算的 TFIDF 分数的文档之间的余弦相似度。
通常我会使用(例如)TFIDFVectorizer这将创建一个文档/术语矩阵,并计算 TFIDF 分数。我无法应用此方法,因为它将重新计算 TFIDF 分数。这是不正确的,因为文档已经进行了大量的预处理,包括词袋和 IDF 过滤(我不会解释原因 - 太长了)。
示例性输入 CSV 文件:
Doc, Term, TFIDF score
1, apples, 0.3
1, bananas, 0.7
2, apples, 0.1
2, pears, 0.9
3, apples, 0.6
3, bananas, 0.2
3, pears, 0.2
我需要生成通常由 TFIDFVectorizer 生成的矩阵,例如:
| apples | bananas | pears
1 | 0.3 | 0.7 | 0
2 | 0.1 | 0 | 0.9
3 | 0.6 | 0.2 | 0.2
...这样我就可以计算文档之间的余弦相似度。
我使用的是 Python 2.7,但欢迎提供其他解决方案或工具的建议。我无法轻松切换到 Python 3。
编辑:
这并不是真正的转置 numpy 数组。它涉及将 TFIDF 分数映射到文档/术语矩阵,并使用标记化术语,并将缺失值填充为 0。
最佳答案
我建议使用scipy.sparse
中的稀疏矩阵
from scipy.sparse import csr_matrix, coo_matrix
from sklearn.metrics.pairwise import cosine_similarity
input="""Doc, Term, TFIDF score
1, apples, 0.3
1, bananas, 0.7
2, apples, 0.1
2, pears, 0.9
3, apples, 0.6
3, bananas, 0.2
3, pears, 0.2"""
voc = {}
# sparse matrix representation: the coefficient
# with coordinates (rows[i], cols[i]) contains value data[i]
rows, cols, data = [], [], []
for line in input.split("\n")[1:]: # dismiss header
doc, term, tfidf = line.replace(" ", "").split(",")
rows.append(int(doc))
# map each vocabulary item to an int
if term not in voc:
voc[term] = len(voc)
cols.append(voc[term])
data.append(float(tfidf))
doc_term_matrix = coo_matrix((data, (rows, cols)))
# compressed sparse row matrix (type of sparse matrix with fast row slicing)
sparse_row_matrix = doc_term_matrix.tocsr()
print("Sparse matrix")
print(sparse_row_matrix.toarray()) # convert to array
# compute similarity between each pair of documents
similarities = cosine_similarity(sparse_row_matrix)
print("Similarity matrix")
print(similarities)
输出:
Sparse matrix
[[0. 0. 0. ]
[0.3 0.7 0. ]
[0.1 0. 0.9]
[0.6 0.2 0.2]]
Similarity matrix
[[0. 0. 0. 0. ]
[0. 1. 0.04350111 0.63344607]
[0. 0.04350111 1. 0.39955629]
[0. 0.63344607 0.39955629 1. ]]
关于python - 如何使用已计算的 TFIDF 分数计算余弦相似度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50368930/
我有一个例程,它将接受 union 参数 d、theta、a 和 alpha 作为输入,并将生成相应的 4x4 齐次矩阵作为输出。我已经测试了我的矩阵乘法,它确实工作正常。我将从输入中得到 5 个矩阵
我有以下代码使用预先计算的内存表执行 Sin/Cos 函数。在下面的例子中,表格有 1024*128 个项目,涵盖了从 0 到 2pi 的所有 Sin/Cos 值。我知道我可以使用 Sin/Cos 对
问题是不言自明的。我看过几个 pi 的例子,但没有看到 trigo 函数。也许可以使用泰勒级数 as done here但我不完全确定如何在 python 中实现它。特别是如何存储这么多数字。我应该提
我开始学习 Python 中的数学模块,并试图围绕 Python 中的正弦、余弦和正切的三角函数。 我花了一些时间学习更多关于三角学的知识,并了解了直角三角形的基本公式是如何工作的: 正弦函数: si
我一个月前才开始学习这门 C++ 类(class)。现在我被分配去写一个程序来计算这个。我不知道我做错了什么。 #include #include float gatherl1(); float
这个问题在这里已经有了答案: Python cos(90) and cos(270) not 0 (3 个答案) 关闭 9 年前。 有没有办法获得角度(以弧度为单位)的精确正切/余弦/正弦? mat
这个问题在这里已经有了答案: Java Math.cos(Math.toRadians()) returns weird values (4 个答案) 关闭 10 年前。 我正在编写一个程序,我必须
我做了一个简单的计算器作为我的第一个android程序,现在我想给它添加trigonometry,log等函数。在 C 中,我们必须包含 math library 才能这样做,我似乎无法弄清楚你是如何
我正面临 objective-c 中 cos 函数的奇怪问题。我安装了带有 iOS 4.3 SDK 的 xcode 4.1.1。 我正在计算一个数的余弦值: y= cos(x*M_PI/180) 这将
尝试将以下 php 方法转换为在 .less 样式表中使用: 在 Less 中,如何在不使用特定语言的 cos()/sin() 函数的情况下实现正弦/余弦方法? .rotate(@deg) {
可以使用 iPhone 3GS 或 Pandora 的人请测试我刚刚编写的以下组装程序吗? 它应该在 NEON 矢量 FPU 上非常快速地计算正弦和余弦。我知道它编译得很好,但没有足够的硬件我无法测试
我为泰勒级数编写了以下函数来计算余弦。 double cosine(int x) { x %= 360; // make it less than 360 double rad = x
我是一名优秀的程序员,十分优秀!