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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:59:43+00:00 2026-05-24T00:59:43+00:00

Is it possible to map a NumPy array in place? If yes, how? Given

  • 0

Is it possible to map a NumPy array in place? If yes, how?

Given a_values – 2D array – this is the bit of code that does the trick for me at the moment:

for row in range(len(a_values)):
    for col in range(len(a_values[0])):
        a_values[row][col] = dim(a_values[row][col])

But it’s so ugly that I suspect that somewhere within NumPy there must be a function that does the same with something looking like:

a_values.map_in_place(dim)

but if something like the above exists, I’ve been unable to find it.

  • 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-24T00:59:44+00:00Added an answer on May 24, 2026 at 12:59 am

    It’s only worth trying to do this in-place if you are under significant space constraints. If that’s the case, it is possible to speed up your code a little bit by iterating over a flattened view of the array. Since reshape returns a new view when possible, the data itself isn’t copied (unless the original has unusual structure).

    I don’t know of a better way to achieve bona fide in-place application of an arbitrary Python function.

    >>> def flat_for(a, f):
    ...     a = a.reshape(-1)
    ...     for i, v in enumerate(a):
    ...         a[i] = f(v)
    ... 
    >>> a = numpy.arange(25).reshape(5, 5)
    >>> flat_for(a, lambda x: x + 5)
    >>> a
    
    array([[ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19],
           [20, 21, 22, 23, 24],
           [25, 26, 27, 28, 29]])
    

    Some timings:

    >>> a = numpy.arange(2500).reshape(50, 50)
    >>> f = lambda x: x + 5
    >>> %timeit flat_for(a, f)
    1000 loops, best of 3: 1.86 ms per loop
    

    It’s about twice as fast as the nested loop version:

    >>> a = numpy.arange(2500).reshape(50, 50)
    >>> def nested_for(a, f):
    ...     for i in range(len(a)):
    ...         for j in range(len(a[0])):
    ...             a[i][j] = f(a[i][j])
    ... 
    >>> %timeit nested_for(a, f)
    100 loops, best of 3: 3.79 ms per loop
    

    Of course vectorize is still faster, so if you can make a copy, use that:

    >>> a = numpy.arange(2500).reshape(50, 50)
    >>> g = numpy.vectorize(lambda x: x + 5)
    >>> %timeit g(a)
    1000 loops, best of 3: 584 us per loop
    

    And if you can rewrite dim using built-in ufuncs, then please, please, don’t vectorize:

    >>> a = numpy.arange(2500).reshape(50, 50)
    >>> %timeit a + 5
    100000 loops, best of 3: 4.66 us per loop
    

    numpy does operations like += in place, just as you might expect — so you can get the speed of a ufunc with in-place application at no cost. Sometimes it’s even faster! See here for an example.


    By the way, my original answer to this question, which can be viewed in its edit history, is ridiculous, and involved vectorizing over indices into a. Not only did it have to do some funky stuff to bypass vectorize‘s type-detection mechanism, it turned out to be just as slow as the nested loop version. So much for cleverness!

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

Sidebar

Related Questions

Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
Is it possible to add an image overlay to a google map that scales
Is this possible to map a SQL Native query (instead of a table) with
Given a binary string coded on 1 byte, is it possible to map several
Is it possible to map an enum as a string using Fluent Nhibernate?
I'm wondering, is it possible to somehow map a key-press event to act like
Is it possible to do a google map lookup using the google maps API
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
I am looking to take a map<char, vector<char> > and generate each possible map<char,

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.