numpy的数组赋值问题 [英] numpy array assignment problem

查看:198
本文介绍了numpy的数组赋值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 2.6.5与numpy的一个奇怪的问题。我分配一个numpy的数组,那么等同于一个新的变量给它。当我执行到新阵列的任何操作,原来的价值也随之变化。这是为什么?请看下面的例子。请赐教,因为我是相当新的Python和编程一般。

-Sujan

 >>>导入numpy的是NP
>>>一个= np.array([1,2],[3,4])
>>> B = A
>>> b
阵列([1,2],
       [3,4]])
>>> C = A
>>> C
阵列([1,2],
       [3,4]])
>>> C [:1] = C [:1] + 5
>>> C阵列([1,7],
       [3,9]])
>>> b
阵列([1,7],
       [3,9]])
>>>一个
阵列([1,7],
       [3,9]])


解决方案

这实际上不是在所有问题;它的阵列(以及其它对象)在Python的工作方式。

想想看这样的:你在code例如创建数组是坐落在内存中某个位置的对象。但是,你不能在内存中去寻找它告诉Python的在程序中使用它;你必须给它一个名字。当你写

  A = np.array([1,2],[3,4]])

您都创建阵列并创建一个名称, A ,引用它。从这一点上来说,巨蟒知道 A 指的是内存地址0x123674283(或其他)。有一个在Python运行时的内部表(称为符号表,如果我没记错的话),它包含所有这些信息,所以Python code运行上述行之后,此表将包含

  ...
A:0x123674283,
...

在一个变量的值赋给另外,像

  B = A

Python不复制整个阵列,因为如果它是一个大阵,也需要很长的时间。相反,这是不言而喻的符号表和副本的内存地址 A 在表中的新行 B 。所以,你风与

  ...
A:0x123674283,
...
B:0x123674283,
...

所以你看, A B 实际上指的是在内存中的位置相同,即同一个对象。你对其中一个的任何更改将反映在其他的,因为他们只是两个名称相同的事情。

如果你想真正使数组的一个副本,你必须调用做明确的方法。 numpy的阵列具有拷贝,你可以使用这个目的方法。所以,如果你写

  B = a.copy()

那么Python会先实际上使数组的副本 - 也就是说,它留出一个新的内存区域,让我们在地址0x123904381​​说,然后去内存地址0x123674283并复制数组的所有值从后者的存储器部分前者。所以,你有相同的内容坐在两个不同的地方在内存中。

  ...
A:0x123674283,
...
B:0x123904381​​,
...

现在,当您更改的元素之一,这种变化不会出现 A ,因为 A b 不再引用计算机内存的同一节。由于有数组数据的两个独立的副本,可以改变一个不影响其他。

I have a strange problem in Python 2.6.5 with Numpy. I assign a numpy array, then equate a new variable to it. When I perform any operation to the new array, the original's values also change. Why is that? Please see the example below. Kindly enlighten me, as I'm fairly new to Python, and programming in general.

-Sujan

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> b = a
>>> b
array([[1, 2],
       [3, 4]])
>>> c = a
>>> c
array([[1, 2],
       [3, 4]])
>>> c[:,1] = c[:,1] + 5
>>> c

array([[1, 7],
       [3, 9]])
>>> b
array([[1, 7],
       [3, 9]])
>>> a
array([[1, 7],
       [3, 9]])

解决方案

That's actually not a problem at all; it's the way arrays (and other objects) work in Python.

Think about it like this: the array you created in your code example is an object that sits at some location in memory. But you can't use it in your program by telling Python where in memory to go look for it; you have to give it a name. When you write

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

you are both creating the array and creating a name, a, that refers to it. From that point on, Python knows that a refers to "memory address 0x123674283" (or whatever). There's an internal table in the Python runtime (called the "symbol table" if I remember correctly) that contains all this information, so after the above line of Python code runs, this table would contain

...,
'a' : 0x123674283,
...

When you assign the value of one variable to another, like

b = a

Python doesn't copy the whole array, because if it were a big array, it would take a long time. Instead, it goes to the symbol table and copies the memory address for a to a new row in the table for b. So you wind up with

...,
'a' : 0x123674283,
...,
'b' : 0x123674283,
...

So you see, a and b are actually referring to the same location in memory, i.e. the same object. Any changes you make to one will be reflected in the other, since they're just two names for the same thing.

If you want to actually make a copy of the array, you have to call a method to do that explicitly. Numpy arrays have a copy method which you can use for just this purpose. So if you write

b = a.copy()

then Python will first actually make a copy of the array - that is, it sets aside a new region of memory, let's say at address 0x123904381, then goes to memory address 0x123674283 and copies all the values of the array from the latter section of memory to the former. So you have the same content sitting in two different places in memory.

...,
'a' : 0x123674283,
...,
'b' : 0x123904381,
...

Now, when you change one of the elements of b, that change won't show up in a, since a and b no longer refer to the same section of the computer's memory. Since there are two independent copies of the array data, you can change one without affecting the other.

这篇关于numpy的数组赋值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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