【发布时间】:2021-02-22 14:07:43
【问题描述】:
我研究 NumPy 已经有一段时间了,一个奇怪的行为阻止了我。
我希望以下代码 sn-ps 可以帮助:
这是我将使用的数组:e = np.arange(1, 10).reshape((3,3))。
1- 将值插入排名 1 的数组:np.insert(e, 0, [1,2,3]) 和 np.insert(e, [0], [1,2,3]) 是等价的。
输出: array([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9])。
2- 将值插入 2 级数组(行):np.insert(e, 0, [10,11,12], axis=0) 和 np.insert(e, [0], [10,11,12], axis=0) 是等效的。
请注意,我没有使用行的形状 -[[10,11,12]]- 并且效果很好。
尽管如此,我尝试了np.insert(e, 0, [[10,11,12]], axis=0) 和np.insert(e, [0], [[10,11,12]], axis=0) 并获得了相同的结果。
output:
array([[10, 11, 12],
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]])
正如预期的那样,使用np.insert(e, [0,2], [10,11,12], axis=0) 或np.insert(e, [0,2], [[10,11,12]], axis=0) 会将行插入到数组中的两个不同位置。
output:
array([[10, 11, 12],
[ 1, 2, 3],
[ 4, 5, 6],
[10, 11, 12],
[ 7, 8, 9]])
现在是奇怪的行为。
3- 将值插入 2 级数组(列):
一个。 (1)np.insert(e, 0, [10,11,12], axis=1) VS (2)np.insert(e, [0], [10,11,12], axis=1).
湾。 (3)np.insert(e, 0, [[10],[11],[12]], axis=1) 与 (4)np.insert(e, [0], [[10],[11],[12]], axis=1).
a. outputs from (1) and (4):
array([[10, 1, 2, 3],
[11, 4, 5, 6],
[12, 7, 8, 9]])
b. outputs from (2) and (3):
array([[10, 11, 12, 1, 2, 3],
[10, 11, 12, 4, 5, 6],
[10, 11, 12, 7, 8, 9]])
c。 (5)np.insert(e, [0,2], [[10],[11],[12]], axis=1) VS (6)np.insert(e, [0,2], [10,11,12], axis=1).
output from (5):
array([[10, 1, 2, 10, 3],
[11, 4, 5, 11, 6],
[12, 7, 8, 12, 9]])
The output from (6):
ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (2,3)
1- 如果形状是可选的,如行示例,为什么代码 (1) 和 (4) 中的列不是这种情况?
2- 如果形状很重要,为什么它适用于 (4) 而不是 (3)?
3- 如果形状是强制性的,那么(5)和(6)有什么区别?为什么 NumPy 会在 (6) 中广播这个元素列表?
【问题讨论】:
-
np.insert是一个复杂的 python 函数,根据输入采取不同的方法。稍后 ut 会连接一些数组,或者创建一个接收数组并将数组复制到它。你也可以这样做,而且可能更快。 -
我依稀记得回答过一个类似的问题(在过去的一年或 2 年)。
object(尤其是标量还是列表)和values形状之间的相互作用很难理清。文档试图解释这一点。我不确定我现在想花时间重新审视它。 -
我认为在所有这些情况下
insert都会根据object和values的大小(和类型)计算返回结果。它将原件复制到适当的空间,然后复制值。使用 2 个插入“列”和 3 个(或 (3,1))值,它可以将输出形状扩展 2 或 6 (2 x 3)。
标签: python numpy numpy-ndarray array-broadcasting