Python numpy doesn't store array ()
I am getting a weird error when trying to (binary) save some arrays in python 2 I have highlighted the error, in particular by assuming that
p1 = [1, 5, 10, 20]
p2 = [1, 5, 10, 20, 30]
p3 =np.zeros( (5,10), dtype=float)
then
np.save("foo1", (p1, p2))
np.save("foo2", (p1, p3))
works fine but
np.save("foo3", (p2, p3))
returns an error
Any ideas what's going on? The error says "setting array element with sequence" Tried looking around, converting arrays, etc. but to no avail. What's funny is that, as mentioned, the former are kept in order and p1 is very similar to p2 ...
source to share
The error is not related to np.save
, but with an attempt to create an array from nested sequences. I am getting a similar but different error, possibly because I am working on a development version using any of the options np.array
:
>>> np.array((p2, p3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (5,10) into shape (5)
Not sure that this corresponds to an error, but that disables numpy, lies in the fact that the first measurement p2
and p3
coincides with 5 in your case. Therefore numpy thinks it should create an array of the arr
shape (2, 5, ...)
. It then assigns values from p2
to arr[0, :]
without issue. But when it tries to assign values to p3
before arr[1, :]
, an error occurs: you are trying to get into one position, for example. arr[1, 0]
, 5 elements in p3[0, :]
.
Perhaps Numpy could be smarter about this, and not assume the appropriate size means that the depth of all sequences is the same as it seems. You may want to want to print out the numpy mailing list to see if one of the developers has a more informed opinion on whether this is an unwanted behavior or a design choice.
source to share