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.

BUG: np.array fails on a list of arrays with partially matching dimensions

See original GitHub issue

The functions numpy.array and numpy.asarray have a well defined behaviour when applied to lists of arrays: if the listed arrays have the same dimensions and size, the list is turned in one of the dimensions of the resulting array (let’s call it “mode 1”). If not, an array of arrays is returned (“mode 2”).

However, the behaviour of numpy.array and numpy.asarray in “mode 2” seems to be dependent on the number of items in the arrays. The following code is not very elegant relative to numpy usefulness, but works:

>>> a = np.array([1, 2, 3])
>>> b = np.array([[1, 0], [0, 1]])
>>> np.asarray([a, b])
array([array([1, 2, 3]), array([[1, 0],
       [0, 1]])], dtype=object)

But the following doesn’t:

>>> a = np.array([1, 2])
>>> b = np.array([[1, 0], [0, 1]])
>>> np.asarray([a, b])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/numpy/core/numeric.py", line 474, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (2,2) into shape (2)

Clearly, the problem is that, when numpy.asarray sees the first dimension has the same length in a and b, it tries to go for “mode 1”, which is impossible here.

EDIT: I’m using numpy 1.10.4 and python 3.4.3.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
roman-khcommented, Apr 6, 2017

Providing dtype=object does not help - you still get the same error. However, adding an empty array lets you avoid it:

arr_of_arr = np.array([np.array([]), a, b])[1:]
3reactions
ppwwyyxxcommented, Jul 18, 2018

Still exists in 1.14.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

List to numpy array conversion possible bug - Stack Overflow
On version 1.9 I get different results (error). 3 same shape arrays produce the expected 3d array: In [726]: np.array([np.ones((3,2)) ...
Read more >
Indexing on ndarrays — NumPy v1.24 Manual
When the index consists of as many integer arrays as dimensions of the array being indexed, the indexing is straightforward, but different from...
Read more >
NumPy 1.20.0 Release Notes
In NumPy 1.20, a warning will be given when an array-like is not also a sequence (but behaviour remains identical, see deprecations).
Read more >
the absolute basics for beginners — NumPy v1.25.dev0 Manual
The shape of the array is a tuple of integers giving the size of the array along each dimension. One way we can...
Read more >
numpy.loadtxt — NumPy v1.24 Manual
Load data from a text file. Parameters: fnamefile, str, pathlib.Path, list of str, generator. File, filename ...
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