numpy.maximum / numpy.minimum fail quietly on sparse matrices
See original GitHub issueAs 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:
- Created 8 years ago
- Comments:11 (7 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
It is 2020 now and I still got this issue.
scipy.version: 1.4.1 numpy.version: 1.18.1
the outputs seem to be the same now, still non-working however python 3.8.10 scipy 1.8.0 numpy 1.22.3