Python中a-= b和a = a-b之间的区别 [英] Difference between a -= b and a = a - b in Python

查看:198
本文介绍了Python中a-= b和a = a-b之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近应用了解决方案,用于平均每N行矩阵. 尽管该解决方案总体上可行,但在将其应用于7x1阵列时遇到了问题.我注意到问题出在使用-=运算符时. 举一个小例子:

I have recently applied this solution for averaging every N rows of matrix. Although the solution works in general I had problems when applied to a 7x1 array. I have noticed that the problem is when using the -= operator. To make a small example:

import numpy as np

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

a[1:] -= a[:-1]
b[1:] = b[1:] - b[:-1]

print a
print b

输出:

[1 1 2]
[1 1 1]

因此,对于数组,a -= b会产生与a = a - b不同的结果.我认为到目前为止,这两种方式是完全相同的.有什么区别?

So, in the case of an array a -= b produces a different result than a = a - b. I thought until now that these two ways are exactly the same. What is the difference?

我提到的将矩阵中每N行求和的方法是如何工作的,例如是7x4矩阵,而不是7x1阵列?

How come the method I am mentioning for summing every N rows in a matrix is working e.g. for a 7x4 matrix but not for a 7x1 array?

推荐答案

注意:从1.13.0版开始,在共享内存的NumPy数组上使用就地操作不再是问题(请参阅详细信息此处).这两个操作将产生相同的结果.此答案仅适用于NumPy的早期版本.

Note: using in-place operations on NumPy arrays that share memory in no longer a problem in version 1.13.0 onward (see details here). The two operation will produce the same result. This answer only applies to earlier versions of NumPy.

在计算中使用数组时对其进行突变会导致意外的结果!

Mutating arrays while they're being used in computations can lead to unexpected results!

在问题的示例中,用-=减法会修改a的第二个元素,然后立即在对该a的第三个元素的操作中使用该修改的第二个元素.

In the example in the question, subtraction with -= modifies the second element of a and then immediately uses that modified second element in the operation on the third element of a.

以下是a[1:] -= a[:-1]逐步发生的情况:

Here is what happens with a[1:] -= a[:-1] step by step:

  • a是具有数据[1, 2, 3]的数组.

我们对此数据有两个视图:a[1:][2, 3],而a[:-1][1, 2].

We have two views onto this data: a[1:] is [2, 3], and a[:-1] is [1, 2].

就地减法-=开始.从a[1:]的第一个元素中减去a[:-1]的第一个元素1.这已将a修改为[1, 1, 3].现在我们有了a[1:]是数据[1, 3]的视图,而a[:-1]是数据[1, 1]的视图(数组a的第二个元素已更改).

The in-place subtraction -= begins. The first element of a[:-1], 1, is subtracted from the first element of a[1:]. This has modified a to be [1, 1, 3]. Now we have that a[1:] is a view of the data [1, 3], and a[:-1] is a view of the data [1, 1] (the second element of array a has been changed).

a[:-1]现在是[1, 1],并且NumPy现在必须从a[1:]的第二个元素中减去其第二个元素 1 (现在不再是2!).这使a[1:]成为值[1, 2]的视图.

a[:-1] is now [1, 1] and NumPy must now subtract its second element which is 1 (not 2 anymore!) from the second element of a[1:]. This makes a[1:] a view of the values [1, 2].

a现在是一个值为[1, 1, 2]的数组.

a is now an array with the values [1, 1, 2].

b[1:] = b[1:] - b[:-1]不会出现此问题,因为b[1:] - b[:-1]首先创建一个 new 数组,然后将该数组中的值分配给b[1:].减法期间它不会修改b本身,因此视图b[1:]b[:-1]不会更改.

b[1:] = b[1:] - b[:-1] does not have this problem because b[1:] - b[:-1] creates a new array first and then assigns the values in this array to b[1:]. It does not modify b itself during the subtraction, so the views b[1:] and b[:-1] do not change.

一般建议是,如果一个视图重叠,则应避免在一个视图和另一个视图之间进行就地修改.这包括运算符-=*=等,并在通用函数(如np.subtractnp.multiply)中使用out参数来写回数组之一.

The general advice is to avoid modifying one view inplace with another if they overlap. This includes the operators -=, *=, etc. and using the out parameter in universal functions (like np.subtract and np.multiply) to write back to one of the arrays.

这篇关于Python中a-= b和a = a-b之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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