np.apply_along_axis: Numpy apply_along_axis() Method

The apply_along_axis() function is used to apply the function to 1D slices along the given axis. It executes func1d(a, *args) where func1d operates on 1D arrays, and a is the 1D slice of arr along the axis. The np.apply_along_axis() helps us apply a required function to 1D slices of the given array.

np.apply_along_axis

The np.apply_along_axis() is a numpy library function used to apply the function to 1D slices along the given axis of an nd-array. The numpy.apply_along_axis() function accepts 1d_func, axis, array, *args, **kwargs arguments and returns the output array, except along the axis dimension.

Syntax

numpy.apply_along_axis(1d_func, axis, array, *args, **kwargs)

Parameters

The apply_along_axis() function has 5 parameters:

  1. 1d_func: This is the required function that will operate the 1D array. It can be applied in 1D slices of the input array and along a particular axis.
  2. axis: This is the required axis along which we want the input array to be sliced.
  3. array: This is the array on which we want to work.
  4. *args: This is an additional argument to 1D function (1d_func).
  5. **kwargs: Additional named argument to 1D function (1d_func).

Return Value

The apply_along_axis() function returns an output array, except along the axis dimension. The axis is removed and replaced with new dimensions equal to the shape of a return value of 1d_func. So if 1d_func returns a scalar out will have one fewer dimension than an array.

Program to show the work of apply_along_axis() on a 1D array

See the following code.

#We will find sum of all elements of the array

#Importing numpy
import numpy as np


#Function to calculate sum of all elements
def arr_sum(arr):
    return np.sum(arr)


#We will create an 1D array
arr = np.array([40, 2, 4, 6])
#Printing the array
print("The array is: ", arr)
#Shape of the array
print("Shape of the array is : ", np.shape(arr))

#Now we will call apply_along_axis to get the output
print("Sum of all elements of the array is: ")
print(np.apply_along_axis(arr_sum, 0, arr))

Output

The array is:  [40  2  4  6]
Shape of the array is :  (4,)
Sum of all elements of the array is:
52

Explanation

In this program, we have declared a 1D-array, and then we have printed that array and its shape. Then we wanted to get the sum of all the array elements using apply_along_axis.

To do so, we have first declared a function “arr_sum(),” and there we have returned np.sum()- which calculates the sum of all numpy array elements. 

At last, we have called apply_along_axis() in which we have passed the fun arr_sum(), axis value=0 and the array name=arr. And the result we got is the sum of all array elements.

Program to show the work of apply_along_axis on a 2D array

See the following code.

#Importing numpy
import numpy as np


#We will create a function which will count sum of arr elements
def arr_sum(arr):
    return np.sum(arr)


#We will create a 2D array
#Of shape 4x3
arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
#Printing the array
print("The array is: ")
print(arr)

#Now we will call apply_along_axis to get the output

print("Sum of all elements column wise of the array is: ")
print(np.apply_along_axis(arr_sum, 0, arr))

print("\nSum of all elements row wise of the array is: ")
print(np.apply_along_axis(arr_sum, 1, arr))

Output

The array is:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [50 51 52]]
Sum of all elements column wise of the array is:
[62 66 70]

Sum of all elements row wise of the array is:
[  6  15  24 153]

Explanation

In this program, we have declared a 2D-array, and then we have printed that array and its shape. Then we wanted to get the sum of all the array elements using apply_along_axis. To do so, we have first declared a function “arr_sum(),” and there we have returned np.sum()- which calculates the sum of all numpy array elements. 

At last, we have passed the apply_along_axis() in which we have passed the fun arr_sum(), and we have given axis =0 ( which calculated sum of array elements column-wise) and axis=1 ( which calculated sum of array elements row-wise) and one more parameter, the array name.

We can see that, when axis=0, the sum is [62 66 70] and when axis=1, the sum is [ 6 15 24 153].

That’s it for this tutorial.

See also

NumPy argmin()

NumPy argmax()

NumPy take()

NumPy any()

NumPy all()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.