我的目标是编写一个函数:(1)复制numpy数组,(2)修改此副本,(3)返回修改后的副本。但是,这并不像我想的那样有效......
为了展示一个简单的例子,我们假设我有一个简单的z分数归一化函数:
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)
结果是我的期望:
>>> 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]])
但是,假设我想要返回副本的修改版本。我想知道为什么它不起作用。例如,
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)
# 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]])
>>> standardizing2(ary, [0, 1])
array([[-1, 1],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 1, -1]])
但是如果我将它分配给一个新的数组,而没有“切片”,它会再次起作用
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]])
答案 0 :(得分:2)
当你这样做时
ary = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]])
您创建一个整数dtype数组。这意味着
ary_new = array.copy()
也是整数dtype的数组。 无法保存浮点数;当你试图将花车放入其中时:
ary_new[:, columns] = ...
它们会自动转换为整数。
如果需要浮点数组,则必须在创建数组时指定:
ary_new = array.astype(float)