Answer

How do you normalize data to a 0-1 range?

Inspired by a question on Cross Validated ·

normalizationdescriptive statistics

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

Common pitfalls

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

More answered questions

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 question

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