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.
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?
Reveal 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.
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.
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.
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.
- 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.hstackjoins 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.
[1, 0, 1.0, 0.0]
[1, 0, 1.5, 0.0]
[0, 1, 0.0, 2.5]
[0, 1, 0.0, 3.5]- 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
| x | Bin | Indicator bin 0 | Indicator bin 1 | Product bin 0 | Product bin 1 |
|---|---|---|---|---|---|
| 1.0 | 0 | 1 | 0 | 1.0 | 0.0 |
| 1.5 | 0 | 1 | 0 | 1.5 | 0.0 |
| 2.5 | 1 | 0 | 1 | 0.0 | 2.5 |
| 3.5 | 1 | 0 | 1 | 0.0 | 3.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.
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.
- 1Identify 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.
- 2Apply 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.
- 3Identify the bin for x = 2.5
2.5 is at or above 2.0, so it belongs to bin 1.
- 4Apply bin 1's line
The prediction is 3.0 + (0.25)(2.5) = 3.0 + 0.625 = 3.625.
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 likeAdding only the indicator columns and fitting a model
Why it failsIndicators change the height of each bin's line but leave the x feature with only one slope.
Do this insteadAdd 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 likeStacking the single x column next to the indicators
Why it failsEvery point then gets the same slope, so the model still has one shared slope.
Do this insteadMultiply 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 failsThe product weight multiplies x, so it controls how the output changes as x changes. The indicator weight is the offset.
Do this insteadRead 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
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.