BUG: np.array fails on a list of arrays with partially matching dimensions
See original GitHub issueThe 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:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top 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 >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
Providing
dtype=object
does not help - you still get the same error. However, adding an empty array lets you avoid it:Still exists in 1.14.4