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

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

问题描述

我在使用 Numpy 的 Python 2.6.5 中有一个奇怪的问题.我分配了一个 numpy 数组,然后将一个新变量等同于它.当我对新数组执行任何操作时,原始数组的值也会发生变化.这是为什么?请看下面的例子.请赐教,因为我对 Python 和一般编程还很陌生.

-苏扬

<预><代码>>>>将 numpy 导入为 np>>>a = np.array([[1,2],[3,4]])>>>乙 = 一>>>乙数组([[1, 2],[3, 4]])>>>c = 一个>>>C数组([[1, 2],[3, 4]])>>>c[:,1] = c[:,1] + 5>>>C数组([[1, 7],[3, 9]])>>>乙数组([[1, 7],[3, 9]])>>>一种数组([[1, 7],[3, 9]])

解决方案

这其实根本不是问题;这是数组(和其他对象)在 Python 中的工作方式.

这样想:您在代码示例中创建的数组是一个位于内存中某个位置的对象.但是你不能通过告诉 Python 在内存中的哪个位置去寻找它来在你的程序中使用它;你必须给它一个名字.当你写

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

您既要创建数组,又要创建一个名称,a 来引用它.从那时起,Python 知道 a 指的是内存地址 0x123674283"(或其他).Python 运行时中有一个内部表(如果我没记错的话,称为符号表")包含所有这些信息,因此在上述 Python 代码行运行后,该表将包含

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

当你将一个变量的值赋给另一个变量时,比如

b = a

Python 不会复制整个数组,因为如果它是一个大数组,则需要很长时间.相反,它转到符号表并将 a 的内存地址复制到 b 表中的新行.所以你结束了

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

所以你看,ab 实际上指的是内存中的同一个位置,即同一个对象.您对其中一个所做的任何更改都会反映在另一个中,因为它们只是同一事物的两个名称.

如果您想真正制作数组的副本,则必须调用一个方法来显式执行此操作.Numpy 数组有一个 copy 方法,您可以将其用于此目的.所以如果你写

b = a.copy()

然后 Python 将首先实际制作数组的副本 - 也就是说,它留出一个新的内存区域,假设地址为 0x123904381​​,然后转到内存地址 0x123674283 并从后者复制数组的所有值前段记忆.因此,您在内存中的两个不同位置拥有相同的内容.

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

现在,当您更改 b 的元素之一时,该更改不会显示在 a 中,因为 ab 不再指计算机内存的同一部分.由于数组数据有两个独立的副本,您可以更改其中一个而不影响另一个.

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天全站免登陆