This is a rather simple operation, but it is repeated millions of times in my actual code and, if possible, I'd like to improve its performance.
import numpy as np
# Initial data array
xx = np.random.uniform(0., 1., (3, 14, 1))
# Coefficients used to modify 'xx'
a, b, c = np.random.uniform(0., 1., 3)
# Operation on 'xx' to obtain the final array 'yy'
yy = xx[0] * a * b + xx[1] * b + xx[2] * c
The last line is the one I'd like to improve. Basically, each term in xx
is multiplied by a factor (given by the a, b, c
coefficients) and then all terms are added to give a final yy
array with the shape (14, 1)
vs the shape of the initial xx
array (3, 14, 1)
.
Is it possible to do this via numpy broadcasting?