Open In App

How to normalize an array in NumPy in Python?

Last Updated : 16 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

Vector_formula
Normalization formula

Example 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.


Next Article
Article Tags :
Practice Tags :

Similar Reads