Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scale and print an array based on its minimum and maximum value?

I’m trying to scale the following NumPy array based on its minimum and maximum values.

array = [[17405.051 17442.4   17199.6   17245.65 ]
 [17094.949 17291.75  17091.15  17222.75 ]
 [17289.    17294.9   17076.551 17153.   ]
 [17181.85  17235.1   17003.9   17222.   ]]

Formula used is:
m=(x-xmin)/(xmax-xmin)

wherein m is an individually scaled item, x is an individual item, xmax is the highest value and xmin is the smallest value of the array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question is how do I print the scaled array?

P.S. – I can’t use MinMaxScaler as I need to scale a given number (outside the array) by plugging it in the mentioned formula with xmin & xmax of the given array.

I tried scaling the individual items by iterating over the array but I’m unable to put together the scaled array.

I’m new to NumPy, any suggestions would be welcome.
Thank you.

>Solution :

Use method ndarray.min(), ndarray.max() or ndarray.ptp()(gets the range of the values in the array):

>>> ar =  np.array([[17405.051, 17442.4,   17199.6,   17245.65 ],
...  [17094.949, 17291.75,  17091.15,  17222.75 ],
...  [17289.,    17294.9,   17076.551, 17153.   ],
...  [17181.85,  17235.1,   17003.9,   17222.   ]])
>>> min_val = ar.min()
>>> range_val = ar.ptp()
>>> (ar - min_val) / range_val
array([[0.91482554, 1.        , 0.44629418, 0.55131129],
       [0.2076374 , 0.65644242, 0.19897377, 0.4990878 ],
       [0.65017104, 0.663626  , 0.16568073, 0.34002281],
       [0.40581528, 0.527252  , 0.        , 0.49737742]])

I think you should learn more about the basic operation of numpy.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading