New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: np.array fails on a list of arrays with partially matching dimensions #7453
Comments
Agreed, this sort of fallback logic is unfortunate. We've discussed not making |
Providing arr_of_arr = np.array([np.array([]), a, b])[1:] |
Still exists in 1.14.4 |
@ppwwyyxx - indeed, partially as it is not a trivial change, partially as it is not something one gets hit with all the time, so the urgency is relatively low (and there not that many people having time to contribute...). But what might help here is to make it clearer exactly what the desired behaviour would be. @shoyer mentioned the also long-standing request to explicitly requiring |
I'm willing to contribute if anyone can send me some pointers on what to do. I just glanced at the related code in ctors.c, the constructor logic seems to be quite complicated as it needs to deal with many different forms of input. |
Same issue: #8330 |
I think this might be fixed by #11601. Edit: It is not. |
So, what's happening here is roughly: a = np.array([1, 2])
b = np.array([[1, 0], [0, 1]])
out = np.asarray([a, b])
# translates to
out = np.empty((2, 2)) #shape is correctly inferred
out[0,:] = a
out[1,:] = b # error comes from here |
This is fixed in NumPy 1.19 (or 1.20). You have to provide In [9]: a = np.array([1, 2, 3])
...: b = np.array([[1, 0], [0, 1]])
...: print(np.asarray([a, b], dtype=object))
...: np.asarray([a, b])
...:
[array([1, 2, 3]) array([[1, 0],
[0, 1]])]
<ipython-input-9-f2d78f006499>:4: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.asarray([a, b])
Out[9]:
array([array([1, 2, 3]), array([[1, 0],
[0, 1]])], dtype=object) If that doesn't seem like the right fix, please open a new 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:
But the following doesn't:
Clearly, the problem is that, when numpy.asarray sees the first dimension has the same length in
a
andb
, it tries to go for "mode 1", which is impossible here.EDIT: I'm using numpy 1.10.4 and python 3.4.3.
The text was updated successfully, but these errors were encountered: