【发布时间】:2018-07-13 14:17:30
【问题描述】:
我对@987654321@ 的矩阵乘法有些问题。
我将两个矩阵相乘,定义如下:
A = np.diagflat(diag)
其中 diag 是一个随机数数组,B 是一个简单的对称矩阵。 A 和 B 都是 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