NumPy 数组的最小-最大归一化 [英] Min-max normalisation of a NumPy array

查看:38
本文介绍了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]]

推荐答案

参考此交叉验证链接,如何将数据归一化为 0-1 范围?,看来你可以对 foo 的最后一列进行 min-max 归一化.

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 数组的最小-最大归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆