Concepts / Rescaling and Shifting Data

Rescaling and Shifting Data

Preprocessing and scaling adjust the data representation so that it becomes more suitable for certain algorithms.

  • Programming

A Dataset With Two Very Different Scales

Imagine a dataset where one feature is a person's age, with values near 20 to 40, and another feature is an income, with values in the tens of thousands. Both numbers are perfectly valid, but they live on very different scales. This article answers two questions: what can be done about that difference, and why it matters for some algorithms more than others.

What do you think happens?

Suppose a learning algorithm measures how far apart two people are by combining their age and their income. Which feature will mostly decide the distance between two rows?

  • Age, because it is the first feature

  • Income, because its raw values are much larger

  • Both equally, because both are valid numbers

Reveal answer
Answer:

Income, because its raw values are much larger

When one feature ranges over thousands while another ranges over a few tens, the raw magnitudes can drive the algorithm's behavior, rather than the patterns in the data.

Why Some Algorithms Are Sensitive to Raw Magnitudes

The reason this adjustment is needed comes from how some algorithms behave. Neural networks and support vector machines are very sensitive to the scaling of the data. If one feature ranges over thousands while another ranges over fractions, the algorithm can be affected by those raw magnitudes rather than by the patterns the data contains. A common response is to adjust the features so that the data representation fits these algorithms better.

Age minimum20Income minimum30000Age maximum40Income maximum90000Age range20Income range60000subtractsubtractsubtractsubtract
How the raw ranges of age and income compare before any adjustment

The two columns above differ by a factor of three in range, and the income values are in the thousands while the age values are in the twenties. Neither feature is wrong. The problem is only that the algorithm sees their raw sizes directly, so the representation needs to change before the algorithm receives it.

Shifting and Rescaling One Feature

The adjustment described in the source is often a simple per-feature rescaling and shift. Shifting moves all of a feature's values by the same amount. Rescaling stretches or compresses them by a chosen factor. Per-feature means that each feature is handled on its own: the feature's own values determine how it is shifted and how it is scaled. A different feature in the same dataset can receive a completely different shift and scale.

Formula

The formula above is one concrete way to perform a per-feature shift and rescale. The source illustrates the general idea of shifting and scaling each feature rather than prescribing this exact recipe. Here, x is an original value in one feature, min(x) is the smallest value in that feature, and max(x) is the largest. The subtraction is the shift, which moves the minimum to zero. The division by the range is the rescaling, which makes the largest value end up at one.

Tracing the Age Feature Step by Step

Worked example

Rescaling the age feature

The age feature holds the values 20, 30, and 40. Apply the per-feature shift and rescale and find the new value of each age.

  1. 1
    Find the minimum

    The smallest age is 20, so the shift is subtracting 20 from every value.

  2. 2
    Shift

    Subtract 20 from each value: 20 becomes 0, 30 becomes 10, and 40 becomes 20. The minimum is now zero.

  3. 3
    Find the range

    The range is the largest value minus the smallest: 40 minus 20 gives 20.

  4. 4
    Rescale

    Divide each shifted value by 20: 0 becomes 0.0, 10 becomes 0.5, and 20 becomes 1.0. The largest value now sits at one.

Result

The ages 20, 30, and 40 become 0.0, 0.5, and 1.0. The gap between 20 and 30 is still half of the total spread of ages.

Age 20minimumAdjusted 0.0new minimumAge 30middleAdjusted 0.5new middleAge 40maximumAdjusted 1.0new maximumshift and rescaleshift and rescaleshift and rescale
Each original age maps to a new value on the shared zero-to-one range; the minimum moves to zero and the maximum moves to one

Notice that the transformation changed the numbers but not their order or their relative positions. The minimum moved to zero, the maximum moved to one, and the middle value landed exactly halfway between them, because 30 sat halfway between 20 and 40.

Tracing Both Features Through One Pass

Now apply the same procedure to the income feature, which holds 30000, 45000, and 90000. Each feature receives its own shift and its own scale, so the income feature is handled independently of age.

Original valueShifted (minus feature minimum)Rescaled (divided by feature range)
Age 2000.0
Age 30100.5
Age 40201.0
Income 3000000.0
Income 45000150000.25
Income 90000600001.0

Each feature is shifted by its own minimum and divided by its own range

Notice what happened. Both features now sit on the same range from zero to one, even though their raw magnitudes were very different before. The relative spacing inside each feature is preserved: the gap between 30000 and 45000 is still a quarter of the total spread of incomes, just as the gap between 20 and 30 is still half the total spread of ages.

Keeping the Recipe Per Feature

Because the adjustment is per feature, each column of the dataset has its own shift and its own scale. The age column was shifted by 20 and divided by 20. The income column was shifted by 30000 and divided by 60000. Mixing these up is an easy error, because the numbers look like they belong to one process when they belong to two separate features.

Where Scaling Sits in a Learning Pipeline

It helps to see scaling as one stage between the raw data and the algorithm. The raw feature values come in first. Each feature receives its shift, and its scale is applied. The adjusted values are what the algorithm finally receives. The algorithm never needs to know how the adjustment was done; it simply works with the representation it is given, which is the purpose of the adjustment in the first place.

Raw feature valuesage and incomeScalershift and rescale,unsupervisedAdjusted featuresvalues between 0 and 1Supervised modellearns from features andlabelsTarget labelssupervised informationfeature values onlytransformfeatureslabels
The scaler uses only feature values, while the supervised model also receives the target labels

Why Scaling Counts as Unsupervised

Scaling is unsupervised because it does not use supervised information such as target labels. The shift and scale for a feature are computed from that feature's values alone. The labels never enter the calculation. This remains true even when the scaled features are fed into a supervised model, as the pipeline above shows. The scaler sits in front of the supervised model and touches only the feature values, while the labels are used only by the model.

Mistakes When Preprocessing Features

  • Common mistake

    Assuming every algorithm needs scaled features

    Why it fails

    The source highlights neural networks and support vector machines as very sensitive to scaling. The need for adjustment depends on the algorithm, so it should not be applied as a blanket rule.

    Do this instead

    Check how the chosen algorithm responds to feature magnitudes before deciding that adjustment is needed.

  • Common mistake

    Using one shift and scale for all features

    What it looks like

    Subtracting 20 and dividing by 20 from the income column as well as the age column.

    Why it fails

    Each feature is handled using its own values, so a single recipe applied to every column ignores the different ranges of the features.

    Do this instead

    Compute the minimum and range separately for each feature and apply each feature's own shift and scale.

  • Common mistake

    Shifting without rescaling, or rescaling without shifting

    Why it fails

    Shifting only moves values and does not change their spread, while rescaling only stretches or compresses them. Together they bring the feature onto a common range.

    Do this instead

    Apply both the shift that moves the minimum and the scale that sets the range.

  • Common mistake

    Believing scaling needs the target labels

    Why it fails

    Scaling is unsupervised because it does not use target labels, even when it is part of a supervised workflow.

    Do this instead

    Compute the scaling parameters from the feature values alone, and let the labels be used only by the supervised model.

  • Common mistake

    Losing track of which adjusted values belong to which original feature after the transformation

Applying the Recipe to New Values

EASY

A feature holds the values 5, 10, and 25. Using the per-feature shift and rescale, find the new value for each of the three original values. Then state which original value becomes the new minimum and which becomes the new maximum.

Hints
  • The minimum is 5, so subtract 5 from every value first.
  • The range is 25 minus 5, which is 20. Divide each shifted value by 20.
  • After shifting, the smallest value should be zero and the largest should equal the range.

Key Takeaways

  • Some algorithms, including neural networks and support vector machines, are very sensitive to the scale of the data, so their raw magnitudes can affect results more than the underlying patterns.
  • A common adjustment is a per-feature rescaling and shift: each feature is shifted by its own values and then scaled by its own range, putting features on a comparable range while preserving relative spacing within each feature.
  • Each feature receives its own shift and scale, and the same transformation must be applied consistently to every value of that feature.
  • Scaling is unsupervised because it uses only feature values and never target labels, even when it is placed in front of a supervised model.

Related concepts

More concepts starting with R