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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:31:33+00:00 2026-06-02T03:31:33+00:00

I have the following code which is attempting to normalize the values of an

  • 0

I have the following code which is attempting to normalize the values of an m x n array (It will be used as input to a neural network, where m is the number of training examples and n is the number of features).

However, when I inspect the array in the interpreter after the script runs, I see that the values are not normalized; that is, they still have the original values. I guess this is because the assignment to the array variable inside the function is only seen within the function.

How can I do this normalization in place? Or do I have to return a new array from the normalize function?

import numpy

def normalize(array, imin = -1, imax = 1):
    """I = Imin + (Imax-Imin)*(D-Dmin)/(Dmax-Dmin)"""

    dmin = array.min()
    dmax = array.max()

    array = imin + (imax - imin)*(array - dmin)/(dmax - dmin)
    print array[0]


def main():

    array = numpy.loadtxt('test.csv', delimiter=',', skiprows=1)
    for column in array.T:
        normalize(column)

    return array

if __name__ == "__main__":
    a = main()
  • 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-06-02T03:31:35+00:00Added an answer on June 2, 2026 at 3:31 am

    If you want to apply mathematical operations to a numpy array in-place, you can simply use the standard in-place operators +=, -=, /=, etc. So for example:

    >>> def foo(a):
    ...     a += 10
    ... 
    >>> a = numpy.arange(10)
    >>> a
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    >>> foo(a)
    >>> a
    array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    

    The in-place version of these operations is a tad faster to boot, especially for larger arrays:

    >>> def normalize_inplace(array, imin=-1, imax=1):
    ...         dmin = array.min()
    ...         dmax = array.max()
    ...         array -= dmin
    ...         array *= imax - imin
    ...         array /= dmax - dmin
    ...         array += imin
    ...     
    >>> def normalize_copy(array, imin=-1, imax=1):
    ...         dmin = array.min()
    ...         dmax = array.max()
    ...         return imin + (imax - imin) * (array - dmin) / (dmax - dmin)
    ... 
    >>> a = numpy.arange(10000, dtype='f')
    >>> %timeit normalize_inplace(a)
    10000 loops, best of 3: 144 us per loop
    >>> %timeit normalize_copy(a)
    10000 loops, best of 3: 146 us per loop
    >>> a = numpy.arange(1000000, dtype='f')
    >>> %timeit normalize_inplace(a)
    100 loops, best of 3: 12.8 ms per loop
    >>> %timeit normalize_copy(a)
    100 loops, best of 3: 16.4 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code which is attempting to check for password complexity
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which throws an exception COM Exception was unhandled in
I have the following code which inserts multiple records into MySQL: foreach ($_POST AS
I have the following code which sometimes return as true and sometimes doesn't. Any
I have the following code which works just fine when the method is POST,
I have the following code which should put programs startable in Bash. if [

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.