This code implements a single variable regression without an intercept. It works, but I can't figure out a way to do it without resorting to using slow python iteration. Any ideas?
# y: numpy array of n values, for large n
# coeff: numpy array of n values, for large n
# L : size of result
# l_indices : numpy array of indices from 0 to L-1
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
fundamentally an operation like the following, that doesn't do a bunch of memory allocation:
numsum[l] = np.sum(numerator[l_index == l])
(Doing it that way is much lower then my first code)