尝试规范化 numpy.array 中的列时出现意外行为(版本 1.17.4)

Unexpected behavior when trying to normalize a column in numpy.array (version 1.17.4)

所以,我试图规范化(即 max = 1,min = value/max)numpy 数组中的特定列。 我希望这段代码可以解决问题:

bar = np.arange(12).reshape(6,2)

bar
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11]])

bar[:,1] = bar[:,1] / bar[:,1].max()
bar
array([[ 0,  0],
       [ 2,  0],
       [ 4,  0],
       [ 6,  0],
       [ 8,  0],
       [10,  1]])
如果每个值的类型为 'float'。

将按预期工作

foo = np.array([[1.1,2.2],
               [3.3,4.4],
               [5.5,6.6]])
foo[:,1] = foo[:,1] / foo[:,1].max()

foo
array([[1.1       , 0.33333333],
       [3.3       , 0.66666667],
       [5.5       , 1.        ]])

我想我想问的是这个默认值在哪里 'int' 我在这里不见了? (我将其视为 'learning opportunity')

来自 np.arange documentation :

dtype : dtype
The type of the output array. If dtype is not given, infer the data type from the other input arguments.

由于您传递的是 int 值,它会推断数组中的值是 int,因此它们不会更改为 float,如果您愿意,可以这样做:

bar = np.arange(12.0).reshape(6,2)

如果你简单的执行:

out = bar[:,1] / bar[:,1].max()
print(out)
>>> [0.09090909 0.27272727 0.45454545 0.63636364 0.81818182 1.        ]

它工作得很好,因为 out 是一个新创建的浮点数组,用于存储这些浮点值。但是 np.arange(12) 默认给你一个 int 数组。 bar[:,1] = bar[:,1] / bar[:,1].max() 尝试将浮点值存储在整数数组中,所有值都变成整数,你得到 [0 0 0 0 0 1].

默认设置数组为浮点数:

bar = np.arange(12, dtype='float').reshape(6,2)

或者,您也可以使用:

bar = np.arange(12).reshape(6,2).astype('float')

我们需要在整个程序中更改数组的数据类型并不少见,因为您可能并不总是需要您最初定义的数据类型。所以.astype()其实在各种场景下都非常得心应手