Answer
How do you normalize data to a 0-1 range?
The short answer
To rescale a value x into the range [0, 1], subtract the minimum of your data and divide by the range (max minus min): (x - min) / (max - min). The smallest value in the data becomes 0, the largest becomes 1, and every other value keeps its relative position between them. This is called min-max normalization, and it is a different technique from standardizing to z-scores.
The formula
Min-max normalization maps every value in a data set onto a fixed scale, usually 0 to 1, while preserving the order and relative spacing of the values. For a value x drawn from data with minimum min and maximum max, the rescaled value is (x - min) / (max - min).
Plugging in the minimum itself gives (min - min) / (max - min) = 0, and plugging in the maximum gives (max - min) / (max - min) = 1. Everything else lands proportionally in between, so a value that sits 30% of the way from the minimum to the maximum still sits at 0.3 after rescaling.
A worked example
Take five values with a minimum of -23.89 and a maximum of 7.5499067: -23.89, -10.2, -1.5, 5.6878, and 7.5499067. Applying the formula gives 0, 0.4354, 0.7122, 0.9408, and 1 (rounded to four decimals). The most negative value became 0, the largest became 1, and the others fell in order between them.
If you already know the minimum and maximum of the full data set, you can rescale a single new value directly without recomputing anything: with a minimum of -23.89 and a maximum of 7.5499067, the value 5.6878 rescales to about 0.9408, matching its position in the list above.
Scaling to a range other than 0-1
The same idea extends to any target range [a, b]: compute the 0-1 version first, then stretch it with a + (x - min) / (max - min) * (b - a). For the five values above, rescaling to [-1, 1] instead of [0, 1] gives -1, -0.1291, 0.4243, 0.8815, and 1.
When to use it, and when not to
- Use it when you need a bounded scale. Some methods, like neural networks with sigmoid activations or distance-based algorithms such as k-nearest neighbors, work better when every feature sits on the same fixed range.
- It is sensitive to outliers, because a single extreme value sets the minimum or maximum for everyone else. One far-out point can squeeze all the ordinary values into a narrow band near 0 or 1.
- Standardization (z-scores) is the usual alternative. Subtracting the mean and dividing by the standard deviation centers the data at 0 with unit spread, and a single outlier has less effect on the mean and standard deviation than on the min or max.
- Fit the range on training data only. In a predictive modeling pipeline, compute the minimum and maximum from the training set, then apply that same minimum and maximum to validation or new data. Recomputing them separately lets information about the new data leak into the scaling.
Common pitfalls
- Confusing normalization with standardization. Min-max scaling produces values bounded between two numbers; z-score standardization does not bound the values at all, since a new observation can fall outside the range seen in the original data.
- Rescaling categorical or ordinal codes as if they were continuous. Min-max scaling assumes the numbers represent a genuine continuous quantity; applying it to arbitrary category codes (like 1 for red and 2 for blue) does not make them meaningfully comparable.
- Forgetting that a future value can fall outside [0, 1]. If a new observation is smaller or larger than anything seen so far, its rescaled value will be below 0 or above 1, which is expected and not an error.
See it in R
This reproduces the worked example above, extends it to an arbitrary target range, and shows how a single large outlier affects min-max scaling compared with z-score standardization.
# A small, self-contained data set with an obvious min and max.
# (No random component here, so no seed is needed -- every number below
# is reproducible exactly as printed.)
x <- c(-23.89, -10.2, -1.5, 5.6878, 7.5499067)
min_max <- function(v, lo = min(v), hi = max(v)) {
(v - lo) / (hi - lo)
}
round(min_max(x), 4)
# The minimum maps to 0, the maximum maps to 1, and everything
# else falls in between in the same relative position.
# The single-value case from a known min and max, without recomputing them:
min_max(5.6878, lo = -23.89, hi = 7.5499067)
# Rescaling to an arbitrary range [a, b] instead of [0, 1]:
rescale <- function(v, a, b, lo = min(v), hi = max(v)) {
a + (v - lo) / (hi - lo) * (b - a)
}
round(rescale(x, a = -1, b = 1), 4)
# Min-max scaling is sensitive to outliers, because they set lo/hi.
# Standardization (z-scores) is less affected by a single extreme point:
y <- c(x, 500) # one outlier added
round(min_max(y), 4) # the outlier compresses everything else near 0
round(scale(y)[, 1], 4) # z-scores spread the non-outlier points out more
Adding the outlier of 500 compresses the original five values into the narrow band 0 to 0.06 under min-max scaling, while the z-scores for those same five values still spread from about -0.50 to -0.35, keeping their relative differences easier to see.
Related tools and guides
- Which statistical test should I use?
- The Data Audit Checklist to Run First
- Feature scaling (Wikipedia)
- Standard score (Wikipedia)
More answered questions
- What does kurtosis actually tell you about your data?
- Why does standard deviation square the differences?
Working with your own data?
General answers only go so far. Send us your situation and we'll reply by email within two business days.
Ask your questionWritten by AskStats with AI assistance. This is general information, not advice for your specific data or study. When the results matter, check your approach with a qualified statistician.