为什么Python复制尺寸长度相同的NumPy数组? [英] Why does Python copy NumPy arrays where the length of the dimensions are the same?

查看:226
本文介绍了为什么Python复制尺寸长度相同的NumPy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在引用NumPy数组时遇到问题. 我有一个表格形式的数组

I have a problem with referencing to a NumPy array. I have an array of the form

import numpy as np
a = [np.array([0.0, 0.2, 0.4, 0.6, 0.8]),
     np.array([0.0, 0.2, 0.4, 0.6, 0.8]),
     np.array([0.0, 0.2, 0.4, 0.6, 0.8])]

如果我现在创建一个新变量,

If I now create a new variable,

b = np.array(a)

然后做

b[0] += 1
print(a)

然后a不变.

a = [array([0. , 0.2, 0.4, 0.6, 0.8]),
     array([0. , 0.2, 0.4, 0.6, 0.8]),
     array([0. , 0.2, 0.4, 0.6, 0.8])]

但是,如果我用以下方法做同样的事情:

But if I do the same thing with:

a = [np.array([0.0, 0.2, 0.4, 0.6, 0.8]),
     np.array([0.0, 0.2, 0.4, 0.6, 0.8]),
     np.array([0.0, 0.2, 0.4, 0.6])]

所以我在最后一个维度的末尾删除了一个数字.然后我再做一次:

so I removed one number in the end of the last dimension. Then I do this again:

b = np.array(a)
b[0] += 1
print(a)

现在a正在改变,我认为这是Python中的正常行为.

Now a is changing, what I thought is the normal behavior in Python.

a = [array([1. , 1.2, 1.4, 1.6, 1.8]),
     array([0. , 0.2, 0.4, 0.6, 0.8]),
     array([0. , 0.2, 0.4, 0.6])]

有人可以向我解释吗?

推荐答案

In [1]: a = [np.array([0.0, 0.2, 0.4, 0.6, 0.8]), 
   ...:      np.array([0.0, 0.2, 0.4, 0.6, 0.8]), 
   ...:      np.array([0.0, 0.2, 0.4, 0.6, 0.8])]                               
In [2]:                                                                         
In [2]: a                                                                       
Out[2]: 
[array([0. , 0.2, 0.4, 0.6, 0.8]),
 array([0. , 0.2, 0.4, 0.6, 0.8]),
 array([0. , 0.2, 0.4, 0.6, 0.8])]

a是数组的列表. b是2d数组.

a is a list of arrays. b is a 2d array.

In [3]: b = np.array(a)                                                         
In [4]: b                                                                       
Out[4]: 
array([[0. , 0.2, 0.4, 0.6, 0.8],
       [0. , 0.2, 0.4, 0.6, 0.8],
       [0. , 0.2, 0.4, 0.6, 0.8]])
In [5]: b[0] += 1                                                               
In [6]: b                                                                       
Out[6]: 
array([[1. , 1.2, 1.4, 1.6, 1.8],
       [0. , 0.2, 0.4, 0.6, 0.8],
       [0. , 0.2, 0.4, 0.6, 0.8]])

ba获取值,但不包含任何a对象.此b的基础数据结构与a列表非常不同.如果不清楚,您可能需要复习numpy基础知识(有关形状,步幅和数据缓冲区).

b gets values from a but does not contain any of the a objects. The underlying data structure of this b is very different from a, the list. If that isn't clear, you may want to review the numpy basics (which talk about shape, strides, and data buffers).

在第二种情况下,b是一个对象数组,包含与a相同的对象:

In the second case, b is an object array, containing the same objects as a:

In [8]: b = np.array(a)                                                         
In [9]: b                                                                       
Out[9]: 
array([array([0. , 0.2, 0.4, 0.6, 0.8]), array([0. , 0.2, 0.4, 0.6, 0.8]),
       array([0. , 0.2, 0.4, 0.6])], dtype=object)

b的行为与a非常相似-都包含数组.

This b behaves a lot like the a - both contain arrays.

此对象数组的构造与2d数值数组完全不同.我认为数值数组是默认的或正常的numpy行为,而对象数组是一种让步",它为我们提供了一种有用的工具,但它没有多维数组的计算能力.

The construction of this object array is quite different from the 2d numeric array. I think of the numeric array as the default, or normal, numpy behavior, while the object array is a 'concession', giving us a useful tool, but one which does not have the calculation power of the multidimensional array.

很容易错误地创建对象数组-有人说太容易了.通过设计可靠地制造一个可能会更加困难.对于带有原始a的示例,我们必须这样做:

It is easy to make an object array by mistake - some say too easy. It can be harder to make one reliably by design. FOr example with the original a, we have to do:

In [17]: b = np.empty(3, object)                                                
In [18]: b[:] = a[:]                                                            
In [19]: b                                                                      
Out[19]: 
array([array([0. , 0.2, 0.4, 0.6, 0.8]), array([0. , 0.2, 0.4, 0.6, 0.8]),
       array([0. , 0.2, 0.4, 0.6, 0.8])], dtype=object)

甚至for i in range(3): b[i] = a[i]

这篇关于为什么Python复制尺寸长度相同的NumPy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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