使用高级索引复制数组数据分配吗? [英] Does assignment with advanced indexing copy array data?

查看:113
本文介绍了使用高级索引复制数组数据分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我慢慢想了解查看 s和 copy在numpy中的区别,以及可变



如果我使用高级索引,它应该返回一个副本。这似乎是真的:

 在[1]:import numpy as np 
In [2]:a = np.zeros((3,3))
In [3]:b = np.array(np.identity(3),dtype = bool)

In [4]:c = a [b]

在[5]:c [:] = 9

在[6]:a
Out [6]:
array([[0.,0,0,0],
[0.,0.,0],
[0.,0.,0 。]])

由于 c 一个副本,它不共享数据,更改不会改变 a 。但是,这让我困惑:

 在[7]:a [b] = 1 

In [8]:a
Out [8]:
array([[1,0,0,0],
[ 1.,0.],
[0.,0.,1.]])

所以,看来,即使我使用高级索引,分配仍然把左边的东西视为一个视图。显然,第2行中的 a 与第6行中的 a 是相同的对象/数据,因为突变 c 对此没有影响。



所以我的问题是: a 在第8行与以前相同的对象/数据(不计算对角线当然)还是它是一个副本?换句话说, a 的数据是否复制到新的 a ,或者是其数据是否在原位? / p>

例如,是否如下:

  x = 2,3] 
x + = [4]

或类似:

  y =(1,2,3)
y + =(4,)

我不知道如何检查这个,因为在这两种情况下, a.flags.owndata True

解决方案

如果你这么做的话,请随时解释或回答一个不同的问题。 c = a [b] a .__ get_item __ 使用 b 作为其唯一的参数,并且返回的任何值都分配给 c



使用 b a [b] = c a .__ setitem __



因此,尽管有相同的<$ c $> c> a [b] 语法,两个表达式都在做不同的事情。你可以子类化 ndarray ,重载这两个函数,并使它们的行为不同。默认情况下在numpy中,前者返回一个副本(如果 b 是数组),但后者修改 a


I am slowly trying to understand the difference between views and copys in numpy, as well as mutable vs. immutable types.

If I access part of an array with 'advanced indexing' it is supposed to return a copy. This seems to be true:

In [1]: import numpy as np
In [2]: a = np.zeros((3,3))
In [3]: b = np.array(np.identity(3), dtype=bool)

In [4]: c = a[b]

In [5]: c[:] = 9

In [6]: a
Out[6]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

Since c is just a copy, it does not share data and changing it does not mutate a. However, this is what confuses me:

In [7]: a[b] = 1

In [8]: a
Out[8]: 
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

So, it seems, even if I use advanced indexing, assignment still treats the thing on the left as a view. Clearly the a in line 2 is the same object/data as the a in line 6, since mutating c has no effect on it.

So my question: is the a in line 8 the same object/data as before (not counting the diagonal of course) or is it a copy? In other words, was a's data copied to the new a, or was its data mutated in place?

For example, is it like:

x = [1,2,3]
x += [4]

or like:

y = (1,2,3)
y += (4,)

I don't know how to check for this because in either case, a.flags.owndata is True. Please feel free to elaborate or answer a different question if I'm thinking about this in a confusing way.

解决方案

When you do c = a[b], a.__get_item__ is called with b as its only argument, and whatever gets returned is assigned to c.

When you doa[b] = c, a.__setitem__ is called with b and c as arguments and whatever gets returned is silently discarded.

So despite having the same a[b] syntax, both expressions are doing different things. You could subclass ndarray, overload this two functions, and have them behave differently. As is by default in numpy, the former returns a copy (if b is an array) but the latter modifies a in place.

这篇关于使用高级索引复制数组数据分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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