【问题标题】:problems with the result of np.dot in multiplying matricesnp.dot 在乘法矩阵中的结果问题
【发布时间】:2018-07-13 14:17:30
【问题描述】:

我对@9​​87654321@ 的矩阵乘法有些问题。

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

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)

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

谢谢!

【问题讨论】:

  • 正确的用法是np.dot(A, B)
  • 这无关紧要,A.dot(B) = np.dot(A, B)
  • 我使用 dot 得到相同的结果 :(
  • 您应该考虑重构问题以包含B 的性质,包括考虑编辑标题,以便将来人们可能会发现这个问题。
  • 问题是np.dot 使用错误的方式将B 转换为密集数组。 np.array(B) 产生 about 结果; B,toarray() 是正确的。

标签: python numpy matrix sparse-matrix algebra


【解决方案1】:

你说过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)

【讨论】:

  • 很抱歉我没有具体说明我是如何创建 B 的:我正在研究一个图形,所以我使用 networkx 库作为 R = nx.grid_graph(dim=[10,10] ) 然后我得到矩阵 B,因为它是拉普拉斯矩阵: B = nx.laplacian_matrix(R) 其中拉普拉斯矩阵是半定正对称矩阵
  • 哦哦!谢谢!完美,现在我明白了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多