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.

numpy.maximum / numpy.minimum fail quietly on sparse matrices

See original GitHub issue

As I understand, the numpy.maximum and numpy.minimum functions do not support sparse arrays. However, they fail quietly, producing erroneous output when given a scipy.sparse.dok_matrix:

>>> import numpy
>>> from scipy.sparse import dok_matrix
>>> numpy.maximum(dok_matrix([[1,0]]), dok_matrix([[0,1]])).toarray()
75: array([[0, 1]])

When given a scipy.sparse.csr_matrix, these functions fail with an exception, but the error is not clear:

>>> import numpy
>>> from scipy.sparse import csr_matrix
>>> numpy.maximum(csr_matrix([[1,0]]), csr_matrix([[0,1]])).toarray()
Traceback (most recent call last):
  File "<pyshell#169>", line 1, in <module>
    numpy.maximum(csr_matrix([[1,0]]), csr_matrix([[0,1]])).toarray()
  File "/home/amitmo/virtualenv/local/lib/python2.7/site-packages/scipy/sparse/base.py", line 183, in __bool__
    raise ValueError("The truth value of an array with more than one "
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
>>> 

I think an exception should be raised if the data type is not supported.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
SimZhoucommented, Mar 5, 2020

It is 2020 now and I still got this issue.

scipy.version: 1.4.1 numpy.version: 1.18.1

0reactions
thirschbuechlercommented, Apr 11, 2022

the outputs seem to be the same now, still non-working however python 3.8.10 scipy 1.8.0 numpy 1.22.3

/home/user/.local/lib/python3.8/site-packages/scipy/sparse/_compressed.py:311: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.
  warn("Comparing sparse matrices using >= and <= is inefficient, "
Traceback (most recent call last):
  File "/home/user/test2.py", line 3, in <module>
    numpy.maximum(dok_matrix([[1,0]]), dok_matrix([[0,1]])).toarray()
  File "/home/user/.local/lib/python3.8/site-packages/scipy/sparse/_base.py", line 337, in __bool__
    raise ValueError("The truth value of an array with more than one "
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
/home/user/.local/lib/python3.8/site-packages/scipy/sparse/_compressed.py:311: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.
  warn("Comparing sparse matrices using >= and <= is inefficient, "
Traceback (most recent call last):
  File "/home/user/test.py", line 3, in <module>
    numpy.maximum(csr_matrix([[1,0]]), csr_matrix([[0,1]])).toarray()
  File "/home/user/.local/lib/python3.8/site-packages/scipy/sparse/_base.py", line 337, in __bool__
    raise ValueError("The truth value of an array with more than one "
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().



Read more comments on GitHub >

github_iconTop Results From Across the Web

Elementwise maximum of sparse Scipy matrix & vector with ...
This works perfectly in Numpy using np.maximum(mat, vec) via Numpy's broadcasting. However, Scipy's .maximum() does not have broadcasting. My ...
Read more >
NumPy User Guide
It is a Python library that provides a multidi- mensional array object, various derived objects (such as masked arrays and matrices), and an ......
Read more >
6.3. Preprocessing data — scikit-learn 1.2.0 documentation
from sklearn import preprocessing >>> import numpy as np >>> X_train ... to lie between a given minimum and maximum value, often between...
Read more >
Sparse matrices (scipy.sparse) — SciPy v1.9.3 Manual
This package is switching to an array interface, compatible with NumPy arrays, from the older matrix interface. We recommend that you use the...
Read more >
CuPy Documentation - Read the Docs
CuPy provides a ndarray, sparse matrices, and the associated routines for GPU devices, all having the same API as. NumPy and SciPy:.
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