Numpy modify array in place?

Posted by User on Stack Overflow See other posts from Stack Overflow or by User
Published on 2012-04-13T23:10:30Z Indexed on 2012/04/13 23:29 UTC
Read the original article Hit count: 254

Filed under:
|
|
|

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()

© Stack Overflow or respective owner

Related posts about python

Related posts about arrays