question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deprecate sparse.spmatrix.todense

See original GitHub issue

Is your feature request related to a problem? Please describe.

np.matrix is deprecated, and is slowly wiped from the public code. New users, I hope, don’t learn what np.matrix is, and that’s to the better.

At the same time, sparse matrices have a public interface for creating numpy matrices, which is actually used quite a lot in scipy tests. A new user who is unaware about the existence of numpy matrix type does not have a clear way to anticipate that a matrix is returned, or that it’s deprecated. So a possible scenario (from my personal experience, even though I’m not a new user) is that:

  • The user calls spmatrix.todense()
  • The following code either emits a deprecation warning for doing matrix operations, or does the usual confusing stuff with matrix vector multiplication.
  • The user realizes they should have used spmatrix.toarray(), switches to it and carries on.

Describe the solution you’d like

I propose to move towards making .todense an alias for .toarray through a deprecation cycle. This could be done by:

  1. Eliminating .todense from scipy own codebase
  2. Adding a keyword-only argument return_array=False to .todense and emitting a deprecation warning if it isn’t set to True.
  3. Making return_array=True a default
  4. Eventually eliminating it and making .todense an alias to .toarray.

Describe alternatives you’ve considered

Other options are:

  • Keeping todense around until numpy removes it.
  • Making it an alias right away and hoping nobody will notice 🙃 idk

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stefanvcommented, Oct 9, 2021

Will be partially addressed by #14822, but not clear how to address the problem of sparse.eye returning a sparse matrix by default.

0reactions
akhmerovcommented, Jul 29, 2021

Perhaps, I’d start from pinging @perimosocordiae as the code owner of scipy.sparse: is now the time to start getting rid of np.matrix legacy?

#14131 and https://github.com/scipy/scipy/pull/14488#discussion_r678451919 indicate that there is ongoing cost both to users and developers from scipy.sparse preferring a np.matrix interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.sparse.spmatrix.todense — SciPy v1.2.1 Reference Guide
Return a dense matrix representation of this matrix. Parameters: order : {'C', 'F'}, optional.
Read more >
How to avoid sparse to dense matrix convertions
Here is how to do it using scipy.sparse.diags . diags is similar to spdiags but a bit more convenient. >>> import numpy as...
Read more >
META: deprecation tracker · Issue #15765 · scipy/scipy - GitHub
DEP: change default of atol in scipy.sparse.linalg.* #15738 ... Deprecate sparse.spmatrix.todense #14494
Read more >
Source code for sparse._coo.core
s.dot(s.T).sum(axis=0).todense() # doctest: +NORMALIZE_WHITESPACE array([ 1, 1, ... x : scipy.sparse.spmatrix The sparse matrix to construct the array from.
Read more >
Mailman 3 ANN: SciPy 1.8.0 - NumPy-Discussion - python.org
Please refer to the `scipy.sparse` docstring for more information. ... MAINT: switch to using spmatrix.toarray instead of .todense * `#14499 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found