How to normalize np.ndarray using MinMaxScaler?

Viewed 14

I have a type of class 'numpy.ndarray', how to normalize this array between 0 and 1? The array look like [-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. Then how to change to MinMaxScaler? Thanks

1 Answers

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])
Related