本文介绍了NumPy数组的最小-最大归一化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下numpy数组:
I have the following numpy array:
foo = np.array([[0.0, 10.0], [0.13216, 12.11837], [0.25379, 42.05027], [0.30874, 13.11784]])
产生:
[[ 0. 10. ]
[ 0.13216 12.11837]
[ 0.25379 42.05027]
[ 0.30874 13.11784]]
如何标准化此数组的Y分量.所以它给了我类似的东西:
How can I normalize the Y component of this array. So it gives me something like:
[[ 0. 0. ]
[ 0.13216 0.06 ]
[ 0.25379 1 ]
[ 0.30874 0.097]]
推荐答案
请参阅此交叉验证链接,,看来您可以在foo
的最后一列执行最小-最大标准化.
Referring to this Cross Validated Link, How to normalize data to 0-1 range?, it looks like you can perform min-max normalisation on the last column of foo
.
v = foo[:, 1] # foo[:, -1] for the last column
foo[:, 1] = (v - v.min()) / (v.max() - v.min())
foo
array([[ 0. , 0. ],
[ 0.13216 , 0.06609523],
[ 0.25379 , 1. ],
[ 0.30874 , 0.09727968]])
执行归一化的另一种方法(由OP建议)是使用sklearn.preprocessing.normalize
,其产生的结果略有不同-
Another option for performing normalisation (as suggested by OP) is using sklearn.preprocessing.normalize
, which yields slightly different results -
from sklearn.preprocessing import normalize
foo[:, [-1]] = normalize(foo[:, -1, None], norm='max', axis=0)
foo
array([[ 0. , 0.2378106 ],
[ 0.13216 , 0.28818769],
[ 0.25379 , 1. ],
[ 0.30874 , 0.31195614]])
这篇关于NumPy数组的最小-最大归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!