np.dot 在乘法矩阵中的结果问题

problems with the result of np.dot in multiplying matrices

我在使用 np.dot 进行矩阵乘法时遇到一些问题。

我将两个定义如下的矩阵相乘:

A = np.diagflat(diag)

其中 diag 是一个随机数数组,B 只是一个对称矩阵。 AB 都是 100 x 100.

当我尝试执行 A.dot(B) 时,我得到以下结果:

array([[<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>],
   [<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>],
   [<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>],
   ...,
   [<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>],
   [<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>],
   [<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    ...,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>,
    <100x100 sparse matrix of type '<class 'numpy.float64'>'
with 460 stored elements in Compressed Sparse Row format>]], dtype=object)

我不明白这个结果,它似乎是一个稀疏矩阵数组,但为什么会这样?我哪里错了?

谢谢!

你说过 A 是来自随机向量的对角矩阵:

A = np.diagflat(np.random.randint(10, size=100))

并且B是与图关联的矩阵的拉普拉斯算子:

R = nx.grid_graph(dim=[10,10])
B = nx.laplacian_matrix(R)

矩阵 B 是一个 sparse matrix,如果你想 看到 它需要转换成一个 numpy 数组,比如调试,但如果您的代码需要扩展,您应该保持稀疏。

那么np.dot产品是:

product = A.dot(B.toarray())

你也可以使用 B.A 表示法,我虽然在这种情况下很困惑:

product = A.dot(B.A)

product 为:

array([[16, -8,  0, ...,  0,  0,  0],
   [-3,  9, -3, ...,  0,  0,  0],
   [ 0, -1,  3, ...,  0,  0,  0],
   ...,
   [ 0,  0,  0, ..., 27, -9,  0],
   [ 0,  0,  0, ...,  0,  0,  0],
   [ 0,  0,  0, ...,  0, -4,  8]], dtype=int64)