I want map a numpy.array
from NxM to NxMx3, where a vector of three elements is a function of the original entry:
lambda x: [f1(x), f2(x), f3(x)]
However, things like numpy.vectorize
do not allow to change dimensions.
Sure, I can create an array of zeros and make a loop (and it is what I am doing by now), but it does not sound neither Pythonic nor efficient (as every looping in Python).
Is there a better way to perform an elementwise operation on numpy.array, producing a vector for each entry?
N
andM
are significantly larger than 3, the looping over the third dimension will have an insignificant effect on performance. And there's nothing un-pythonic in using for loops! What isn't very numpythonic or efficient is usingnp.vectorize
. You could try to convertf1
,f2
andf3
into a single function that took arrays and returned arrays. Without knowing what your functions are doing, it is not possible to know if this approach would suit your problem. – Jaime Jun 15 at 14:07