Skip to content Skip to sidebar Skip to footer

Numpy, Apply A List Of Functions Along Array Dimension

I have a list of functions of the type: func_list = [lambda x: function1(input), lambda x: function2(input), lambda x: function3(input), lamb

Solution 1:

You can iterate over your function list using np.apply_along_axis:

import numpy as npx= np.ranom.randn(100, 100)
for f in fun_list:
    x = np.apply_along_axis(f, 0, x)

Based on OP's Update

Assuming your functions and batches are the same in size:

batch = ... # tuple of 4 images  batch_out = tuple([np.apply_along_axis(f, 0, x) for f, x in zip(fun_list, batch)])

Solution 2:

I tried @Coldspeed's answer, and it does work (so I will accept it) but here is an alternative I found, without using for loops:

result = tuple(map(lambda x,y:x(y), functions, image_tuple))

Edit: forgot to add the tuple(), thanks @Coldspeed

Post a Comment for "Numpy, Apply A List Of Functions Along Array Dimension"