Skip to content

How to Normalize a List of Numbers in Python

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,,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)
Code language: Python (python)

Option 2 – Using MinMaxScaler from sklearn

import numpy as np from sklearn import preprocessing list = np.array([6,1,,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)
Code language: Python (python)

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

import numpy as np from sklearn import preprocessing list = np.array([6,1,,2,7,3,8,1,5]).reshape(-1,1) print('Original List:',list) scaler = preprocessing.MinMaxScaler(feature_range=(, 3)) normalizedlist=scaler.fit_transform(list) print('Normalized List:',normalizedlist)
Code language: Python (python)

See also  How to Find the Longest Substring in Alphabetical Order in Python
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x