How to normalize a NumPy array to within a certain range?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To normalize a NumPy array into a chosen range, the usual technique is min-max scaling. You shift the data so the original minimum becomes the lower bound, then rescale the spread so the original maximum becomes the upper bound.
The Standard Min-Max Formula
If the array has minimum old_min and maximum old_max, and you want a new range from new_min to new_max, the formula is:
new_min + (x - old_min) * (new_max - new_min) / (old_max - old_min)
In NumPy, that becomes straightforward code:
This maps the input to the interval [0, 1], but the same formula works for any numeric destination range.
Normalize to Another Range
For example, to scale the same array into [-1, 1]:
Only the target bounds change. The scaling logic stays the same.
Handle Constant Arrays Explicitly
If every value in the array is the same, then old_max - old_min equals zero and the usual formula divides by zero. You should handle that case directly:
There is no single universally correct output for a constant array, but returning the lower bound or the midpoint is much better than allowing a divide-by-zero warning.
Normalize Along an Axis
Sometimes you do not want to scale the entire array globally. In machine learning, it is common to normalize each feature column independently.
Axis-aware scaling matters because global normalization can mix unrelated feature ranges together.
When Min-Max Scaling Is Not the Best Choice
Min-max normalization is useful for bounded inputs, visualization, and models that benefit from fixed numeric ranges. But it is sensitive to outliers. If a few extreme values dominate the range, most other values may get compressed into a narrow band.
In those cases, standardization or robust scaling may be more appropriate. So the right question is not only "how do I normalize" but also "is min-max normalization the right preprocessing step for this data."
Common Pitfalls
- Forgetting the constant-array case and dividing by zero.
- Normalizing the whole matrix globally when each feature should be scaled separately.
- Letting extreme outliers determine the scale for the entire dataset.
- Using different min and max values for training and inference by accident.
- Assuming min-max scaling is always the right preprocessing choice.
Summary
- Min-max scaling maps an array from its original range into a chosen target range.
- The same formula works for
[0, 1],[-1, 1], or any other interval. - Constant arrays need explicit handling because the denominator becomes zero.
- Feature-wise normalization often matters more than global normalization.
- Pick min-max scaling because it suits the data and model, not just because it is easy to code.
Related reading
- how to normalize input data for models in tensorflow
- How to normalize the Train and Test data using MinMaxScaler sklearn
- How to obtain features' weights
- How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory?
- How to pass another entire column as argument to pandas fillna
- How to pass in multidimensional data to xgboost model
- How to obtain the index permutation after the sorting
- How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.