Вы можете использовать numpy.bincount
:
import numpy as np
def simple_regression(y, coeff, L, l_index):
numerator = y*coeff
denominator = np.square(coeff)
numsum = np.zeros(L)
denomsum = np.zeros(L)
for (n,d,l) in zip(numerator,denominator,l_index):
numsum[l] += n
denomsum[l] += d
return numsum / denomsum
def simple_regression_pp(y, coeff, L, l_index):
numerator = y*coeff
denominator = np.square(coeff)
numsum = np.bincount(l_index, numerator, L)
denomsum = np.bincount(l_index, denominator, L)
return numsum / denomsum
def simple_regression_br(y, coeff, L, l_index):
numerator = y*coeff
denominator = np.square(coeff)
numsum = np.zeros(L)
denomsum = np.zeros(L)
np.add.at(numsum, l_index, numerator)
np.add.at(denomsum, l_index, denominator)
return numsum / denomsum
L, N = 1_000, 1_000_000
y, coeff = np.random.random((2, N))
l_index = np.random.randint(0, L, (N,))
from timeit import timeit
print('OP', timeit("simple_regression(y, coeff, L, l_index)", globals=globals(),
number=10), 'sec')
print('pp', timeit("simple_regression_pp(y, coeff, L, l_index)",
globals=globals(), number=10), 'sec')
print('br', timeit("simple_regression_br(y, coeff, L, l_index)",
globals=globals(), number=10), 'sec')
Пробный прогон:
OP 6.602819449035451 sec
pp 0.12009818502701819 sec
br 1.5504542298149318 sec