Python:稀疏矩阵乘法与numpy.dot()不一致

Python: Inconsistency between sparse matrix multiplication and numpy.dot()

Ubuntu16.04_64bit + Python3.5.2 + numpy1.13.3 + scipy1.0.0 我在处理 scipy.sparse.csc.csc_matrixnumpy.ndarray 之间的矩阵乘法时遇到了这个问题。我在这里举个例子:

import numpy as np
import scipy.sparse

a = np.random.random(1000,1000)
b = np.random.random(1000,2000)
da = scipy.sparse.csc.csc_matrix(a)
db = scipy.sparse.csc.csc_matrix(b)

ab = a.dot(b)
dadb = da.dot(db)
dab = da.dot(b)

那么区别如下:

In [31]: np.sum(dadb.toarray() != ab)
Out[31]: 1869078

In [33]: np.sum(dab != dadb.toarray())
Out[33]: 0

In [34]: np.sum(dab != ab)
Out[34]: 1869078

为什么?它们之间有什么区别?用它做什么?

您看到的是典型的浮点运算(有关详细解释,请参阅 What Every Computer Scientist Should Know About Floating-Point Arithmetic or the answers to Why Are Floating Point Numbers Inaccurate?)。与真正的算术不同,浮点算术中的运算顺序会(略微)改变结果,因为舍入误差以不同的方式累积。这意味着计算相同结果的不同方法不能完全一致,但它们会大致一致。

如果您使用 np.allclose 而不是使用完全相等,您可以看到这一点:

>>> np.allclose(dab, ab)
True

>>> np.allclose(dadb.toarray(), ab)
True

简而言之,这些操作的行为符合预期。