NumPy apply_along_axis()

apply_along_axis() 方法允许您将一个函数应用于多维数组的每一行或每一列,而无需使用显式循环。

示例

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# function to calculate the sum of an array
def sumArray(arr):
    return np.sum(arr)

# apply the sumArray function along the rows (axis=1) result = np.apply_along_axis(sumArray, axis=1, arr=arr)
print(result) # Output: [ 6 15 24]

apply_along_axis() 语法

apply_along_axis() 的语法是

numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)

apply_along_axis() 参数

apply_along_axis() 方法接受以下参数

  • func1d - 要沿着指定轴应用的函数
  • axis - 应用函数的轴
  • arr - 将应用函数的输入数组
  • *args**kwargs - func1d 中存在的附加参数和关键字参数

注意: func1d 应接受一维数组作为输入并返回单个值或一组值。

apply_along_axis()返回值

apply_along_axis() 方法返回应用函数后的结果数组。


示例 1:应用返回单个值的函数

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# define a function to return the last element of an array
def lastItem(subArr):
    return np.max(subArr[-1])

# return last item along the rows (axis=1) result = np.apply_along_axis(lastItem, axis=1, arr=arr)
print(result)
# return last item along the columns (axis=0) result = np.apply_along_axis(lastItem, axis=0, arr=arr)
print(result)

输出

[3 6 9]
[7 8 9]

示例 2:应用返回数组的函数

我们也可以从函数返回一个值数组。

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
               [4, 5, 6],
              [7, 8, 9]])

# function to return the square of elements of an array
def square(arr):
    return (arr*arr)

# return the square of elements result = np.apply_along_axis(square, axis = 0, arr=arr
) print(result)

输出

[[ 1  4  9]
 [16 25 36]
 [49 64 81]]

示例 3:应用返回 N 维数组的函数

我们可以从函数返回一个 N 维值数组。

让我们看一个例子。

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
               	[4, 5, 6],
                [7, 8, 9]])

# define a function that returns a 2D array
def square_and_cube(arr):
    return np.array([arr**2, arr**3])

# apply the square_and_cube function along the columns (axis=0) result = np.apply_along_axis(square_and_cube, axis=0, arr=arr)
print('Along axis 0\n',result)
# apply the square_and_cube function along the rows (axis=1) result = np.apply_along_axis(square_and_cube, axis=1, arr=arr)
print('Along axis 1\n',result)

输出

Along axis 0
[[[  1   4   9]
  [ 16  25  36]
  [ 49  64  81]]

 [[  1   8  27]
  [ 64 125 216]
  [343 512 729]]]

Along axis 1
 [[[  1   4   9]
  [  1   8  27]]

 [[ 16  25  36]
  [ 64 125 216]]

 [[ 49  64  81]
  [343 512 729]]]

对于返回更高维度数组的函数,维度会插入到轴维度所在的位置。


示例 4:将 lambda 函数应用于数组

我们也可以直接传递一个 lambda 函数,而不是定义一个函数。

让我们看一个例子。

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
               	[4, 5, 6],
              	[7, 8, 9]])

# apply the summation lambda function along the rows (axis=1) result = np.apply_along_axis(lambda arr:np.sum(arr), axis=1, arr=arr)
print(result)

输出

[ 6 15 24]

推荐阅读

Numpy apply_over_axes()

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战