8000 Unexpected behavior with csr matrices · Issue #14487 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Unexpected behavior with csr matrices #14487

New issue

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

Closed
zjpoh opened this issue Sep 11, 2019 · 3 comments
Closed

Unexpected behavior with csr matrices #14487

zjpoh opened this issue Sep 11, 2019 · 3 comments
Labels
57 - Close? Issues which may be closable unless discussion continued component: numpy._core

Comments

@zjpoh
Copy link
Member
zjpoh commented Sep 11, 2019

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)

@rgommers
Copy link
Member

So what's happening here is that np.asarray(sparse_matrix) returns an object array:

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:

  • NumPy may change its behavior regarding creating object arrays at some point, or at least start giving warnings for such cases.
  • SciPy could decide to raise a TypeError here via an added __array__ method on all sparse matrices.

@seberg
Copy link
Member
seberg commented Sep 16, 2019

I actually think the second option in scipy is probably the best thing. Raise an error and ask to use todense specifically.

@seberg seberg added the 57 - Close? Issues which may be closable unless discussion continued label Sep 16, 2019
@mattip
Copy link
Member
mattip commented Dec 3, 2019

Closing. Please reopen with new information if needed.

@mattip mattip closed this as completed Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
57 - Close? Issues which may be closable unless discussion continued component: numpy._core
Projects
None yet
Development

No branches or pull requests

4 participants
0