[英]Append values to numpy array of empty numpy arrays
我有以下问题,我有一个numpy数组,该数组具有空的numpy数组作为元素,其形状为2x2,但实际上报告为2,2,0,这很有意义。 问题是当您尝试将值附加到任何空的numpy数组时,什么也没有发生。
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)
这意味着什么也没发生。 如何将值一次附加到2x2 numpy数组中?
修补了一段时间后,我意识到答案很明显。
当您尝试在4个空numpy数组中的任何一个上添加一个元素时,实际上是在破坏2,2,0数组,并且numpy模块以某种方式阻止您这样做。
如果您想一次添加一个元素,则必须将它们添加到4个一组的临时1D数组中,然后将临时数组重塑为(2,2,1)形状,然后附加完整的临时2x2 numpy数组到空数组,本质上是从2,2,0形状变为2,2,1形状。
根据需要重复多次。 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))
然后您可以将元素访问为a [0] [0] [0] a [0] [0] [1]
奇怪的是,当您将临时数组附加到时,它会自动将其形状调整为形状(4,)
a = np.array([[],[],[],[]])
使
array([], shape=(4, 0), dtype=float64)
这是一个0元素的数组,包含浮点数,形状为(4,0)
。 重塑会更改形状,但不会更改元素数量-仍为2 * 2 * 0 = 0。
这不是包含其他数组的数组。
追加到的元件a
产生具有形状的1个元件阵列(1,)
In [164]: np.append(a[0,0],1)
Out[164]: array([ 1.])
尝试将其分配回a[0,0]
不会执行任何操作。 实际上,我本来希望有一个错误。 但是无论如何,它不应该也不能向数组添加一个值,按照定义,该值具有0个元素。
您必须考虑已定义了2x2数组,其中每个元素可以是一个对象,例如另一个数组。 为此,您需要以其他方式创建阵列。
例如:
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).
我可以使用np.append
创建一个新数组(尽管我通常不建议使用np.append
)。 (但要注意列表操作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)
我可以将元素替换为2x2数组:
In [182]: a[0,1]=np.array([[1,2],[3,4]])
或一个字符串
In [183]: a[1,0]='astring'
或其他清单
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)
这个(2,2)对象数组与一个3或4d浮点数组(2,2 ,?)之间存在真正的区别。
这是我在您的答案中执行附加操作的方式
直接创建(2,2,0)数组:
In [207]: a=np.zeros((2,2,0))
和(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
只是一个备用前端concatenate
。 因此,我将对轴进行显式控制。 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.