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.

Can't create COO object if shape parameter is ndarray

See original GitHub issue

When 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:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Mar 23, 2022

For what it’s worth, it looks like scipy.sparse handles shapes specified as arrays:

>>> sparse.coo_matrix(([0], ([0], [0])), shape=np.array([5, 5]))
<5x5 sparse matrix of type '<class 'numpy.int64'>'
	with 1 stored elements in COOrdinate format>
0reactions
hameerabbasicommented, Mar 23, 2022

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.

Read more comments on GitHub >

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

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