python - Multiplying each element of a numpy array and summing it up -
i have 2 numpy arrays, x , y. x of size m, , y of size n. need multiply each element of y every element of x, , sum up.
something [sum(x[0]*y) sum(x[1]*y) sum(x[n]*y)]
this mean
np.sum(x[:, np.newaxis] * y, axis=1) however typically x , y large , doing
x[:, np.newaxis] * y creates huge temporary array, blows stuff. there better way of implementing this?
if you're multiplying each element of y every element of x, multiply elements of x first, use multiply array y number , sum:
num = x.prod() (num * y).sum() edit: array specify can obtained multiplying array x sum of elements of y:
x * y.sum()
Comments
Post a Comment