Documentation issue for sum on sparse matrices
See original GitHub issueEDIT: The original issue posted below is in fact working as intended. See first comment for remaining documentation issue.
There is a strange behavior when summing sparse arrays along a specific axis. The matrix dimensions and slicing of the result do not work as expected. The behavior contradicts the documentation (which mentions the axis will be dropped) and also does not correspond to behavior as seen in numpy.
Reproducing code example:
from scipy.sparse import csr_matrix
csr_matrix((2,3)).sum(axis=0)
# matrix([[0., 0., 0.]])
csr_matrix((2,3)).sum(axis=0)[0]
# matrix([[0., 0., 0.]])
csr_matrix((2,3)).sum(axis=0)[0][0][0][0][0][0]
# matrix([[0., 0., 0.]])
csr_matrix((2,3)).sum(axis=1)
# matrix([[0.],[0.]])
csr_matrix((2,3)).sum(axis=1)[0]
# matrix([[0.]])
csr_matrix((2,3)).sum(axis=1)[0][0][0][0]
# matrix([[0.]])
import numpy as np
np.zeros((2,3)).sum(axis=0)
# array([0., 0., 0.])
np.zeros((2,3)).sum(axis=1)
# array([0., 0.])
Scipy/Numpy/Python version information:
import sys, scipy, numpy
print(scipy.__version__, numpy.__version__, sys.version_info)
1.5.2 1.19.1 sys.version_info(major=3, minor=7, micro=8, releaselevel='final', serial=0)
Also tested on scipy 1.5.0 and 1.4.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to sum a list of sparse matrices? - python - Stack Overflow
I'm trying to figure out how to efficiently sum .csr_matrix sparse matrices stored in a list. Here's the issue:
Read more >scipy.sparse.csr_matrix.sum — SciPy v0.14.0 Reference Guide
This is documentation for an old release of SciPy (version 0.14.0). Read this page Search for ... Sum the matrix over the given...
Read more >sparse matrix: drop dimension after sum · Issue #13901 - GitHub
It seems, summing a sparse matrix over a selected axis leaves a dimension of size 1 that cannot be dropped. Reproducing code example:...
Read more >Sum over cell array of sparse matrices error - MathWorks
This line works for non-sparse matrices, but not for sparse matrices. Manually extract cell elements and sum them is not acceptable. Any idea...
Read more >Slow arithmetic on views of sparse matrices - Julia Discourse
I would think this is expected: The sum of a sparse CSC matrix is trivial: just sum the internal vector storing the values...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I find it irritating to get a matrix back, and usually want an array. Apparently numpy also by now suggests to prefer using ndarrays instead of matrix: https://numpy.org/doc/stable/reference/generated/numpy.matrix.html any plans on eventually migrating the sparse matrix classes to produce ndarrays instead of matrixes?
Desired behavior implemented in #14822. Docstring for
sumcan still be improved.