JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Normalizing a list of numbers in Python

Author:JIYIK Last Updated:2025/05/09 Views:

Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learning algorithms perform better when the data set has smaller values.

Suppose we have a list {1,2,3}, after normalizing to scale of 0 and 1, the list will be {0, 0.5, 1}. We can also normalize our data to some other scale. Suppose between 2 and 6. So, if we have the list {1,2,3} after normalization, it will be {2,4,6}.


Normalization formula

Understand how normalization works. We will look at its formula. We subtract the minimum value from each number and then divide it by the range ie: max-min. So, in the output, we get the normalized value of that particular number.

There are two methods we can use to normalize a list. We can use the inbuilt function which sklearnis available in the preprocessing module of the package. Or we can formulate the logic for it with the same formula as discussed above.


Normalize a list of numbers using the function sklearnin PythonMinMaxScaler

sklearnpreprocessingA built-in method called is available in the module of the package MinMaxScaler(). We will create a one-dimensional NumPyarray and pass it to the function. We have to install the NumPyand sklearnpackages to use this function.

Sample 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 the range of our normalization. To do this, we'll MinMaxScaler()specify ours in range. By default, if we don't specify a range, it will be 0 and 1. But we can define our scaling range. In this example, we specify the range 0 and 3.

Sample 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]]

Manually normalizing a list of numbers in Python

We can also normalize a list of numbers manually by writing a complete code for it, which uses the same formula as the normalization discussed above.

Sample 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] = (x - xmin) / (xmax - xmin)
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]

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

Get the index of the maximum and minimum values in a list in Python

Publish Date:2025/05/09 Views:117 Category:Python

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi

List of numbers from 1 to N in Python

Publish Date:2025/05/09 Views:116 Category:Python

This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until

Convert List to Pandas DataFrame in Python

Publish Date:2025/05/09 Views:133 Category:Python

This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i

Sorting a list by another list in Python

Publish Date:2025/05/09 Views:148 Category:Python

Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values ​​in another list in this art

How to Find the Maximum Value in a List in Python

Publish Date:2025/05/09 Views:99 Category:Python

This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding

Convert a list to a set in Python

Publish Date:2025/05/08 Views:145 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial