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 770271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:28:20+00:00 2026-05-14T18:28:20+00:00

I have a function foo that takes a NxM numpy array as an argument

  • 0

I have a function foo that takes a NxM numpy array as an argument and returns a scalar value. I have a AxNxM numpy array data, over which I’d like to map foo to give me a resultant numpy array of length A.

Curently, I’m doing this:

result = numpy.array([foo(x) for x in data])

It works, but it seems like I’m not taking advantage of the numpy magic (and speed). Is there a better way?

I’ve looked at numpy.vectorize, and numpy.apply_along_axis, but neither works for a function of 2D arrays.

EDIT: I’m doing boosted regression on 24×24 image patches, so my AxNxM is something like 1000x24x24. What I called foo above applies a Haar-like feature to a patch (so, not terribly computationally intensive).

  • 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-14T18:28:21+00:00Added an answer on May 14, 2026 at 6:28 pm

    If NxM is big (say, 100), they the cost of iterating over A will be amortized into basically nothing.

    Say the array is 1000 X 100 X 100.

    Iterating is O(1000), but the cumulative cost of the inside function is O(1000 X 100 X 100) – 10,000 times slower. (Note, my terminology is a bit wonky, but I do know what I’m talking about)

    I’m not sure, but you could try this:

    result = numpy.empty(data.shape[0])
    for i in range(len(data)):
        result[i] = foo(data[i])
    

    You would save a big of memory allocation on building the list … but the loop overhead would be greater.

    Or you could write a parallel version of the loop, and split it across multiple processes. That could be a lot faster, depending on how intensive foo is (as it would have to offset the data handling).

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

Sidebar

Related Questions

I have a function foo that takes a data frame as input and returns
I have a function foo(i) that takes an integer and takes a significant amount
I have a function that takes optional arguments as name/value pairs. function example(varargin) %
I have an ojbect with a function that takes an out argument. I want
I have a function that returns an NSError object by reference: NSData *foo(NSData *foo,
I have a C function that takes a function pointer as argument, it's a
I have written a function that takes two arguments and returns a SETOF result.
Say I have a function that takes a list and does something: (defun foo(aList)
I have a function searchWorkByName that takes key as an argument and use SQOL
I have a simple function that takes two variables by reference: void foo(int*& it2,

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.