矩阵相乘时np.dot结果的问题

时间:2018-07-13 14:17:31

标签: python numpy matrix sparse-matrix algebra

我在使用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)

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

谢谢!

1 个答案:

答案 0 :(得分:0)

您曾经说过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)