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.

Documentation issue for sum on sparse matrices

See original GitHub issue

EDIT: 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kno10commented, May 5, 2021

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?

0reactions
stefanvcommented, Oct 9, 2021

Desired behavior implemented in #14822. Docstring for sum can still be improved.

Read more comments on GitHub >

github_iconTop 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 >

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