我有一个最大值为 num 的 numpy 数组。我想通过 newMaxValue/num 缩放数组中的所有值,以便数组的新最大值为 newMaxValue。我试图将数组转换为 float ,然后进行除法,但我似乎无法成功地除法和乘法。我总是以零值数组结束。 这样做的正确方法是什么? 谢谢
请您参考如下方法:
确保将最大值转换为 float:
>>> from numpy import array
>>> a = array([1, 2, 3, 4, 5])
>>> new_max = 6
>>> a / max(a) # This is probably what happens to you
array([0, 0, 0, 0, 1])
>>> a / float(max(a)) # Convert that integer to a float and it'll work
array([ 0.2, 0.4, 0.6, 0.8, 1. ])
>>> a / float(max(a)) * new_max
array([ 1.2, 2.4, 3.6, 4.8, 6. ])
