How to Normalize a List of Numbers in Python

0 min read 99 words

If you need to normalize a list of numbers in Python, then you can do the following:

Option 1 – Using Native Python

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] = (x-xmin) / (xmax-xmin)
print('Normalized List:',list)

Option 2 – Using MinMaxScaler from sklearn

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)

You can also specify the range of the MinMaxScaler().

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)
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts