Python 基于numpy/scipy包的代码中的形状不一致

Python 基于numpy/scipy包的代码中的形状不一致,python,numpy,scipy,shape,operation,Python,Numpy,Scipy,Shape,Operation,我有两个矩阵和一个向量,我使用numpy的dot()函数将它们相乘 print D.shape, A.shape, y.shape, type(D), type(A), type(y) # (236, 236) (236, 236) (236,) # <class 'scipy.sparse.csr.csr_matrix'> # <class 'scipy.sparse.csr.csr_matrix'> # <type 'numpy.ndarray'> y_

我有两个矩阵和一个向量,我使用numpy的
dot()
函数将它们相乘

print D.shape, A.shape, y.shape, type(D), type(A), type(y)
# (236, 236) (236, 236) (236,)
# <class 'scipy.sparse.csr.csr_matrix'>
# <class 'scipy.sparse.csr.csr_matrix'>
# <type 'numpy.ndarray'>

y_next = np.dot(D, np.dot(A, y))

print y_next.shape
# (236,)

我不知道
scipy.sparse
的内部是如何工作的,但这是您的问题:

In [1]: from scipy.sparse import csr_matrix

In [2]: A = csr_matrix([[1,2],[3,4]])

In [3]: y = scipy.array([1, 2])

In [4]: print A
  (0, 0)    1
  (0, 1)    2
  (1, 0)    3
  (1, 1)    4

In [5]: print y
[1 2]

In [6]: print scipy.dot(A, y)
[  (0, 0)   1
  (0, 1)    2
  (1, 0)    3
  (1, 1)    4
   (0, 0)   2
  (0, 1)    4
  (1, 0)    6
  (1, 1)    8]
为什么会这样,我不知道

相反,首先确保两个操作数的类型相同:

In [7]: print scipy.dot(A, csr_matrix(y).T)
  (0, 0)    5
  (1, 0)    11

In [13]: scipy.dot(A.toarray(), y)
Out[13]: array([ 5, 11])

造成混淆的原因是对scipy稀疏矩阵使用numpy
dot
运算符。对于numpy和scipy矩阵(注意矩阵,而不是数组),
*
运算符计算点积,如下所示:

In [47]: import scipy.sparse as sp

In [48]: import numpy as np

In [49]: D=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))

In [50]: A=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))

In [51]: y=np.random.random(100)

In [52]: y_next = A*(D*y)

In [53]: print y_next.shape, type(y_next)
(100,) <type 'numpy.ndarray'>

In [54]: print y_next
[ 0.00478446  0.0234117   0.02234696  0.23123913  0.15545059  0.366065
  0.05674736  0.00238582  0.08701694  0.00099934  0.01687756  0.08190578
  0.17570485  0.08015175  0.00301985  0.00491663  0.09450794  0.1141585
  0.02753342  0.0462671   0.02075956  0.21261696  0.82611774  0.09058998
  0.33545702  0.31456356  0.00260624  0.0449429   0.2431993   0.06302444
  0.01901411  0.02553964  0.02442291  0.02169692  0.15085474  0.41331208
  0.09486585  0.01001604  0.48898697  0.03557272  0.22931588  0.0760863
  0.37686888  0.02801424  0.3280943   0.1695001   0.02890001  0.11712331
  0.02996858  0.43608624  0.00905409  0.00655408  0.01618681  0.1417559
  0.0057121   0.0010656   0.02067559  0.05223334  0.14035328  0.0457123
  0.1273495   0.17688214  0.39300249  0.00625762  0.05356745  0.26719959
  0.08349373  0.05969248  0.02332782  0.0218782   0.1716797   0.04823102
  0.03117486  0.00172426  0.08514879  0.09505655  0.17030885  0.00953221
  0.00134071  0.03951708  0.00243708  0.04247436  0.32152315  0.02039932
  0.00436897  0.00097858  0.08876351  0.00824626  0.12004067  0.01060241
  0.11929884  0.01207807  0.10467955  0.02536641  0.602902    0.04115373
  0.00472405  0.05108167  0.28946041  0.19071962]

使用
numpy.dot
似乎会产生一个内部为稀疏矩阵的
ndarray
类型。您看到的重复条目是单个产品,我认为这些产品被归纳为最终的dot产品。

谢谢您的回复。第二个代码块第一行中的
.T
是做什么的?我在房间里找不到它。您也可能会发现此页面很有帮助:
In [47]: import scipy.sparse as sp

In [48]: import numpy as np

In [49]: D=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))

In [50]: A=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))

In [51]: y=np.random.random(100)

In [52]: y_next = A*(D*y)

In [53]: print y_next.shape, type(y_next)
(100,) <type 'numpy.ndarray'>

In [54]: print y_next
[ 0.00478446  0.0234117   0.02234696  0.23123913  0.15545059  0.366065
  0.05674736  0.00238582  0.08701694  0.00099934  0.01687756  0.08190578
  0.17570485  0.08015175  0.00301985  0.00491663  0.09450794  0.1141585
  0.02753342  0.0462671   0.02075956  0.21261696  0.82611774  0.09058998
  0.33545702  0.31456356  0.00260624  0.0449429   0.2431993   0.06302444
  0.01901411  0.02553964  0.02442291  0.02169692  0.15085474  0.41331208
  0.09486585  0.01001604  0.48898697  0.03557272  0.22931588  0.0760863
  0.37686888  0.02801424  0.3280943   0.1695001   0.02890001  0.11712331
  0.02996858  0.43608624  0.00905409  0.00655408  0.01618681  0.1417559
  0.0057121   0.0010656   0.02067559  0.05223334  0.14035328  0.0457123
  0.1273495   0.17688214  0.39300249  0.00625762  0.05356745  0.26719959
  0.08349373  0.05969248  0.02332782  0.0218782   0.1716797   0.04823102
  0.03117486  0.00172426  0.08514879  0.09505655  0.17030885  0.00953221
  0.00134071  0.03951708  0.00243708  0.04247436  0.32152315  0.02039932
  0.00436897  0.00097858  0.08876351  0.00824626  0.12004067  0.01060241
  0.11929884  0.01207807  0.10467955  0.02536641  0.602902    0.04115373
  0.00472405  0.05108167  0.28946041  0.19071962]
In [55]: print D.dot(A.dot(y)) - (D*(A*y))

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]