【发布时间】:2020-08-14 11:35:39
【问题描述】:
我有一个这样的函数列表:
test_functions = [lambda x: np.sin(x),
lambda x: np.sin(x * np.cos(x)),
lambda x: np.exp(np.sin(10*x)),
lambda x: np.sin(np.exp(np.abs(np.cos(x))))]
我还有一个矩阵[batch_size, channels, sample_len],这样每个样本代表x axis采样点,例如:
tensor = [[[0, 2.5, 7]],
[[1, 2.5, 3]]]
所以batch_size = 2、channels = 1 和sample_len = 3。其中示例本身意味着我想知道x=0, x=2.5 and x=7 处的函数值(对于第一个示例)。
我生成了一个随机整数矩阵 (shape=(batch_size, channels)),这样条目就是索引要使用的函数。
例如,如果矩阵是:
functions_to_be_used = [[0], [2]]
然后我想实现某种可以产生的索引:
result = [[[np.sin(0), np.sin(2.5), np.sin(7)]],
[np.exp(np.sin(10*1)), np.exp(np.sin(10*2.5)), np.exp(np.sin(10*3))]]]
类似的东西(不起作用):
result = functions[functions_to_be_used](tensor[:, :, :])
为了性能,我想避免使用 for 循环。
【问题讨论】:
标签: python-3.x numpy indexing data-science