Can't create COO object if shape parameter is ndarray
See original GitHub issueWhen creating a new sparse COO object there is an error if the shape is an ndarray instead of a list.
The error can be encountered as follows:
import numpy as np
import sparse
coords = [[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
data = [10, 20, 30, 40, 50]
s = sparse.COO(coords, data, shape=np.array((5, 5)))
Which results in the following propt
File "<stdin>", line 1, in <module>
File "path\to\directory\.venv\lib\site-packages\sparse\_coo\core.py", line 244, in __init__
if shape and not self.coords.size:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The error is produced when shape is casted into a bool, if it is a list the result will be False if it’s empty and True in any other situation. For an ndarray the casting is done element by element and thus cannot be evaluated in an if statement.
The solution is just to cast the value of shape into a list before operating with it
System
- Windows 10
- sparse version
0.13.0 - NumPy version
1.21.5 - Numba version
0.55.1
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
Why can we pass a list for the shape of a NumPy array ...
The page states that empty() takes a shape parameter, allowing the user to specify the shape of the array to be created. The...
Read more >2. Creating Numpy Arrays | Numerical Programming
NumPy tutorial: Creating basic array structures and manipulating arrays. Introducing shape, dimension and Slicing.
Read more >numpy.ndarray.shape — NumPy v1.24 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array...
Read more >1.4.1. The NumPy array object - Scipy Lecture Notes
Python objects: ... Create a memory-map to an array stored in a *binary* file on disk. ... Use the functions len() , numpy.shape()...
Read more >NDArray API — mxnet documentation
Creates an array from any object exposing the array interface. empty, Returns a new array of given shape and type, without initializing entries....
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

For what it’s worth, it looks like
scipy.sparsehandles shapes specified as arrays:Ah, I understand. You are looking to use a 1-D array of integers as a shape.
Yes, this is easily fixed. I’m willing to accept a PR, or I can try to do it over the weekend.
Please feel free to ping me if I don’t.