I have the matrices Gx
and Gy
both sparse of type coo.
I perform the following operations with them:
A = np.hstack((Gx.transpose(),Gy.transpose()))
B = np.vstack((Gx,Gy))
L = np.dot(A,B)
I want to visualize the solution, C so I have used C.toarray() and C.todense(), but the answer is the following:
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'
How could I do to see the matrix C
in a dense form?
From:
array([ <16x16 sparse matrix of type '<type 'numpy.float64'>'
with 64 stored elements in Compressed Sparse Row format>], dtype=object)
I deduce that C
is a 1 element dense array with dtype=object
. That one element is a sparse matrix.
So I expect that
C[0].toarray()
will work. As the error says, a numpy
array does not have a toarray
method. But in this case its element does.
Since Gx
and Gy
are sparse, then you need to use the sparse versions of hstack
and vstack
, not the numpy
versions. Check the type of A
and B
. I be those are numpy arrays, not sparse matrices.
Look what happens when I use np.hstack
with a couple of sparse matrices:
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)
The result is not sparse, rather it dense with 2 sparse elements.