[英]Append values to numpy array of empty numpy arrays
I have the following problem, I have an numpy array that has empty numpy arrays as elements and its shape is 2x2 but is reported as 2,2,0 actually which makes sense. 我有以下问题,我有一个numpy数组,该数组具有空的numpy数组作为元素,其形状为2x2,但实际上报告为2,2,0,这很有意义。 The problem is when you try to append values to any of the empty numpy arrays nothing happens.
问题是当您尝试将值附加到任何空的numpy数组时,什么也没有发生。
MWE: MWE:
import numpy as np
a = np.array([[],[],[],[]])
a = np.reshape(a, (2,2,0))
a[0][0] = np.append(a[0][0], 1)
a[0][1] = np.append(a[0][0], [1])
Output:
>>>a[0][0]
array([], dtype=float64)
>>>a[0][1]
array([], dtype=float64)
Which means that nothing happens. 这意味着什么也没发生。 How can I append values to my 2x2 numpy array one at a time?
如何将值一次附加到2x2 numpy数组中?
After tinkering for a while I realized that the answer is obvious. 修补了一段时间后,我意识到答案很明显。
When you are trying to add one element on any of the 4 empty numpy arrays you are essentially breaking the 2,2,0 array and somehow the numpy module prevents you from doing so. 当您尝试在4个空numpy数组中的任何一个上添加一个元素时,实际上是在破坏2,2,0数组,并且numpy模块以某种方式阻止您这样做。
If you would like to add the elements one at a time you would have to add them to a temporary 1D array in groups of 4 and then reshape the temporary array into a (2,2,1) shape and then append the full temporary 2x2 numpy array to the empty one, essentially going from a 2,2,0 shape to a 2,2,1 shape. 如果您想一次添加一个元素,则必须将它们添加到4个一组的临时1D数组中,然后将临时数组重塑为(2,2,1)形状,然后附加完整的临时2x2 numpy数组到空数组,本质上是从2,2,0形状变为2,2,1形状。
Repeat as many times as needed. 根据需要重复多次。 MWE:
MWE:
import numpy as np
a = np.array([[],[],[],[]])
a = np.reshape(a, (2,2,0))
temporary = np.array([])
for i in range(4):
temporary = np.append(temporary, i)
temporary = np.reshape(temporary, (2,2,1))
a = np.append(a, temporary)
a = np.reshape(a, (2,2,1))
a = np.append(a, temporary)
a = np.reshape(a, (2,2,2))
And then you can access the elements as a[0][0][0] a[0][0][1] 然后您可以将元素访问为a [0] [0] [0] a [0] [0] [1]
Sth weird is that when you are appending the temporary array to a it automatically reshapes it to shape (4,) 奇怪的是,当您将临时数组附加到时,它会自动将其形状调整为形状(4,)
a = np.array([[],[],[],[]])
makes a = np.array([[],[],[],[]])
使
array([], shape=(4, 0), dtype=float64)
This is an array of 0 elements, that contains floats, and has a shape (4,0)
. 这是一个0元素的数组,包含浮点数,形状为
(4,0)
。 Your reshape changes the shape, but not the number of elements - still 2*2*0=0. 重塑会更改形状,但不会更改元素数量-仍为2 * 2 * 0 = 0。
This is not an array that contains other arrays. 这不是包含其他数组的数组。
Appending to an element of a
produces a 1 element array with shape (1,)
追加到的元件
a
产生具有形状的1个元件阵列(1,)
In [164]: np.append(a[0,0],1)
Out[164]: array([ 1.])
Trying to assign it back to a[0,0]
does nothing. 尝试将其分配回
a[0,0]
不会执行任何操作。 Actually I would have expected an error. 实际上,我本来希望有一个错误。 But in any case, it shouldn't and can't add an value to the array, that by definition, has 0 elements.
但是无论如何,它不应该也不能向数组添加一个值,按照定义,该值具有0个元素。
You must be thinking that you have defined a 2x2 array where each element can be an object, such as another array. 您必须考虑已定义了2x2数组,其中每个元素可以是一个对象,例如另一个数组。 To do that you need create the array differently.
为此,您需要以其他方式创建阵列。
For example: 例如:
In [176]: a=np.empty((2,2),dtype=object)
In [177]: a
Out[177]:
array([[None, None],
[None, None]], dtype=object)
In [178]: a.fill([]) # lazy way of replacing the None
In [179]: a
Out[179]:
array([[[], []],
[[], []]], dtype=object)
Now I have a (2,2) array, where each element can be any Python object, though at the moment they all are empty lists. As noted in the comment, by using `fill`, each element is the same empty list; change one (in a mutable way), and you change all).
I could use np.append
to create a new array (though I don't generally recommend using np.append
). 我可以使用
np.append
创建一个新数组(尽管我通常不建议使用np.append
)。 (but beware of a[0,0].append(1)
, a list operation). (但要注意列表操作
a[0,0].append(1)
)。
In [180]: a[0,0]=np.append(a[0,0],1)
In [181]: a
Out[181]:
array([[array([ 1.]), []],
[[], []]], dtype=object)
I could replace an element with a 2x2 array: 我可以将元素替换为2x2数组:
In [182]: a[0,1]=np.array([[1,2],[3,4]])
or a string 或一个字符串
In [183]: a[1,0]='astring'
or another list 或其他清单
In [184]: a[1,1]=[1,2,3]
In [185]: a
Out[185]:
array([[array([ 1.]), array([[1, 2],
[3, 4]])],
['astring', [1, 2, 3]]], dtype=object)
There's a real difference between this (2,2) array of objects and a 3 or 4d array of floats, (2,2,?). 这个(2,2)对象数组与一个3或4d浮点数组(2,2 ,?)之间存在真正的区别。
Here's how I'd perform the appends in your answer 这是我在您的答案中执行附加操作的方式
create the (2,2,0) array directly: 直接创建(2,2,0)数组:
In [207]: a=np.zeros((2,2,0))
and the (2,2,1) is simply range reshaped: 和(2,2,1)只是范围调整:
In [208]: temporary =np.arange(4).reshape(2,2,1)
In [209]: a
Out[209]: array([], shape=(2, 2, 0), dtype=float64)
In [210]: temporary
Out[210]:
array([[[0],
[1]],
[[2],
[3]]])
np.append
is just an alternate front end to concatenate
. np.append
只是一个备用前端concatenate
。 So I'll use that with explicit control over the axis. 因此,我将对轴进行显式控制。
append
is for Python users who persist in thinking in list terms. append
适用于坚持使用列表术语进行思考的Python用户。
In [211]: np.concatenate([a,temporary],axis=2)
Out[211]:
array([[[ 0.],
[ 1.]],
[[ 2.],
[ 3.]]])
In [212]: a1=np.concatenate([a,temporary],axis=2)
In [213]: a2=np.concatenate([a1,temporary],axis=2)
In [214]: a2
Out[214]:
array([[[ 0., 0.],
[ 1., 1.]],
[[ 2., 2.],
[ 3., 3.]]])
In [215]: a2.shape
Out[215]: (2, 2, 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.