Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.apply_along_axis() in Python
Next article icon

numpy.apply_along_axis() in Python

Last Updated : 28 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The numpy.apply_along_axis() function helps us to apply a required function to 1D slices of the given array. 
1d_func(ar, *args) : works on 1-D arrays, where ar is 1D slice of arr along axis.

Syntax : 

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

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  

What *args and **kwargs actually are? 

Both of these allow you to pass a variable no. of arguments to the function. 
*args : allow to send a non-keyword variable length argument list to the function. 

Python
# Python Program illustrating  # use of *args  args = [3, 8] a = list(range(*args)) print("use of args  : \n   ", a) 

Output : 

use of args  :      [3, 4, 5, 6, 7]


**kwargs: allows you to pass keyword variable length of argument to a function. It is used when we want to handle named argument in a function. 

Python
# Python Program illustrating  # use of **kwargs  def test_args_kwargs(in1, in2, in3):     print ("in1:", in1)     print ("in2:", in2)     print ("in3:", in3)           kwargs = {"in3": 1, "in2": "No.","in1":"geeks"} test_args_kwargs(**kwargs) 

Output : 

in1: geeks in2: No. in3: 1


Code 1: Python code explaining the use of numpy.apply_along_axis().  

Python
# Python Program illustrating # apply_along_axis() in NumPy  import numpy as geek   # 1D_func is "geek_fun" def geek_fun(a):     # Returning the sum of elements at start index and at last index     # inout array      return (a[0] + a[-1])   arr = geek.array([[1,2,3],                  [4,5,6],                  [7,8,9]])      '''               -> [1,2,3] <-   1 + 7                  [4,5,6]      2 + 8               -> [7,8,9] <-   3 + 9 '''  print("axis=0 : ", geek.apply_along_axis(geek_fun, 0, arr)) print("\n")  '''             |   |                [1,2,3]   1 + 3                [4,5,6]   4 + 6                [7,8,9]   7 + 9                 ^   ^                '''  print("axis=1 : ", geek.apply_along_axis(geek_fun, 1, arr)) 

Output : 

axis=0 :  [ 8 10 12]   axis=1 :  [ 4 10 16]


Code 2: Sorting using apply_along_axis() in NumPy Python 

Python
# Python Program illustrating # apply_along_axis() in NumPy  import numpy as geek   geek_array = geek.array([[8,1,7],                          [4,3,9],                          [5,2,6]])  # using pre-defined sorted function as 1D_func print("Sorted as per axis 1 : \n", geek.apply_along_axis(sorted, 1, geek_array))  print("\n")  print("Sorted as per axis 0 : \n", geek.apply_along_axis(sorted, 0, geek_array)) 

Output : 

Sorted as per axis 1 :   [[1 7 8]  [3 4 9]  [2 5 6]]   Sorted as per axis 0 :   [[4 1 6]  [5 2 7]  [8 3 9]]


Note : 
These codes won't run on online IDE's. So please, run them on your systems to explore the working.

   


Next Article
numpy.apply_along_axis() in Python

M

Mohit Gupta
Improve
Article Tags :
  • Misc
  • Python
  • Python-numpy
Practice Tags :
  • Misc
  • python

Similar Reads

    numpy.apply_over_axes() in Python
    The numpy.apply_over_axes()applies a function repeatedly over multiple axes in an array. Syntax : numpy.apply_over_axes(func, array, axes) 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. a
    3 min read
    numpy.moveaxis() function | Python
    numpy.moveaxis() function allows you to rearrange axes of an array. It is used when you need to shift dimensions of an array to different positions without altering the actual data. The syntax for the numpy.moveaxis() function is as follows:numpy.moveaxis(array, source, destination)Parameters:array:
    2 min read
    Matplotlib.axes.Axes.axis() in Python
    The Axes.axis() function in Matplotlib is used to get or set the properties of the x-axis and y-axis limits on a given Axes object. It provides an easy way to control the view limits, aspect ratio, and visibility of the axes in a plot. It's key feature include:Get the current axis limits as [xmin, x
    2 min read
    Matplotlib.axes.Axes.add_line() in Python
    Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
    2 min read
    Matplotlib.axes.Axes.inset_axes() in Python
    Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences