The basic question is: What happens under the hood when doing: a[i] += b
?
Given the following:
import numpy as np
a = np.arange(4)
i = a > 0
i
= array([False, True, True, True], dtype=bool)
I understand that:
a[i] = x
is the same as a.__setitem__(i, x)
, which assigns directly to the items indicated by i
a += x
is the same as a.__iadd__(x)
, which does the addition in place
But what happens when I do:
a[i] += x
Specifically:
- Is this the same as
a[i] = a[i] + x
? (which is not an in-place operation)
- Does it make a difference in this case if
i
is:
- an
int
index, or
- an
ndarray
, or
- a
slice
object
Background
The reason I started delving into this is that I encountered a non-intuitive behavior when working with duplicate indices:
a = np.zeros(4)
x = np.arange(4)
indices = np.zeros(4,dtype=np.int) # duplicate indices
a[indices] += x
a
= array([ 3., 0., 0., 0.])
More interesting stuff about duplicate indices in this question.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…