numpy数组值更改而不会被弄糊涂? [英] Numpy array values changed without being aksed?

查看:71
本文介绍了numpy数组值更改而不会被弄糊涂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个向量, posVec dirVec ,它们的值我想保持在(0.0,0.0,0.0).

I have 2 vectors, posVec and dirVec who's value I woudl like to keep at (0.0,0.0,0.0).

所以我设置了临时向量 t_pos & t_dir 并根据 cylCut

So I set up temporary vectors t_pos & t_dir and change their values depending on the value of cylCut

然后在循环的下一次迭代中,我重置了临时向量.

Then on the next iteration of the loop I reset the temporary vectors.

代码:

import numpy as np

posVec = np.array([0.0,0.0,0.0]) # orginal vectors
dirVec = np.array([0.0,0.0,0.0])
print "................."
print 'Before'
print "................."
print posVec
print dirVec
print "................."

for cylCut in range(0,3):
    t_pos = posVec      # temporary vectors
    t_dir = dirVec
    if cylCut == 0:     # set temp vects accordingly
        print '0'
        t_pos[0] = 1.0
        t_dir[0] = 1.0
    if cylCut == 1:
        print '1'
        t_pos[1] = 1.0
        t_dir[1] = 1.0
    if cylCut == 2:
        print '2'
        t_pos[2] = 1.0
        t_dir[2] = 1.0
    print t_pos
    print t_dir

print "................."
print 'After'
print "................."
print posVec
print dirVec
print "................."

(至少对我而言)出于未知原因(至少对我而言) posVec & posDir 也正在更改.

For an unknown reason (to me at least) the contents of posVec & posDir are also being changed.

我得到的输出,

.................
Before
.................
[ 0.  0.  0.]
[ 0.  0.  0.]
.................
0
[ 1.  0.  0.]
[ 1.  0.  0.]
1
[ 1.  1.  0.]
[ 1.  1.  0.]
2
[ 1.  1.  1.]
[ 1.  1.  1.]
.................
After
.................
[ 1.  1.  1.]
[ 1.  1.  1.]
.................

为什么会这样?我没有明确地更改矢量. Python/Numpy是否自动链接变量?为什么要这么做?如何停止?

Why is this happening? At not point am I explicitly changing the vectors. Does Python/Numpy automatically link variable? Why would you do that? How do I stop it?

推荐答案

这是因为

t_pos = posVec
t_dir = dirVec

复制矢量的内容,但复制它们的引用.因此,基本上 t_pos posVec 指向完全相同的numpy数组对象.

You are not copying the contents of the vectors but you copy their references. So basically t_pos and posVec are pointing to the very same numpy array object.

您要执行的操作是:

t_pos = numpy.copy(posVec)
t_dir = numpy.copy(dirVec)

复制方法的差异:

  1 import timeit                                                                                                       
  2 import copy  
  3 import numpy as np
  4              
  5 someArr = np.arange(100)
  6              
  7 def copy1(): 
  8     tempArr = someArr.copy()
  9              
 10 def copy2(): 
 11     tempArr = np.copy(someArr)
 12              
 13 def copy3(): 
 14     tempArr = copy.copy(someArr)
 15              
 16 def copy4(): 
 17     tempArr = copy.deepcopy(someArr)
 18              
 19 print ("copy1: " + str(timeit.timeit(lambda: copy1(), number=1000000)))
 20 print ("copy2: " + str(timeit.timeit(lambda: copy2(), number=1000000)))
 21 print ("copy3: " + str(timeit.timeit(lambda: copy3(), number=1000000)))
 22 print ("copy4: " + str(timeit.timeit(lambda: copy4(), number=1000000)))

打印以下输出(以秒为单位):

Prints the following output (in seconds):

copy1: 0.5677032630010217
copy2: 1.473885050001627
copy3: 1.2420436849988619
copy4: 2.733253653999782

是的,有很大的区别,但仅在使用的计算资源方面.您的示例的结果保持不变.

So yes, there are huge differences but only in terms of used computation resources. The results for your example stay exact the same.

someArr.copy()指的是您已经保存在内存中的对象,因此访问此对象是最快的方法,每个numpy数组都具有这样的方法,因此,基本上,您正在访问已创建的numpy数组的方法.

someArr.copy() refers to the object you already have saved in memory, so accessing this is the fastest way and every numpy array holds such a method, so basically you are accessing the method of your already created numpy array.

np.copy(someArr)调用numpy库的复制方法,因此它比较慢,因为(据我所知)它在创建临时实例时会产生一些开销.

np.copy(someArr) calls the copy-method of the numpy library, therefore it is slower, because it (as far as I know) produced some overhead with creating a temporary instance.

copy.copy(someArr)是通常复制对象的python(非numpy)方式.我有点困惑,它的性能要比numpy的方法好一点……由于它的通用性(对于几乎所有对象而言)它的性能都应该更差……至少我以为如此.

copy.copy(someArr) is the python (non-numpy) way to copy objects generally. I am a bit confused that it performs slightly better than the numpy way...because of its generality (for nearly all objects) it should perform worse...at least I thought that.

copy.deepcopy(someArr)显然表现最差,但记住它很重要. Deepcopy不仅会创建对象的副本(someArr),而且还会在数组中创建每个元素的新副本.因此,如果您的对象(引用)存储在数组中,您将面临与您同样的问题.因此,使用了Deepcopy,当然,不仅要创建数组的副本,而且要创建数组中的每个元素都需要花费大量时间.

copy.deepcopy(someArr) clearly performs worst but it is important to remember it. Deepcopy not only creates a copy of your object (someArr) but it also creates new, fresh copies of every element in your array. So if you have objects (references) stored in your array, you will face the same problem you did. Therefore deepcopy is used and of course creating not only a copy of your array, but also of every element in it takes the most time.

这篇关于numpy数组值更改而不会被弄糊涂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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