2 个形状正确的 csr 矩阵的乘积

product of 2 csr matrices with correct shapes

我需要将 2 个形状为 A : (385019, 72) B : (72, 385019) 的 csr 矩阵相乘。我确实看到 A 的列数是 72,这正是 B 中的行数。但是当我执行 sparse.csr_matrix(A).multiply(sparse.csr_matrix(A)) 时,我得到 ValueError: inconsistent shapes 我浏览过其他帖子,但还没有任何帮助。 Very large matrices using Python and NumPy

您要查找的乘法称为 "dot product",在 python 中,您可以按如下方式进行

sparse.csr_matrix(A) * sparse.csr_matrix(B)

但是,您在所描述的问题中使用 sparse.csr_matrix(A).multiply(sparse.csr_matrix(A)) 的乘法称为 "Point-wise multiplication by another matrix, vector, or scalar"。这意味着如果 A 和 B 都是矩阵,则 A 的每个元素都将乘以 B 的每个元素;在这种情况下,A 和 B 的大小必须相同。如果 B 是标量,则 A 的每个元素都将乘以 B。