How to normalize an array in NumPy in Python?
Last Updated :
16 Jun, 2025
Normalizing an array in NumPy refers to the process of scaling its values to a specific range, typically between 0 and 1. For example, an array like [1, 2, 4, 8, 10] can be normalized to [0.0, 0.125, 0.375, 0.875, 1.0], where the smallest value becomes 0, the largest becomes 1 and all other values are scaled proportionally in between. Let's explore different methods to perform this efficiently.
Using vectorized normalization
This method uses pure NumPy operations to scale all values in an array to a desired range, usually [0, 1]. It's fast, efficient and works well when you're handling normalization manually without external libraries. Formula:
Normalization formulaExample 1: This example normalizes a 1D list by converting it to a NumPy float array and scaling the values to the range [0, 1].
Python
import numpy as np
a = np.array([1, 2, 4, 8, 10], dtype=float)
res = (a - a.min()) / (a.max() - a.min())
print(res.tolist())
Output[0.0, 0.1111111111111111, 0.3333333333333333, 0.7777777777777778, 1.0]
Explanation: Array a is converted to a float type. Then, min-max normalization is applied using the formula (a - min) / (max - min). This scales all values to fall between 0 and 1.
Example 2: This example normalizes a 2D array by flattening it to 1D, applying min-max scaling, then reshaping it back.
Python
import numpy as np
a = np.array([[1, 2], [3, 6], [8, 10]], dtype=float)
f = a.flatten()
n = (f - f.min()) / (f.max() - f.min())
res = n.reshape(a.shape)
print(res.tolist())
Output[[0.0, 0.1111111111111111], [0.2222222222222222, 0.5555555555555556], [0.7777777777777778, 1.0]]
Explanation: 2D array is flattened to 1D, normalized using the same formula and then reshaped back to its original shape. This method normalizes the entire array as one distribution.
Using Sklearn MinMaxScaler
MinMaxScaler is part of sklearn.preprocessing and automates feature scaling. It rescales features to a specific range (default is [0, 1]), handling multiple features/columns efficiently.
Example 1: This example reshapes the 1D list for MinMaxScaler, which fits and transforms it to the range [0, 1].
Python
from sklearn.preprocessing import MinMaxScaler
import numpy as np
a = np.array([1, 2, 4, 8, 10, 15]).reshape(-1, 1)
s = MinMaxScaler()
res = scaler.fit_transform(a).flatten()
print(res.tolist())
Output
[0.0, 0.07142857142857142, 0.21428571428571427, 0.5, 0.6428571428571428, 1.0]
Explanation: 1D array is reshaped into a 2D column vector required by Scikit-learn. fit_transform() computes the min and max, then scales all values to [0, 1]. The result is flattened and returned as a list.
Example 2: This example normalizes each column of the 2D array independently using MinMaxScaler.
Python
from sklearn.preprocessing import MinMaxScaler
import numpy as np
a = np.array([[1, 2], [3, 6], [8, 10]])
s = MinMaxScaler()
res = scaler.fit_transform(a)
print(res.tolist())
Output
[[0.0, 0.0], [0.2857142857142857, 0.5], [1.0, 1.0]]
Explanation: 2D array a is scaled column-wise to the range [0, 1] using MinMaxScaler and fit_transform(), which normalizes each column based on its min and max. The result is converted to a list with .tolist() for readability.
Using Precomputed Min/Max
This method is similar to vectorized normalization, but the min and max values are calculated beforehand or known ahead of time. It’s especially useful in real-time systems or when consistent scaling is needed e.g., test data based on training data stats.
Example 1: This example normalizes a 1D list using explicitly calculated min and max values.
Python
import numpy as np
a = np.array([1, 2, 4, 8, 10, 15], dtype=float)
min_val, max_val = 1, 15
res = (a - min_val) / (max_val - min_val)
print(res.tolist())
Output[0.0, 0.07142857142857142, 0.21428571428571427, 0.5, 0.6428571428571429, 1.0]
Explanation: List a is converted to a NumPy float array, then min-max normalization is applied to scale values to the range (t_min, t_max) (default [0, 1]). The result is returned as a readable list using .tolist().
Example 2: This example flattens a 2D array, applies precomputed min-max normalization and reshapes it back.
Python
import numpy as np
a = np.array([[1, 2], [3, 6], [8, 10]], dtype=float)
min_val, max_val = a.min(), a.max()
res = (a - min_val) / (max_val - min_val)
print(res.tolist())
Output[[0.0, 0.1111111111111111], [0.2222222222222222, 0.5555555555555556], [0.7777777777777778, 1.0]]
Explanation: 2D NumPy array a is normalized directly using global min-max scaling. Values are scaled to [0, 1] based on a.min() and a.max() and the result is converted to a list with .tolist() for readability.
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 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 Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
2 min read