Numpy, apply function list by array size
I have a list of functions like:
func_list = [lambda x: function1(input),
lambda x: function2(input),
lambda x: function3(input),
lambda x: x]
and an array of the form [4, 200, 200, 1] (batch of images).
I want to apply a list of functions in order along the 0th axis.
EDIT: Rephrase the problem. This is equivalent to the above. Let's say, instead of an array, I have a tuple of 4 identical arrays, of the form (200, 200, 1) and I want to apply function 1 to the first element, function2 on the second element, etc. Can this be done without a for loop?
+3
source to share
2 answers
You can iterate over the list of functions with np.apply_along_axis
:
import numpy as np
x = np.ranom.randn(100, 100)
for f in fun_list:
x = np.apply_along_axis(f, 0, x)
Based on OP update
Assuming your functions and batches are the same size:
batch = ... # tuple of 4 images
batch_out = tuple([np.apply_along_axis(f, 0, x) for f, x in zip(fun_list, batch)])
+2
source to share