微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NumPy 插入函数:使用单个整数索引与索引列表有什么影响?形状如何受到影响?

如何解决NumPy 插入函数:使用单个整数索引与索引列表有什么影响?形状如何受到影响?

我研究 NumPy 有一段时间了,一个奇怪的行为阻止了我。

我希望以下代码片段能有所帮助:

这是我将使用的数组:
e = np.arange(1,10).reshape((3,3))

1- 将值插入到排名 1 的数组中:
np.insert(e,[1,2,3])np.insert(e,[0],3]) 是等价的。
输出 array([1,3,1,4,5,6,7,8,9]).

2- 将值插入 2 级数组(行):
np.insert(e,[10,11,12],axis=0)np.insert(e,axis=0) 是等价的。
请注意,我没有使用行的形状 -[[10,12]]- 并且它工作得很好。

尽管如此,我还是尝试了 np.insert(e,[[10,12]],axis=0) 并获得了相同的结果。

output:
array([[10,[ 1,3],[ 4,6],[ 7,9]]) 

正如预期的那样,使用 np.insert(e,[0,2],axis=0)np.insert(e,axis=0) 会将行插入到数组中的两个不同位置。

output:
array([[10,9]])

现在是奇怪的行为。

3- 将值插入 2 级数组(列):
一种。 (1)np.insert(e,axis=1) VS (2)np.insert(e,axis=1).
湾(3)np.insert(e,[[10],[11],[12]],axis=1) 与 (4)np.insert(e,axis=1)

a. outputs from (1) and (4):
array([[10,[11,[12,9]]) 
b. outputs from (2) and (3):
array([[10,12,9]])

c. (5)np.insert(e,axis=1) VS (6)np.insert(e,axis=1)

output from (5):
array([[10,10,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)中广播这个元素列表?

解决方法

文档说:

`values` should be shaped so that ``arr[...,obj,...] = values``
is legal.

这是一个例子:

将 2 行插入 (3,3) 数组:

In [19]: e=np.zeros((3,3),int)
In [32]: res=np.insert(e,[0,2],[1,2,3],axis=0)
In [33]: res
Out[33]: 
array([[1,0],0]])

结果是一个 (5,3) 数组。可以通过以下方式访问插入的值:

In [34]: res[[0,:]
Out[34]: 
array([[1,3]])

这是一个 (2,3) 数组。

(我不得不将 (0,2) 更改为 (0,3) 以考虑插入的行。insert 进行了这种调整。

我可以将 (3,) 分配给 (2,3) 槽。通过广播扩展到 (1,3) 和 (2,3):

In [35]: res[[0,:] = [1,3]

但是 a (3,1) 不能广播:

In [36]: res[[0,:] = [[1],[2],[3]]
Traceback (most recent call last):
  File "<ipython-input-36-de940597bcbf>",line 1,in <module>
    res[[0,[3]]
ValueError: shape mismatch: value array of shape (3,1)  could not be broadcast to indexing result of shape (2,3)

你的 (6) 有同样的问题,但有列:

In [38]: res=np.insert(e,3,axis=1)
In [39]: res
Out[39]: 
array([[3,[3,0]])
In [40]: res[:,3]]            # a (3,2) insert
Out[40]: 
array([[3,3]])
In [41]: res[:,3]] = np.ones((3,2))
In [42]: res[:,1))  # broadcasts fine

但是 (3,) 和 (1,3) 不能放在 (3,2) 空间中:

In [43]: res[:,))
Traceback (most recent call last):
  File "<ipython-input-43-3a546b2939fc>",in <module>
    res[:,))
ValueError: shape mismatch: value array of shape (3,)  could not be broadcast to indexing result of shape (2,3)

In [44]: res[:,3]] = np.ones((1,3))
Traceback (most recent call last):
  File "<ipython-input-44-6858764218b0>",3))
ValueError: shape mismatch: value array of shape (1,3)  could not be broadcast to indexing result of shape (2,3)

我不明白为什么错误消息显示 to indexing result of shape (2,3)。我希望阅读 (3,2)。

作为参考,这里是带有 insert 错误的完整回溯:

In [45]: np.insert(e,[10,11,12],axis=1)
Traceback (most recent call last):
  File "<ipython-input-45-a7ae562f20c6>",in <module>
    np.insert(e,axis=1)
  File "<__array_function__ internals>",line 5,in insert
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py",line 4678,in insert
    new[tuple(slobj)] = values
ValueError: shape mismatch: value array of shape (3,3)

new 必须是一个形状为 (3,5)、tuple(slobj) 之类的数组,类似于 (slice(None),np.array([0,3]))(与我的 [:,3]] 相当),而 valuesnp.array([10,12]) 数组。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。