Concepts / Linear Regression on Continuous Features

Linear Regression on Continuous Features

Linear models can only model linear relationships, so with one feature their fitted form is a line.

  • Programming

Two Models, One Continuous Feature

Suppose you have a single continuous input feature, such as a value that can take any number within a range, and an output you want to predict from it. You fit two different kinds of regression model to the same training points: a linear regression model and a decision tree regressor. When you draw both predictions over the raw points, the two curves can look very different. This article explains why, and it shows how a change in the feature representation can change what a linear model is able to do.

EASY

Before reading the explanation, make a prediction. You are fitting a straight-line model and a decision tree to the same wavy set of training points with one input feature. Which of the two curves do you expect to follow the training points more closely, and why? Write down your guess and the reason for it, then keep reading to check your reasoning.

Hints
  • Think about what shapes each model is allowed to produce, not only how well it was trained.
  • Consider whether a straight line can bend to follow a wavy pattern.

What a Single Straight Line Can Express

A linear model is limited by its form. When there is a single input feature, the relationship between input and output is a line. A line has one slope and one intercept. No choice of those two numbers can make the line curve up and down to chase a wavy pattern. So the linear regression curve in a comparison is a straight line, even when the training points clearly bend.

This limitation belongs to the model family, not to the quality of the fit. A linear model that looks poor on wavy data is doing exactly what it was built to do. When you see a straight prediction line cutting through a curved cloud of points, the first explanation to consider is the model's form. Blaming the code or the data comes after that.

How a Decision Tree Follows Bends

A decision tree regressor does not have a fixed functional form like a line. Instead, it builds its prediction by making a sequence of splits on the input feature and assigning an output value to each region those splits create. Because the prediction is built region by region, it can follow bends and irregular patterns that a single straight line cannot. The source describes the tree as able to build a much more complex model of the data.

Linear formone straight lineParametersone slope and one interceptFit across xsame slope everywhereConstructionsequence of splits on xRegionsone output value per regionFit across xcan follow bendsdefined bylimitscreatesallows
How does a straight fitted line differ from the step-shaped fit a decision tree produces across the same range of x values?

The source is careful about one qualification. The tree's ability to capture complex patterns depends strongly on how the data is represented. The same tree can look powerful or weak depending on the feature representation it receives. So the real question in a comparison is not only which model is more capable, but how each model consumes the feature it is given.

Binning a Continuous Feature

Binning, also called discretization, splits one continuous feature into multiple features. The source presents it as one way to make a linear model more powerful on continuous data. You choose a set of ranges that cover the input values. Each range becomes a bin. Each data point is then described by which bin it falls into, using a set of indicator features that are 1 for the bin the value falls in and 0 for every other bin.

Worked example

Binning one value into four indicator features

A continuous feature x ranges from 0 to 8. Split it into four bins of equal width: 0 to 2, 2 to 4, 4 to 6, and 6 to 8. Show how the value x = 5.3 is represented after binning.

  1. 1
    Choose the bins

    The four bins are 0 to 2, 2 to 4, 4 to 6, and 6 to 8. Each bin will get its own indicator feature.

  2. 2
    Locate the value

    The value 5.3 is greater than 4 and less than 6, so it falls in the bin 4 to 6, which is the third bin.

  3. 3
    Set the indicators

    The indicator for the 4 to 6 bin becomes 1. The other three indicators become 0.

  4. 4
    Hand the new features to the model

    The linear model now receives the vector 0, 0, 1, 0 in place of the single number 5.3.

Result

The single continuous feature 5.3 becomes four binary features 0, 0, 1, 0. The raw value is no longer visible to the linear model; only the bin is.

x = 5.3raw continuous valuebin 0 to 20bin 2 to 40bin 4 to 61bin 6 to 80falls in
For a given x value, which bin does it fall into, and which of the new binary columns becomes 1 while the others become 0?

Notice what this representation does not contain. The original value 5.3 has been replaced by indicator features that record only which range it falls in. A value of 5.9 would produce the same indicator pattern, and so would any other value between 4 and 6. Binning trades fine detail inside each range for the ability to give each range its own weight.

Before and After the Binning Change

Before binning, the linear model sees one feature, x, and its prediction is a single line. After binning, the linear model sees several indicator features. Each derived feature can carry its own weight. The combined prediction can therefore rise and fall in different regions of the input range. The model is still linear in the features it receives, but the representation of the data has changed, and that change is what makes the fit different.

xone continuous featurebin 0 to 2its own weightbin 2 to 4its own weightbin 4 to 6its own weightbin 6 to 8its own weight
After one continuous feature is split into bin indicators, how does the fitted line change into a set of flat segments, one per bin?

In the after picture, each bin acts as a separate feature with its own weight. For any input, only one indicator is 1, so the prediction reduces to the weight of the active bin. Across the whole range this produces a set of flat segments, one per bin, instead of one slope across the entire input range. This is a step-shaped fit, similar in spirit to the regions a decision tree creates.

Mistakes When Reading Model Comparisons

  • Common mistake

    Treating a straight line that misses the bends as a broken fit

    What it looks like

    A linear regression line cuts through a wavy cloud of points and the learner assumes the training code failed.

    Why it fails

    A line has one slope and one intercept, so it cannot curve to follow a wavy pattern. The poor match reflects the model's form.

    Do this instead

    Check the shape the model family can express first, then look at the code and the data.

  • Common mistake

    Assuming the tree is always the more capable model

    What it looks like

    A learner concludes that a decision tree will always beat a linear model on any continuous feature.

    Why it fails

    The source says the tree's ability to capture complex patterns depends strongly on how the data is represented.

    Do this instead

    Ask how each model consumes its input feature, not only which model is more powerful in general.

  • Common mistake

    Forgetting that binning changes the input the model sees

    What it looks like

    A learner compares a binned linear model with an unbinned one and reads the difference as a property of the algorithm alone.

    Why it fails

    After binning, the linear model receives indicator features rather than the raw value, so the representation itself is different.

    Do this instead

    Record the feature representation as part of every comparison, alongside the model name.

  • Common mistake

    Drawing conclusions from the figure before reading the feature count

Locating Where a Surprise Starts

When a model comparison gives a result you did not expect, work through the pipeline in order instead of guessing. Start with the feature representation, then the model family, and only then the fitted curves. The divergence usually appears at one of these three points, and the first mismatch between your expectation and your reasoning tells you where to look.

Featurerepresentationhow many features,continuous or transformedModel familywhat shape it can expressFitted curvespredictions drawn over thepointsFinal predictioncompare with trainingpointsthenthenthen
What happens to the feature representation at each step of the model comparison, and where does the data move from raw x to the final prediction?
MEDIUM

A colleague compares a linear regression model on one continuous feature with a decision tree on the same feature. The linear model's curve is a straight line that misses a clear bend in the training points, and the colleague says the linear model is broken. Using the three-step order, say at which step you would look first, and what you would check at that step.

Hints
  • Start with the feature representation: how many input features does the linear model receive, and are they continuous as given?
  • If the model received one continuous feature, what shape can its prediction take?

Key Takeaways

  • A linear model with one input feature produces a straight line, because a line has only one slope and one intercept and cannot bend to follow a wavy pattern.
  • A decision tree regressor builds its prediction from a sequence of splits and region-by-region output values, so it can follow bends, but its strength depends strongly on how the data is represented.
  • Binning, also called discretization, replaces one continuous feature with several indicator features, one per bin, so a linear model can give each region its own weight and produce a step-shaped fit.
  • When a comparison surprises you, check the feature representation first, then the model family, then the fitted curves, and note the first step where your expectation and reasoning disagree.

Related concepts

More concepts starting with L