8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I also have unexpected behavior with csr matrices:
import numpy as np from scipy.sparse import csr_matrix a = np.array([[1, 0, 2, 2], [1, 4, 2, 4]]) m = csr_matrix(a) m.shape Out: (2, 4) m.todense() Out: matrix([[1, 0, 2, 2], [1, 4, 2, 4]]) m_2 = np.asarray(m) # weird shape m_2.shape Out: () # whereas it seems to be conserved somehow m_2 Out: array(<2x4 sparse matrix of type '<type 'numpy.int64'>' with 7 stored elements in Compressed Sparse Row format>, dtype=object) m_2.todense() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-12-3ca00cd97e3f> in <module>() ----> 1 m_2.todense() AttributeError: 'numpy.ndarray' object has no attribute 'todense'
versions: 1.17.1 3.7.2 (default, Feb 12 2019, 08:16:38) [Clang 10.0.0 (clang-1000.11.45.5)]
python: 3.7.2 scipy: 1.3.1
Originally posted by @leonardbinet in #14221 (comment)
The text was updated successfully, but these errors were encountered:
So what's happening here is that np.asarray(sparse_matrix) returns an object array:
np.asarray(sparse_matrix)
In [28]: m_2.shape Out[28]: () In [29]: type(m_2) Out[29]: numpy.ndarray In [30]: m_2.dtype Out[30]: dtype('O')
With the sparse array being contained inside:
In [31]: m_2[()] Out[31]: <2x4 sparse matrix of type '<class 'numpy.int64'>' with 7 stored elements in Compressed Sparse Row format>
So m_2.todense() giving an AttributeError is expected, since m_2 is a numpy.ndarray. tl;dr not a bug but highly annoying. Two separate ways forward:
m_2.todense()
AttributeError
m_2
numpy.ndarray
TypeError
__array__
Sorry, something went wrong.
I actually think the second option in scipy is probably the best thing. Raise an error and ask to use todense specifically.
todense
Closing. Please reopen with new information if needed.
No branches or pull requests
I also have unexpected behavior with csr matrices:
versions:
1.17.1 3.7.2 (default, Feb 12 2019, 08:16:38)
[Clang 10.0.0 (clang-1000.11.45.5)]
Originally posted by @leonardbinet in #14221 (comment)
The text was updated successfully, but these errors were encountered: