Change language

numpy.apply_over_axes () in Python

|

Parameters :

  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 sliced ​​ array:  Input array to work on  * args:  Additional arguments to 1D_function  ** kwargs:  Additional arguments to 1D_function 

Return:

 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. 

Code 1:

# 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 a predefined sum function along the axis of a 3D array

print ( "func sum:" , geek.apply_over_axes (geek. sum , geek_array, [ 1 , 1 , 0 ]))

 
# Applying the predefined min function along the 3D array axis

print ( " func min: " , geek.apply_over_axes (geek. min , geek_array, [ 1 , 1 , 0 ]))

Output:

 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]]] 

Code 2:

# Python program illustrating
# apply_over_axis () in NumPy

 

import numpy as geek 

 
# Using 2D -array

geek_array = geek.arange ( 16 ). reshape ( 4 , 4 )

print ( "geek array :" , geek_array)

 
"" "

-" [[0 1 2 3] min: 0 max: 3 total = 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 a predefined min functions along the 2D array axis

print ( " Applying func max: " , geek.apply_over_axes (geek. max , geek_array, [ 1 , - 1 ]))

 
# Applying the predefined min function along the 2D array axis

print ( "Applying func min:" , geek.apply_over_axes (geek.  min , geek_array, [ 1 , - 1 ]))

 
# Applies a predefined sum function along the axis of a two-dimensional array

print ( "Applying func sum:" , geek.apply_over_axes (geek. sum , geek_array, [ 1 , - 1 ]))

Exit :

 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]] 

Code 3: Equivalent to Code 2 without using numpy.apply_over_axis ()

# 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)

 
# return the sum of all elements along the axis

print ( "func:" , geek. sum (geek_array, axis = ( 1 , 0 , 2 ), keepdims = True ))

Output:

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

Links:  
https://docs.scipy.org/doc/numpy-dev/ reference / generated / numpy.apply_over_axes.html
Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work.
,
This article is provided by Mohit Gupta_OMG

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method