the interpreter sees the code this way.
import numpy as np
a = np.zeros(10)
b1 = a
b2 = b1 + 1 # make a new array b1 + 1 and save it in b2
print(b2)
the +
operator in numpy (or out of numpy) allocates new memory on the heap, this new memory is then assigned to the name b
. (this is also a convention outside of numpy)
to prevent this, you should use the np.add
function directly and pass the out
parameter. (most numpy functions have an out
parameter for this purpose)
import numpy as np
a = np.zeros(10)
b = a
np.add(b,1,out=b) # no new memory is allocated here.
print(a)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
alternatively this will do almost the same end result but is less efficient.
import numpy as np
a = np.zeros(10)
b = a
b[:] = b + 1
print(a)
which would create a new array that will contain b + 1
then copy its elements into the preexisting array of b.
using the out
parameter is useful when working with large data, where a simple "reserve a temporary array for results" may cause a memory error, especially when working with gpus using cupy (which has the same numpy interface) where memory is very restricted.