Normalize a List of Numbers in Python
 the Formula for Normalization

Normalize a List of Numbers Using the
MinMaxScaler
Function in Pythonsklearn
 Normalize a List of Numbers Manually in Python
Normalization means converting a given data into another scale. We rescale data in such a way that it falls between two values. Most of the time data is rescaled between 0 and 1. We rescale the data for a different purpose. For example, machine learning algorithms perform better when the dataset values are small.
Let’s say we have a list {1,2,3} and after the normalization of the scale of 0 and 1, the list will be {0, 0.5, 1}
. We can also normalize our data to some other scale as well. Let’s say between 2 and 6. So, again if we have the list {1,2,3} after normalization it will be {2,4,6}.
the Formula for Normalization
To understand how normalization works. We will have a look at its formula. We subtract the minimum value from every number and divide it by the range ie: maxmin. So, in output, we get the normalized value of that specific number.
We can use two methods to normalize a list. Either we can use the builtin function, which is available in the preprocessing module of the sklearn
package. Or we can make our logic for it which works on the same formula as discussed above.
Normalize a List of Numbers Using the MinMaxScaler
Function in Python sklearn
A builtin method named MinMaxScaler()
is available in preprocessing
module of the sklearn
package. We will create a 1D NumPy
array and pass it to the function. We must have NumPy
and sklearn
packages installed to use this function.
Example Code:
# python 3.x
import numpy as np
from sklearn import preprocessing
list = np.array([6,1,0,2,7,3,8,1,5]).reshape(1,1)
print('Original List:',list)
scaler = preprocessing.MinMaxScaler()
normalizedlist=scaler.fit_transform(list)
print('Normalized List:',normalizedlist)
Output:
Original List: [[6]
[1]
[0]
[2]
[7]
[3]
[8]
[1]
[5]]
Normalized List: [[0.75 ]
[0.125]
[0. ]
[0.25 ]
[0.875]
[0.375]
[1. ]
[0.125]
[0.625]]
Now let’s say we want to specify our range for normalization. For this purpose, we will specify our range
in MinMaxScaler()
. By default, if we don’t specify the range, it will be 0 and 1. But we can define our range for scaling. In this example, we specify the range 0 and 3.
Example Code:
# python 3.x
import numpy as np
from sklearn import preprocessing
list = np.array([6,1,0,2,7,3,8,1,5]).reshape(1,1)
print('Original List:',list)
scaler = preprocessing.MinMaxScaler(feature_range=(0, 3))
normalizedlist=scaler.fit_transform(list)
print('Normalized List:',normalizedlist)
Output:
Original List: [[6]
[1]
[0]
[2]
[7]
[3]
[8]
[1]
[5]]
Normalized List: [[2.25 ]
[0.375]
[0. ]
[0.75 ]
[2.625]
[1.125]
[3. ]
[0.375]
[1.875]]
Normalize a List of Numbers Manually in Python
We can also normalize the list of numbers manually by writing the complete code for it, which works on the same formula as discussed above for normalization.
Example Code:
list = [6,1,0,2,7,3,8,1,5]
print('Original List:',list)
xmin = min(list)
xmax=max(list)
for i, x in enumerate(list):
list[i] = (xxmin) / (xmaxxmin)
print('Normalized List:',list)
Output:
Original List: [6, 1, 0, 2, 7, 3, 8, 1, 5]
Normalized List: [0.75, 0.125, 0.0, 0.25, 0.875, 0.375, 1.0, 0.125, 0.625]
I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.
LinkedIn