Concepts / Binning Continuous Features

Binning Continuous Features

A single x-axis feature gives a binned linear model only one shared slope, even when each bin has its own offset.

  • Programming

A Straight Line Through a Bent Curve

Suppose you want a linear model to predict a target from a continuous x value, but the relationship between x and the target is not one straight line. It bends, rising steeply in some stretches and flattening or falling in others. One common response is to cut the x-axis into segments, called bins, and let the model treat each segment differently. This article follows that idea step by step. You will see how binning gives each segment its own offset, why that still leaves the model with a single slope, and how interaction features give every bin its own slope as well.

What do you think happens?

If you give a linear model one offset per bin, do you also get one slope per bin?

  • Yes, each bin gets its own slope automatically

  • No, the model still has one shared slope

Reveal answer
Answer:

No, the model still has one shared slope.

Binned input adds a separate offset for each bin, but the model still has only one x-axis feature, and that feature has only one slope. The same slope is applied to every bin.

Mapping Continuous Values to Bins

Binning replaces a continuous x value with the identity of the segment it falls into. The bin edges partition the x-axis into non-overlapping ranges, and every value lands in exactly one range. For the small worked dataset used later in this article, there are two bins. Bin 0 holds points with x below 2.0, and bin 1 holds points with x at or above 2.0. Because each point belongs to exactly one bin, a point is always in one bin and never in two.

Bin 0x below 2.0x = 1.0bin 0Bin 1x at or above 2.0x = 1.5bin 0x = 2.5bin 1x = 3.5bin 1falls infalls infalls infalls in
Which bin does each x value fall into, and how do the bin edges split the x-axis into segments? Bin edge at 2.0 separates bin 0 from bin 1.

Read the diagram as a lookup. Each x value points to exactly one bin, and the bin edge at 2.0 is the boundary between the two segments. Once every point has a bin label, you can represent that label as a set of indicator columns, one column per bin, where the column for the point's bin holds 1 and every other column holds 0.

Where a Single Slope Breaks Down

A model that receives only binned input can shift its prediction up or down from one bin to the next, because each bin receives its own offset. The model still has only one x-axis feature, though, and that feature has only one slope. Every bin's line therefore rises or falls at the same rate. The lines for different bins can start at different heights, but they are parallel. The downward slope is applied everywhere, regardless of which bin a point sits in.

Bin 0 lineown offset, shared slopeBin 0 lineown offset, own slopeBin 1 lineown offset, shared slopeBin 1 lineown offset, own slope
How does a binned model with one shared slope differ from one where each bin has its own slope, and why does the shared slope force every bin's line to be parallel?

Building the Product Feature

An interaction feature combines two other features so the model can respond to their combination. Here the two features are the bin indicator, which records which bin a data point is in, and the original x value. Their product is the feature that gives each bin its own slope. The product is the original x value when the point is inside a bin and zero everywhere else. Because it is zero outside its bin, the linear model can use it only to adjust predictions for points in that bin.

x = 2.5original x valueBin indicators0, 1Feature row0, 1, 0.0, 2.5Product with x0.0, 2.5multiplymultiplystackstack
For the point x = 2.5, how do the bin indicator and x combine into new feature columns, and which column is nonzero only when the point falls in a specific bin?

The construction in the source is a single line that multiplies the x column by the bin indicator columns and stacks the result beside the indicators. The product multiplies the one x column against each indicator column, producing one product column per bin. The stacking step then joins the indicator columns and the product columns side by side.

python
Notes on the code
  • X_binned holds one indicator column per bin
  • X * X_binned multiplies the single x column against each indicator column, giving one product column per bin
  • np.hstack joins the indicator columns and the product columns side by side

The source dataset has 10 bins and 100 data points. That gives 10 indicator features and 10 product features, a total of 20 features, so the shape is (100, 20). The number of columns is always twice the number of bins in this construction.

The vectorized version hides every value, so it helps to trace the same idea with plain Python lists on four points and two bins. Bin 0 holds x below 2.0 and bin 1 holds x at or above 2.0. Each row has two indicator values followed by two product values.

python
Output (expected)
[1, 0, 1.0, 0.0]
[1, 0, 1.5, 0.0]
[0, 1, 0.0, 2.5]
[0, 1, 0.0, 3.5]
Notes on the code
  • Each indicator is 1 for the bin the point belongs to and 0 otherwise
  • Each product is x multiplied by its indicator, so it is zero outside the point's bin
xBinIndicator bin 0Indicator bin 1Product bin 0Product bin 1
1.00101.00.0
1.50101.50.0
2.51010.02.5
3.51010.03.5

Feature rows for four points and two bins

Follow the row for x = 2.5. Its indicator columns are 0 and 1, which mark it as belonging to bin 1. Its first product column is 0.0, because the point is outside bin 0. Its second product column is 2.5, the original x value, because the point is inside bin 1. This is the zero-outside-the-bin behavior in concrete form.

Reading Slope and Offset from the Weights

When the linear model is fitted on the product representation, every bin gets its own offset and its own slope. The weight on a bin's indicator column acts as that bin's offset, and the weight on that bin's product column acts as its slope. Because a product column is zero for every point outside its bin, its weight only affects predictions inside that bin. The indicator and product weights of each bin together define an independent line for that bin.

Bin 0 indicatorweight1.0 is the offset of bin 0Bin 0 product weight-0.5 is the slope of bin 0Bin 1 indicatorweight3.0 is the offset of bin 1Bin 1 product weight0.25 is the slope of bin 1
How do the bin offset coefficients and the interaction coefficients add together to give the slope and intercept for each bin?
Rule

Worked example

Predicting Inside Each Bin

Using the generated weights above, with bin 0 offset 1.0, bin 0 slope -0.5, bin 1 offset 3.0, and bin 1 slope 0.25, predict the output for x = 1.5 in bin 0 and for x = 2.5 in bin 1.

  1. 1
    Identify the bin for x = 1.5

    1.5 is below 2.0, so it belongs to bin 0. Only bin 0's indicator is 1.

  2. 2
    Apply bin 0's line

    The prediction is the offset plus the slope times x: 1.0 + (-0.5)(1.5) = 1.0 - 0.75 = 0.25.

  3. 3
    Identify the bin for x = 2.5

    2.5 is at or above 2.0, so it belongs to bin 1.

  4. 4
    Apply bin 1's line

    The prediction is 3.0 + (0.25)(2.5) = 3.0 + 0.625 = 3.625.

Result

The prediction is 0.25 for x = 1.5 and 3.625 for x = 2.5. The two bins use different slopes, so their lines are no longer parallel.

Notice what the worked example shows. Bin 0 falls as x rises, while bin 1 rises. A single shared slope could not produce that pattern, because it would force both bins to move in the same direction at the same rate.

Mistakes Learners Make with Interaction Features

  • Common mistake

    Assuming bin offsets also create bin slopes

    What it looks like

    Adding only the indicator columns and fitting a model

    Why it fails

    Indicators change the height of each bin's line but leave the x feature with only one slope.

    Do this instead

    Add the product columns, X * X_binned, so each bin gets its own slope.

  • Common mistake

    Using the raw x value without masking it to its bin

    What it looks like

    Stacking the single x column next to the indicators

    Why it fails

    Every point then gets the same slope, so the model still has one shared slope.

    Do this instead

    Multiply x by each indicator column so the product is zero outside the bin.

  • Common mistake

    Reading the product weight as the bin's intercept

    Why it fails

    The product weight multiplies x, so it controls how the output changes as x changes. The indicator weight is the offset.

    Do this instead

    Read the indicator weight as the offset and the product weight as the slope.

  • Common mistake

    Forgetting that a point belongs to exactly one bin, so its indicator row should contain exactly one 1

Checking Your Understanding

MEDIUM

Bin edges at x = 1.0 and x = 3.0 split the x-axis into three bins: bin 0 for x below 1.0, bin 1 for x from 1.0 up to but not including 3.0, and bin 2 for x at or above 3.0. For the point x = 3.2, write its three indicator values and its three product values. Which product column is the only nonzero one?

Hints
  • Find which bin range contains 3.2 first
  • Only the indicator for that bin is 1, and the product for that bin is x times 1
  • The other two product columns must be 0.0

Key Takeaways

  • A single x-axis feature gives a binned linear model only one shared slope, even though each bin receives its own offset.
  • A shared slope forces every bin's line to be parallel, so it cannot follow a relationship whose rate of change differs between bins.
  • A product feature is the original x value inside its bin and zero everywhere else, built by multiplying the bin indicators by x and stacking the results with the indicators.
  • In the fitted model, a bin's indicator weight acts as its offset and its product weight acts as its slope, giving each bin an independent line.

Related concepts

More concepts starting with B