How to normalize an array in NumPy in Python?
In this article, we are going to discuss how to normalize 1D and 2D arrays in Python using NumPy. Normalization refers to scaling values of an array to the desired range.
Normalization of 1D-Array
Suppose, we have an array = [1,2,3] and to normalize it in range [0,1] means that it will convert array [1,2,3] to [0, 0.5, 1] as 1, 2 and 3 are equidistant.
Array [1,2,4] -> [0, 0.3, 1]
This can also be done in a Range i.e. instead of [0,1], we will use [3,7].
Array [1,2,3] -> [3,5,7]
Array [1,2,4] -> [3,4.3,7]
Let’s see examples with code
Now, Lets input array is [1,2,4,8,10,15] and range is again [0,1]
Normalization of 2D-Array
To normalize a 2D-Array or matrix we need NumPy library. For matrix, general normalization is using The Euclidean norm or Frobenius norm.
The formula for Simple normalization is
Here, v is the matrix and |v| is the determinant or also called The Euclidean norm. v-cap is the normalized matrix.
Below are some examples to implement the above:
We can also use other norms like 1-norm or 2-norm
In this way, we can perform normalization with NumPy in python.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course