To show a simple example, let's assume I have a simple function for z-score normalization:
def standardizing(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 the results are what I expect:
>>> ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]])
>>> standardizing(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 say I don't want to return this array, but overwrite the values in-place. I am wondering why it doesn't work. For example,
def standardizing(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)
# some more processing steps with ary_new
return ary_new
>>> ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]])
>>> standardizing(ary, [0, 1])
array([[-1, 1],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 1, -1]])