How to normalize an NumPy array so the values range exactly between 0 and 1?
Last Updated :
25 Jul, 2022
In this article, we will cover how to normalize a NumPy array so the values range exactly between 0 and 1.
Normalization is done on the data to transform the data to appear on the same scale across all the records. After normalization, The minimum value in the data will be normalized to 0 and the maximum value is normalized to 1. All the other values will range from 0 to 1. Normalization is necessary for the data represented in different scales. Because Machine Learning models may get over-influenced by the parameter with higher values. There are different ways to normalize the data. One of the standard procedures is the min-max value approach.
Normalization using Min Max Values
Here normalization of data can be done by subtracting the data with the minimum value in the data and dividing the result by the difference between the maximum value and the minimum value in the given data. we will look into more deep to the code for a better understanding.
Example
The maximum value and minimum value in a NumPy array can be determined by the min() and max(). The formula for normalization using min-max values is given below
Normalized data= ( data- min(data) )/( max(data)-min(data) )
Python3
# import necessary packages
import numpy as np
# create an array
data = np.array([[10, 20], [30, 40],
[5, 15], [0, 10]])
normalizedData = (data-np.min(data))/(np.max(data)-np.min(data))
# normalized data using min max value
print(normalizedData)
Output
[[0.25 0.5 ]
[0.75 1. ]
[0.125 0.375]
[0. 0.25 ]]
There are other ways too to normalize the data. They are:
- Normalization using sklearn MinMaxScaler
- Normalization using numpy.linalg.norm
- Normalization using Maths formula
Normalization using sklearn MinMaxScaler
In Python, sklearn module provides an object called MinMaxScaler that normalizes the given data using minimum and maximum values. Here fit_tranform method scales the data between 0 and 1 using the MinMaxScaler object.
Python3
# import necessary packages
import numpy as np
from sklearn import preprocessing as p
# create an array
data = np.array([[10, 20], [30, 40],
[5, 15], [0, 10]])
min_max_scaler = p.MinMaxScaler()
normalizedData = min_max_scaler.fit_transform(data)
# normalized data using MinMaxScaler
print(normalizedData)
OutputÂ
[[0.33333333 0.33333333]
[1. 1. ]
[0.16666667 0.16666667]
[0. 0. ]]
Normalization using numpy.linalg.norm
The NumPy library provides a method called norm that returns one of eight different matrix norms or one of an infinite number of vector norms. It entirely depends on the ord parameter in the norm method. By default, the norm considers the Frobenius norm. The data here is normalized by dividing the given data with the returned norm by the norm method.
Python3
# import necessary packages
import numpy as np
# create an array
data = np.array([[10, 20], [30, 40],
[5, 15], [0, 10]])
normalizedData = data/np.linalg.norm(data)
# normalized data using linalg.norm
print(normalizedData)
Output
[[0.17277369 0.34554737]
[0.51832106 0.69109474]
[0.08638684 0.25916053]
[0. 0.17277369]]
Normalization using Maths Formula
Here the data is normalized by diving the data with the square root of the sum of squares of given data. In order to implement this, a simple NumPy library is required which provides square root and sum methods that help to reduce lines of code. Below is the implemented code to normalize the data using the sum of squares of data
Python3
# import necessary packages
import numpy as np
# create an array
data = np.array([[10, 20], [30, 40],
[5, 15], [0, 10]])
normalizedData = data/np.sqrt(np.sum(data**2))
# normalized data using sum of squares
print(normalizedData)
Output
[[0.17277369 0.34554737]
[0.51832106 0.69109474]
[0.08638684 0.25916053]
[0. 0.17277369]]
Similar Reads
How to Change a Single Value in a NumPy Array NumPy arrays are a fundamental data structure in Python, widely used for scientific computing and data analysis. They offer a powerful way to perform operations on large datasets efficiently. One common task when working with NumPy arrays is changing a single value within the array. This article wil
6 min read
How to generate a random number between 0 and 1 in Python ? The use of randomness is an important part of the configuration and evaluation of modern algorithms. Here, we will see the various approaches for generating random numbers between 0 ans 1. Method 1: Here, we will use uniform() method which returns the random number between the two specified numbers
1 min read
How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax
2 min read
How to create a NumPy 1D-array with equally spaced numbers in an interval? At times, we need to make arrays of different types like in AP(equally spaced series of numbers), GP(exponentially spaced series of numbers), or HP(reciprocally spaced series of numbers) to solve various problems, especially while solving some scientific or astronomical problem, to reduce the calcul
3 min read
How to remove NaN values from a given NumPy array? In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array. There are basically three approaches with sl
3 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read
How to normalize a tensor to 0 mean and 1 variance in Pytorch? A tensor in PyTorch is like a NumPy array with the difference that the tensors can utilize the power of GPU whereas arrays can't. To normalize a  tensor, we transform the tensor such that the mean and standard deviation become 0 and 1 respectively. As we know that the variance is the square of the s
3 min read
Find the nearest value and the index of NumPy Array In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Arra
3 min read
How to get the floor, ceiling and truncated values of the elements of a numpy array? In this article, let's discuss how to get the floor, ceiling, and truncated values of the elements of a Numpy array. First, we need to import the NumPy library to use all the functions available in it. This can be done with this import statement: import numpy as np Getting the floor value The greate
3 min read
How to Remove columns in Numpy array that contains non-numeric values? Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np.isnan() function. Example 1: Pyth
2 min read