索引numpy数组和使用整数和numpy标量之间有什么区别? [英] What's the difference when indexing a numpy array between using an integer and a numpy scalar?

查看:91
本文介绍了索引numpy数组和使用整数和numpy标量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没想到它们会有所不同,直到花了2个小时才找到一个bug.这是一个示例,显示了我注意到的差异,但我无法理解.

I didn't expect them to be different, until it just cost me 2 hours to find a bug. Here is an example showing the difference I noticed, but I couldn't make sense of it.

>>> a = np.array([[1, 2], [3, 4]])
>>> a[0][0]
1
>>> a[np.array(0)][np.array(0)]
1
>>> a[0][0] = 5
>>> a
array([[5, 2],
       [3, 4]])
>>> a[np.array(0)][np.array(0)] = 6
>>> a
array([[5, 2],
       [3, 4]])

看起来像使用numpy标量作为索引,元素无法更改.是返回原始数组元素的副本,而不是返回引用吗?

It looks like using numpy scalar as index the element can't be changed. Is a copy of the original array element instead of the reference being returned?

但是,使用元组索引,问题就消失了.

However, with tuple indexing, the problem is gone.

>>> a[np.array(0), np.array(0)] = 6
>>> a
array([[6, 2],
       [3, 4]])

这是怎么回事?我理解链括号索引和元组索引在本质上是不同的,但是原则上它们不应该两者都访问同一个元素吗?

What's happening here? I understand sementically chain bracket indexing and tuple indexing are different, but in principle shouldn't they both access the same element regardless?

出于好奇,我尝试了一维数组.结果是不同的.

Out of curiosity, I tried it with one dimensional array. The result is different.

>>> a = np.array([1, 2])
>>> a[np.array(0)] = 3
>>> a
array([3, 2])

这次元素已被修改.

我吸取的教训是,为了安全起见,我应该对numpy数组使用元组索引.但我真的想对这些不一致的影响做出解释.谢谢!

The lesson I learned is that I should use tuple index for numpy arrays as much as possible just to be safe. But I would really like an explanation for these inconsistent effects. Thanks!

推荐答案

查看数据缓冲区位置:

In [45]: a.__array_interface__['data']
Out[45]: (44666160, False)
In [46]: a[0].__array_interface__['data']
Out[46]: (44666160, False)

a[0]案例的相同位置.修改a[0]会修改a.

Same location for the a[0] case. Modifying a[0] will modify a.

但是对于数组索引,数据缓冲区是不同的-这是一个副本.修改此副本不会影响a.

But with the array index, the data buffer is different - this a copy. Modifying this copy will not affect a.

In [47]: a[np.array(0)].__array_interface__['data']
Out[47]: (43467872, False)

a[i,j]索引比a[i][j]更惯用.在某些情况下,它们是相同的.但是在很多情况下,它们会有所不同,除非您真的知道它的作用及其原因,否则最好避免使用后者.

a[i,j] indexing is more idiomatic than a[i][j]. In some cases they are the same. But there are enough cases where they differ that it is wise to avoid the later unless you really know what it does, and why.

In [49]: a[0]
Out[49]: array([1, 2])
In [50]: a[np.array(0)]   
Out[50]: array([1, 2])
In [51]: a[np.array([0])]
Out[51]: array([[1, 2]])

使用np.array(0)(一个0d数组)建立索引,就像使用np.array([0])(一个1d数组)建立索引.两者都产生一个副本,其第一维的大小类似于索引.

Indexing with np.array(0), a 0d array, is like indexing with np.array([0]), a 1d array. Both produce a copy, whose first dimension is sized like the index.

诚然,这很棘手,除非进行这种设置,否则可能不会出现.

Admittedly this is tricky, and probably doesn't show up except when doing this sort of set.

使用np.matrix时,选择[i][j][i,j]也会影响形状-

When using np.matrix the choice of [i][j] versus [i,j] affects shape as well - python difference between the two form of matrix x[i,j] and x[i][j]

这篇关于索引numpy数组和使用整数和numpy标量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆