Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:40:45+00:00 2026-05-28T13:40:45+00:00

I have come across the numpy.apply_along_axis function in some code. And I don’t understand

  • 0

I have come across the numpy.apply_along_axis function in some code. And I don’t understand the documentation about it.

This is an example of the documentation:

>>> def new_func(a):
...     """Divide elements of a by 2."""
...     return a * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(new_func, 0, b)
array([[ 0.5,  1. ,  1.5],
       [ 2. ,  2.5,  3. ],
       [ 3.5,  4. ,  4.5]])

As far I as thought I understood the documentation, I would have expected:

array([[ 0.5,  1. ,  1.5],
       [ 4  ,  5  ,  6  ],
       [ 7  ,  8  ,  9  ]])

i.e. having applied the function along the axis [1,2,3] which is axis 0 in [[1,2,3], [4,5,6], [7,8,9]]

Obviously I am wrong. Could you correct me ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:40:45+00:00Added an answer on May 28, 2026 at 1:40 pm

    apply_along_axis applies the supplied function along 1D slices of the input array, with the slices taken along the axis you specify. So in your example, new_func is applied over each slice of the array along the first axis. It becomes clearer if you use a vector valued function, rather than a scalar, like this:

    In [20]: b = np.array([[1,2,3], [4,5,6], [7,8,9]])
    
    In [21]: np.apply_along_axis(np.diff,0,b)
    Out[21]: 
    array([[3, 3, 3],
           [3, 3, 3]])
    
    In [22]: np.apply_along_axis(np.diff,1,b)
    Out[22]: 
    array([[1, 1],
           [1, 1],
           [1, 1]])
    

    Here, numpy.diff (i.e. the arithmetic difference of adjacent array elements) is applied along each slice of either the first or second axis (dimension) of the input array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across this line of legacy code, which I am trying to
I have come across some c code where the there is an enum type
I have come across a strange behavior with Microsoft Code Contracts that I don't
I have come across some code in a few projects which use if(Is.NotNull(SomeObject)) instead
I have come across this PHP code to check email address using SMTP without
I am playing with numpy and digging through documentation and I have come across
I have come across Java code in this form for the first time: object.methodA(new
I have come across this bit of jQuery and I am failing to understand
I have come across a strange bug in my code and I cannot understand
I have come across a refernce to a JavaScript syntax that I don't understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.