How to normalize an array in NumPy in Python?
Last Updated :
21 Nov, 2022
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].
Now,
Array [1,2,3] -> [3,5,7]
and
Array [1,2,4] -> [3,4.3,7]
Let's see examples with code
Example 1:
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize(arr, t_min, t_max):
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
# gives range starting from 1 and ending at 3
array_1d = np.arange(1,4)
range_to_normalize = (0,1)
normalized_array_1d = normalize(array_1d,
range_to_normalize[0],
range_to_normalize[1])
# display original and normalized array
print("Original Array = ",array_1d)
print("Normalized Array = ",normalized_array_1d)
Output:

Example 2:
Now, Lets input array is [1,2,4,8,10,15] and range is again [0,1]
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize(arr, t_min, t_max):
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
# assign array and range
array_1d = [1, 2, 4, 8, 10, 15]
range_to_normalize = (0, 1)
normalized_array_1d = normalize(
array_1d, range_to_normalize[0],
range_to_normalize[1])
# display original and normalized array
print("Original Array = ", array_1d)
print("Normalized Array = ", normalized_array_1d)
Output:

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:
Example 1:
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize_2d(matrix):
norm = np.linalg.norm(matrix)
matrix = matrix/norm # normalized matrix
return matrix
# gives and array starting from -2
# and ending at 13
array = np.arange(16) - 2
# converts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
Output:

Example 2:
We can also use other norms like 1-norm or 2-norm
Python3
# import module
import numpy as np
def normalize_2d(matrix):
# Only this is changed to use 2-norm put 2 instead of 1
norm = np.linalg.norm(matrix, 1)
# normalized matrix
matrix = matrix/norm
return matrix
# gives and array starting from -2 and ending at 13
array = np.arange(16) - 2
# converts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
Output:

In this way, we can perform normalization with NumPy in python.
Similar Reads
Convert Python List to numpy Arrays
NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
numpy.asarray_chkfinite() in Python
numpy.asarray_chkfinite() function is used when we want to convert the input to an array, checking for NaNs (Not A Number) or Infs(Infinities). Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asarray_chkfinite(arr, dtype=None, ord
2 min read
How to create a constant matrix in Python with NumPy?
A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th
4 min read
Combining a one and a two-dimensional NumPy Array
Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer(), which provides this facility. Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0) Example 1
2 min read
How to Swap Two Rows in a NumPy Array
One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a N
4 min read
How to Normalize Data Using scikit-learn in Python
Data normalization is a crucial preprocessing step in machine learning. It ensures that features contribute equally to the model by scaling them to a common range. This process helps in improving the convergence of gradient-based optimization algorithms and makes the model training process more effi
4 min read
Normalize an Image in OpenCV Python
Normalization involves adjusting the range of pixel intensity values in an image. Normalization can be beneficial for various purposes, such as improving the contrast or making the image more suitable for processing by other algorithms. In this article, we will explore how to normalize images using
2 min read
Describe a NumPy Array in Python
NumPy is a Python library used for numerical computing. It offers robust multidimensional arrays as a Python object along with a variety of mathematical functions. In this article, we will go through all the essential NumPy functions used in the descriptive analysis of an array. Let's start by initi
3 min read
numpy.nan_to_num() in Python
numpy.nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array. It returns (positive) infinity with a very large number and negative infinity with a very small (or negative) number. Syntax : numpy.nan_to_num(arr, copy=True) Parameters
2 min read
numpy.asarray() in Python
numpy.asarray()function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays. Syntax : numpy.asarray(arr, dtype=None, order=None) Parameters : arr : [array_like] Input data, in any form that can be converted to a
2 min read