多个数组的Python apply_along_axis [英] Python apply_along_axis of multiple arrays
问题描述
如果我有一个函数f(x),它使用一个1d数组作为参数并生成一个1d数组作为输出,则可以使用numpy.apply_along_axis 将该函数应用于二维数组X的每一行,该数组的行是f的有效参数.
If I have a function, f(x) which takes a single 1d-array as argument and produces a 1d-array as output, I can use numpy.apply_along_axis to apply the function, to each row of a 2d-array X whose rows are valid arguments for f.
现在,我想使用带有两个参数的函数来做类似的事情. 例如.我有一个函数f(x,y),它将两个1d数组作为参数,我也有两个2d数组X,Y都具有n行.我想将f应用于每对行,从而产生一个具有n行的数组.
Now I want to do the analogous thing with a function that takes two arguments. E.g. I have a function f(x, y) which takes tow 1d-arrays as arguments and I also have two 2d-arrays X, Y both with n rows. I want to apply f to each pair of rows, producing an array which has again n rows.
如何有效地做到这一点?
How to accomplish this in an efficient way?
我也对变体感兴趣,其中f接受更多参数或涉及更高维数组:
I am also interested in variants, where f takes more arguments or higher dimensional arrays are involved:
例如,f可以采用3个形状为(2,2)的数组x,y,z; (3,); (5,) 并产生形状(4,4)的结果.
For example f could take 3 arrays x, y, z of shape (2,2); (3,); (5,) and produce a result of shape (4,4).
我的X,Y,Z形状为(50、100、2、2); (50,100,3); (50,100,5) 并希望得到形状为(50,100,4,4)的结果
I have X, Y, Z of shapes (50, 100, 2, 2); (50, 100, 3); (50, 100, 5) and want a result of shape (50, 100, 4, 4)
推荐答案
查看numpy.apply_along_axis
的代码,我发现它只是在其他维度上进行迭代,将函数应用于每个行".还有一些额外的代码可以使尺寸约为2.但是对于2d X
,它可以归结为:
Looking at the code for numpy.apply_along_axis
I see that it just iterates over the other dimensions, applying your function to each 'row'. There's extra code that allows for dimensions about 2. But for 2d X
it boils down to:
result = np.empty_like(X)
for i, x in enumerate(X):
result[i] = func1d(x)
还有一些代码可以推断result
应该具有的形状.例如,如果func1d
是np.sum
,则result
将是1d,而不是输入的2d.
There's also code to deduce what shape the result
should have. For example if func1d
is np.sum
, then result
will be 1d, not 2d like the input.
因此,此功能没有特殊的效率".对多个输入的扩展可以是普通的Python zip:
So there's not special 'efficiency' in this function. An extension to multiple inputs could be a normal Python zip:
result = np.empty_like(X)
for i,(x,y) in enumerate(zip(X,Y)):
result[i] = func1d(x,y)
np.ndindex
是用于生成索引的便捷工具.值得看一下它的代码.它使用通用的numpy迭代器np.nditer
,该迭代器请参见: http: //docs.scipy.org/doc/numpy/reference/arrays.nditer.html
np.ndindex
is handy tool for generating indices. It's worth looking at its code. It uses the general purpose numpy iterator, np.nditer
, which see: http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html
例如,f可以采用3个形状为(2,2)的数组x,y,z; (3,); (5,)并产生形状(4,4)的结果.
For example f could take 3 arrays x, y, z of shape (2,2); (3,); (5,) and produce a result of shape (4,4).
我的X,Y,Z形状为(50、100、2、2); (50,100,3); (50,100,5)并想要形状的结果(50,100,4,4)
I have X, Y, Z of shapes (50, 100, 2, 2); (50, 100, 3); (50, 100, 5) and want a result of shape (50, 100, 4, 4)
for i,j in np.ndindex(50,100):
result[i,j,:,:] = f(X[i,j,:,:], Y[i,j,:,:], Z[i,j,:,:])
:"不是必需的,但请明确说明我们正在对其中2个维度进行索引,并对其余维度进行切片.如果要在第1维和第3维上进行迭代,然后在第2维上切片,则需要使用它们.
The ':' aren't necessary, but make it clear that we are indexing on 2 of the dimensions, and slicing the rest. They would be required if you wanted to iterate on the 1 and 3rd dimensions, and slice the 2nd.
这篇关于多个数组的Python apply_along_axis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!