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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read