Numpy,沿数组维度应用函数列表 [英] Numpy, apply a list of functions along array dimension

查看:59
本文介绍了Numpy,沿数组维度应用函数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型的函数列表:

I have a list of functions of the type:

func_list = [lambda x: function1(input),
             lambda x: function2(input),
             lambda x: function3(input),
             lambda x: x]

和一个形状为[4,200,200,1]的数组(一批图像).

and an array of shape [4, 200, 200, 1] (a batch of images).

我想沿第0轴依次应用功能列表.

I want to apply the list of functions, in order, along the 0th axis.

改写问题.这等同于以上内容.说,不是数组,我有四个相同数组的元组,它们的形状为(200,200,1),我想在第一个元素上应用function1,在第二个元素上应用function2,依此类推.一个for循环?

Rephrasing the problem. This is equivalent to the above. Say, instead of the array, I have a tuple of 4 identical arrays, of shape (200, 200, 1), and I want to apply function1 on the first element, function2 on the second element, etc. Can this be done without a for loop?

推荐答案

您可以使用 np.apply_along_axis 遍历函数列表:

You can iterate over your function list using 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)

基于OP的更新

假设您的函数和批处理的大小相同:

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)])

这篇关于Numpy,沿数组维度应用函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆