简体   繁体   English

如何使用 MinMaxScaler 规范化 np.ndarray?

[英]How to normalize np.ndarray using MinMaxScaler?

I have a type of class 'numpy.ndarray' , how to normalize this array between 0 and 1?我有一种类型class 'numpy.ndarray' ,如何在 0 和 1 之间规范化这个数组? The array look like [-78.932495 -77.14235 -76.68105... -70.57554 -70.66422 -71.883995]数组看起来像[-78.932495 -77.14235 -76.68105... -70.57554 -70.66422 -71.883995]

I have an example is like我有一个例子就像

an_array = np.random.rand(10)*10
print(an_array)
OUTPUT
[5.48813504 7.15189366 6.02763376 5.44883183 4.23654799 6.45894113
 4.37587211 8.91773001 9.63662761 3.83441519]

norm = np.linalg.norm(an_array)
normal_array = an_array/norm
print(normal_array)

But it's for normal normalization, not for MinMaxScaler method.但它是用于正常标准化,而不是用于 MinMaxScaler 方法。 Then how to change to MinMaxScaler?那么怎么改成MinMaxScaler呢? Thanks谢谢

Subtract the minimum value of the array and divide by the maximum value after subtraction:减去数组的最小值,再除以减法后的最大值:

>>> ar = np.random.rand(10) * 10
>>> ar
array([6.84588701, 2.34947227, 4.4539159 , 9.66962601, 8.47137966,
       1.77616721, 8.88426798, 7.38305141, 0.17302112, 1.34235451])
>>> ar -= ar.min()
>>> ar /= ar.max()
>>> ar
array([0.70265805, 0.22918203, 0.4507816 , 1.        , 0.87382371,
       0.16881255, 0.91730118, 0.75922189, 0.        , 0.12313173])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM