I have an NxMx3 numpy array with dtype=object. I also have a function f(a,b,c) which takes the three elements in the last axis of this array and returns a np.int32. My question is how do I apply f to my NxMx3 array to yield an NxM array with dtype=np.int32?
My current solution is to use
newarr = np.fromfunction(lambda i,j: f(arr[i,j,0], arr[i,j,1], arr[i,j,2]),
arr.shape[:2], dtype=np.int)
although this is a little more verbose than I had hoped.
You could use
vectorize:This can be simplified by axis rolling and iteration:
Alternatively you can split the array explicitly with
dsplit:or
or
However,
apply_along_axisis probably simpler: