python - How to: (1) making a copy of a numpy array, (2) modifing this copy, and (3) returning the modified copy -


my goal write function (1) makes copy of numpy array, (2) modifies copy, , (3) returns modified copy. however, doesn't work thought would...

to show simple example, let's assume have simple function z-score normalization:

def standardizing1(array, columns, ddof=0):      ary_new = array.copy()     if len(ary_new.shape) == 1:         ary_new = ary_new[:, np.newaxis]      return (ary_new[:, columns] - ary_new[:, columns].mean(axis=0)) /\                        ary_new[:, columns].std(axis=0, ddof=ddof) 

and results expect:

>>> ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]]) >>> standardizing1(ary, [0, 1])  array([[-1.46385011,  1.46385011],    [-0.87831007,  0.87831007],    [-0.29277002,  0.29277002],    [ 0.29277002, -0.29277002],    [ 0.87831007, -0.87831007],    [ 1.46385011, -1.46385011]]) 

however, let's want return modified version of copy. wondering why doesn't work. example,

def standardizing2(array, columns, ddof=0):      ary_new = array.copy()     if len(ary_new.shape) == 1:         ary_new = ary_new[:, np.newaxis]      ary_new[:, columns] = (ary_new[:, columns] - ary_new[:, columns].mean(axis=0)) /\                        ary_new[:, columns].std(axis=0, ddof=ddof)      # more processing steps ary_new     return ary_new   


>>> ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]]) >>> standardizing2(ary, [0, 1])  array([[-1,  1],    [ 0,  0],    [ 0,  0],    [ 0,  0],    [ 0,  0],    [ 1, -1]]) 

but if assign new array, without "slicing", works again

def standardizing3(array, columns, ddof=0):      ary_new = array.copy()     if len(ary_new.shape) == 1:         ary_new = ary_new[:, np.newaxis]      some_ary = (ary_new[:, columns] - ary_new[:, columns].mean(axis=0)) /\                        ary_new[:, columns].std(axis=0, ddof=ddof)      return some_ary 


>>>> ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]]) >>> standardizing3(ary, [0, 1])  array([[-1.46385011,  1.46385011],    [-0.87831007,  0.87831007],    [-0.29277002,  0.29277002],    [ 0.29277002, -0.29277002],    [ 0.87831007, -0.87831007],    [ 1.46385011, -1.46385011]]) 

when do

ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]]) 

you create array of integer dtype. means that

ary_new = array.copy() 

is array of integer dtype. cannot hold floating-point numbers; when try put floats it:

ary_new[:, columns] = ... 

they automatically cast integers.

if want array of floats, have specify when create array:

ary_new = array.astype(float) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -