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

607 views Asked by At

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.

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.

2

There are 2 answers

0
Mechanic Pig On BEST ANSWER

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.

0
Student.py On
import numpy as np

array_list = [[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.   ]]

# Convert list into numpy array
array = np.array(array_list)

# Create empty list
scaled_array_list=[]

for x in array:
    m = (x - np.min(array))/(np.max(array)-np.min(array))
    scaled_array_list.append(m)

# Convert list into numpy array
scaled_array = np.array(scaled_array_list)
    
scaled_array

My version is by iterating over the array as you said.

You can also put everything in a function and use it in future:

def scaler(array_to_scale):
    # Create empty list
    scaled_array_list=[]

    for x in array:
        m = (x - np.min(array))/(np.max(array)-np.min(array))
        scaled_array_list.append(m)

    # Convert list into numpy array
    scaled_array = np.array(scaled_array_list)
    
    return scaled_array

# Here it is our input
array_list = [[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.   ]]

# Convert list into numpy array
array = np.array(array_list)

scaler(array)

Output:

Out: 
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]])