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

  • SEARCH
  • Home
  • 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 6675069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:48:30+00:00 2026-05-26T03:48:30+00:00

If I wanted to apply a function row-wise (or column-wise) to an ndarray, do

  • 0

If I wanted to apply a function row-wise (or column-wise) to an ndarray, do I look to ufuncs (doesn’t seem like it) or some type of array broadcasting (not what I’m looking for either?) ?

Edit

I am looking for something like R’s apply function. For instance,

apply(X,1,function(x) x*2)

would multiply 2 to each row of X through an anonymously defined function, but could also be a named function. (This is of course a silly, contrived example in which apply is not actually needed). There is no generic way to apply a function across an NumPy array’s “axis”, ?

  • 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-26T03:48:30+00:00Added an answer on May 26, 2026 at 3:48 am

    First off, many numpy functions take an axis argument. It’s probably possible (and better) to do what you want with that sort of approach.

    However, a generic “apply this function row-wise” approach would look something like this:

    import numpy as np
    
    def rowwise(func):
        def new_func(array2d, **kwargs):
            # Run the function once to determine the size of the output
            val = func(array2d[0], **kwargs)
            output_array = np.zeros((array2d.shape[0], val.size), dtype=val.dtype)
            output_array[0] = val
            for i,row in enumerate(array2d[1:], start=1):
                output_array[i] = func(row, **kwargs)
            return output_array
        return new_func
    
    @rowwise
    def test(data):
        return np.cumsum(data)
    
    x = np.arange(20).reshape((4,5))
    print test(x)
    

    Keep in mind that we can do exactly the same thing with just:

    np.cumsum(x, axis=1)
    

    There’s often a better way that the generic approach, especially with numpy.

    Edit:

    I completely forgot about it, but the above is essentially equivalent to numpy.apply_along_axis.

    So, we could re-write that as:

    import numpy as np
    
    def test(row):
        return np.cumsum(row)
    
    x = np.arange(20).reshape((4,5))
    print np.apply_along_axis(test, 1, x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Firebug console function calls that I wanted to disable when Firebug
Possible Duplicate: how to apply a function to every row of a matrix (or
I wanted to invoke a function using the javascript apply() method. This works fine
<? while($row = mysql_fetch_array($result)) { $result2 = $row['end_time']; echo<td><div id=defaultCountdown></div></td>; ?> <script type=text/javascript> $(function
Wanted to get some consensus around a UI feature I'm working on right now.
I wanted some of those spiffy rounded corners for a web project that I'm
I wanted to emulate a popular flash game, Chrontron, in C++ and needed some
I wanted to do something like this: <asp:Label ID=lblMyLabel onclick=lblMyLabel_Click runat=server>My Label</asp:Label> I know
Say I have function X in file A, and I wanted to move that
I want to apply a 3 argument function in different ways based on a

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.