Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将稀疏矩阵转换为密集矩阵_Python_Numpy_Sparse Matrix - Fatal编程技术网

Python 无法将稀疏矩阵转换为密集矩阵

Python 无法将稀疏矩阵转换为密集矩阵,python,numpy,sparse-matrix,Python,Numpy,Sparse Matrix,我的矩阵Gx和Gy都是coo类型的稀疏矩阵 我使用它们执行以下操作: A = np.hstack((Gx.transpose(),Gy.transpose())) B = np.vstack((Gx,Gy)) L = np.dot(A,B) 我想将解决方案可视化,所以我使用了C.toarray()和C.todense(),但答案如下: In [391]: C Out[391]: array([ <16x16 sparse matrix of type '<type 'num

我的矩阵
Gx
Gy
都是coo类型的稀疏矩阵

我使用它们执行以下操作:

A = np.hstack((Gx.transpose(),Gy.transpose()))
B = np.vstack((Gx,Gy))

L = np.dot(A,B)
我想将解决方案可视化,所以我使用了C.toarray()和C.todense(),但答案如下:

In [391]: C
Out[391]: 
  array([ <16x16 sparse matrix of type '<type 'numpy.float64'>'
with 64 stored elements in Compressed Sparse Row format>], dtype=object)


In [392]: C.toarray() 
Traceback (most recent call last):
   File "<ipython-input-390-86c90f8dce51>", line 1, in <module>
    C.toarray()
AttributeError: 'numpy.ndarray' object has no attribute 'toarray'
[391]:C中的

出[391]:
数组([],数据类型=对象)
在[392]中:C.toarray()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
C.toarray()
AttributeError:'numpy.ndarray'对象没有属性'toarray'
我如何才能看到密集形式的矩阵
C

array([ <16x16 sparse matrix of type '<type 'numpy.float64'>'
会有用的。正如错误所述,
numpy
数组没有
toarray
方法。但在这种情况下,它的元素会

由于
Gx
Gy
是稀疏的,因此您需要使用
hstack
vstack
的稀疏版本,而不是
numpy
版本。检查
A
B
的类型。我想这些是numpy数组,不是稀疏矩阵


看看当我使用
np.hstack
和几个稀疏矩阵时会发生什么:

In [70]: M=sparse.csr_matrix([[0,1,2],[2,3,4]])
In [71]: np.hstack([M,M])
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:298: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.
  "using <, >, or !=, instead.", SparseEfficiencyWarning)
Out[71]: 
array([ <2x3 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>,
       <2x3 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>], dtype=object)
[70]中的
M=sparse.csr_矩阵([0,1,2],[2,3,4])
在[71]中:np.hstack([M,M])
/usr/lib/python3/dist packages/scipy/sparse/compressed.py:298:SparseEfficiencyWarning:使用>=和比较稀疏矩阵,
],dtype=object)
结果不是稀疏的,而是由两个稀疏元素组成的密集元素。

来自:

array([ <16x16 sparse matrix of type '<type 'numpy.float64'>'
会有用的。正如错误所述,
numpy
数组没有
toarray
方法。但在这种情况下,它的元素会

由于
Gx
Gy
是稀疏的,因此您需要使用
hstack
vstack
的稀疏版本,而不是
numpy
版本。检查
A
B
的类型。我想这些是numpy数组,不是稀疏矩阵


看看当我使用
np.hstack
和几个稀疏矩阵时会发生什么:

In [70]: M=sparse.csr_matrix([[0,1,2],[2,3,4]])
In [71]: np.hstack([M,M])
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:298: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.
  "using <, >, or !=, instead.", SparseEfficiencyWarning)
Out[71]: 
array([ <2x3 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>,
       <2x3 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>], dtype=object)
[70]中的
M=sparse.csr_矩阵([0,1,2],[2,3,4])
在[71]中:np.hstack([M,M])
/usr/lib/python3/dist packages/scipy/sparse/compressed.py:298:SparseEfficiencyWarning:使用>=和比较稀疏矩阵,
],dtype=object)
结果不是稀疏的,而是密集的2个稀疏元素