努比。在Python中将_应用于_轴()

这个 努比。将_应用于_轴() 在阵列中的多个轴上重复应用函数。

null

语法:

numpy.apply_over_axes(func, array, axes)

参数:

1d_func  : the required function to perform over 1D array. It can only be applied in          1D slices of input array and that too along a particular axis. axis     : required axis along which we want input array to be slicedarray    : Input array to work on *args    : Additional arguments to 1D_function **kwargs : Additional arguments to 1D_function  

返回:

The output array. Shape of the output array can be different depending on whether func changes the shape of its output with respect to its input.

代码1:

python

# Python Program illustrating
# apply_over_axis() in NumPy
import numpy as geek
# Using a 3D array
geek_array = geek.arange( 16 ).reshape( 2 , 2 , 4 )
print ( "geek array  :" , geek_array)
# Applying pre-defined sum function over the axis of 3D array
print ( "func sum : " , geek.apply_over_axes(geek. sum , geek_array, [ 1 , 1 , 0 ]))
# Applying pre-defined min function over the axis of 3D array
print ( "func min : " , geek.apply_over_axes(geek. min , geek_array, [ 1 , 1 , 0 ]))


输出:

geek array  : [[[ 0  1  2  3]  [ 4  5  6  7]] [[ 8  9 10 11]  [12 13 14 15]]]func sum :   [[[24 28 32 36]]]func min :   [[[0 1 2 3]]]

代码2:

python

# Python Program illustrating
# apply_over_axis() in NumPy
import numpy as geek
# Using a 2D array
geek_array = geek.arange( 16 ).reshape( 4 , 4 )
print ( "geek array  :" , geek_array)
"""
->[[ 0  1  2  3]    min : 0     max : 3    sum =  0 + 1 + 2 + 3
-> [ 4  5  6  7]    min : 4     max : 7    sum =  4 + 5 + 6 + 7
-> [ 8  9 10 11]    min : 8     max : 11   sum =  8 + 9 + 10 + 11
-> [12 13 14 15]]   min : 12    max : 15   sum =  12 + 13 + 14 + 15
"""
# Applying pre-defined min function over the axis of 2D array
print ( "Applying func max : " , geek.apply_over_axes(geek. max , geek_array, [ 1 , - 1 ]))
# Applying pre-defined min function over the axis of 2D array
print ( "Applying func min : " , geek.apply_over_axes(geek. min , geek_array, [ 1 , - 1 ]))
# Applying pre-defined sum function over the axis of 2D array
print ( "Applying func sum : " , geek.apply_over_axes(geek. sum , geek_array, [ 1 , - 1 ]))


输出:

geek array  : [[ 0  1  2  3] [ 4  5  6  7] [ 8  9 10 11] [12 13 14 15]]Applying func max :   [[ 3] [ 7] [11] [15]]Applying func min :   [[ 0] [ 4] [ 8] [12]]Applying func sum :   [[ 6] [22] [38] [54]]

代码3:相当于代码2,不使用numpy。将_应用于_轴()

python

# Python Program illustrating
# equivalent to apply_over_axis()
import numpy as geek
# Using a 3D array
geek_array = geek.arange( 16 ).reshape( 2 , 2 , 4 )
print ( "geek array  :" , geek_array)
# returning sum of all elements as per the axis
print ( "func : " , geek. sum (geek_array, axis = ( 1 , 0 , 2 ), keepdims = True ))


输出:

geek array  : [[[ 0  1  2  3]  [ 4  5  6  7]] [[ 8  9 10 11]  [12 13 14 15]]]func :  [[[120]]]

参考资料: https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.apply_over_axes.html

注: 这些代码不会在online-ID上运行。请在您的系统上运行它们以探索工作环境。

本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享