simple but difficult numpy memory reference problem
Question:
import numpy as np
a = np.array([0,0,0,0,0,0,0,0])
b = np.array([4,2,3])
c = np.array([5,5,2])
for i, e in enumerate(c):
a[e] += b[i]
print(a)
# [0 0 3 0 0 6 0 0]
a = np.array([0,0,0,0,0,0,0,0])
b = np.array([4,2,3])
c = np.array([5,5,2])
a[c] += b[np.arange(len(b))]
print(a)
# [0 0 3 0 0 2 0 0]
The actual length of c is incredibly long. The problem comes from here.
The python for loop is too slow.
So I want to replace this with numpy without python for loop, but I can’t find a way.
I think it’s because of the memory reference.
Is there a way to change the result value of this to the same?
Answers:
You can try np.add.at
:
cnt = np.arange(0, len(c))
np.add.at(a, c, b[cnt])
print(a)
Prints:
[0 0 3 0 0 6 0 0]
import numpy as np
a = np.array([0,0,0,0,0,0,0,0])
b = np.array([4,2,3])
c = np.array([5,5,2])
for i, e in enumerate(c):
a[e] += b[i]
print(a)
# [0 0 3 0 0 6 0 0]
a = np.array([0,0,0,0,0,0,0,0])
b = np.array([4,2,3])
c = np.array([5,5,2])
a[c] += b[np.arange(len(b))]
print(a)
# [0 0 3 0 0 2 0 0]
The actual length of c is incredibly long. The problem comes from here.
The python for loop is too slow.
So I want to replace this with numpy without python for loop, but I can’t find a way.
I think it’s because of the memory reference.
Is there a way to change the result value of this to the same?
You can try np.add.at
:
cnt = np.arange(0, len(c))
np.add.at(a, c, b[cnt])
print(a)
Prints:
[0 0 3 0 0 6 0 0]