Concepts / Linear Models vs Tree-Based Models

Linear Models vs Tree-Based Models

A linear model is limited in a specific way: it can only model linear relationships.

  • Programming

Predicting the Shape Before You Look

A dataset does not have one best representation in isolation. The right way to represent it depends on the meaning of the data and also on the kind of model you plan to train. This article compares two large, widely used families of models: linear models, and tree-based models such as decision trees, gradient boosted trees, and random forests. Their behavior differs a great deal once you change how the input features are represented.

To make the difference concrete, we use the wave regression dataset from Chapter 2. It has only one input feature, which makes it a clean test case. With one feature, you can draw the model's prediction as a curve over the input axis and see exactly what the model is able to express. The wave points rise and fall, so the underlying relationship is not a single straight trend.

What do you think happens?

The wave points rise and fall. Which model do you expect to track the points more closely across the whole input range: linear regression, or a decision tree regressor? Decide an answer for each model separately before reading on.

  • Linear regression tracks the points more closely

  • The decision tree regressor tracks the points more closely

  • Both track the points equally well

Reveal answer
Answer:

The decision tree regressor can follow the bends in the data, because it can build a much more complex model than a single line. Linear regression is forced into one straight shape, so it cannot follow the rise and fall of the points.

The reason lies in what each model is able to express with one input feature. The next sections trace that capability step by step, so you can check your guess against the curves.

Tracing the Straight Line of a Linear Model

A linear model is limited in a specific way: it can only model linear relationships. With a single input feature, a linear relationship is a line. The model's prediction is a weighted sum of the input plus an intercept, so no amount of fitting can make its curve bend. The best it can do is choose the slope and intercept that place one straight line as close to the points as possible.

Formula, step by step
  1. 1
    x is the single input feature, the value the model receives for each data point.
  2. 2
    w is the learned weight, which sets the slope of the line.
  3. 3
    b is the intercept, which sets where the line crosses the output axis.
  4. 4
    The hat over y, written as $\hat{y}$, marks the predicted output rather than the observed one.
  5. 5
    Because the expression is a line, the linear model's curve on the comparison plot is always straight, regardless of how the training points are arranged.

This is the core reason a linear model struggles with the wave data. The points follow a shape that a single line cannot match. Training can change the slope and the intercept, but the output will always remain one straight line over the input range.

Side-by-Side Prediction Curves

The comparison below summarizes what you should see when both models are fit to the same wave data and their predictions are drawn over a dense grid of input values together with the original points. Both fits use the same arrays, so any difference between the two curves comes from the model, not from the data.

Linear regressionDecision treeregressorSingle straight linePartitioned inputrangeOne slope and oneinterceptTests on the featureStraight curve onlyComplex shape
Which curve can follow the bends in the data, and which one is forced into a single straight shape? Both models were fit to the same single input feature.

When you look at the figure, ask which curve can follow the bends in the data. The linear model's curve is a single straight shape across the whole input range. The decision tree's curve is built from regions created by its tests on the feature, so it can take a more complex shape.

How a Tree Partitions the Input Range

A decision tree can build a much more complex model of the data than a line. The tree does not rely on a single global formula. Instead, it partitions the input range using tests on the feature and makes predictions from the regions those tests create. Each test is a threshold on the single input. Splitting the range again inside one side produces nested regions, and each final region gets its own prediction.

x ≤ t1?x ≤ t2?Region A predictionx ≤ t3?Region B predictionRegion C predictionRegion D predictionyesnoyesnoyesno
Each threshold divides the feature's range into nested regions. A new input travels from the top of the tree down one branch at each test until it reaches one leaf prediction. The thresholds t1, t2 and t3 are placeholders for illustration; the source does not give specific values.
Worked example

Routing one input through the tree

A new input value x arrives and must receive one prediction from the tree in the diagram above.

  1. 1
    Test at the root

    Compare x with threshold t1. If x is at most t1, follow the yes branch to the left; otherwise follow the no branch to the right.

  2. 2
    Take the left branch

    If the first test was yes, the next test is whether x is at most t2. That test splits the left region into two smaller regions.

  3. 3
    Reach a leaf

    The branch ends at a leaf, which is one of the regions A or B. The leaf stores the prediction for every input that landed in that region.

  4. 4
    Read the output

    The prediction for x is the value attached to that leaf. Inputs in the same region all receive the same output.

Result

The input reaches exactly one leaf, and that leaf's region determines the prediction. Because each region has its own constant prediction, the overall curve is piecewise rather than a single line.

The number of divisions depends on how the tree grows. The argument min_samples_split=3 in the source code sets the minimum number of samples a node must contain before the tree may split it further. Smaller values let the tree create more divisions, which generally means a more detailed curve, while larger values keep the tree simpler. This setting is one of the ways the tree's flexibility is controlled.

Binning: Turning One Feature into Several

The source describes one concrete way to make a linear model more powerful on continuous data: binning, which is also known as discretization. Binning takes one continuous feature and splits it into multiple features, each covering a different part of the input range. Once the feature is split this way, the linear model no longer has to fit one line across the whole input. It receives several separate inputs and can assign each one its own weight.

Original feature xbin 11 if x is in bin 1, else 0bin 21 if x is in bin 2, else 0bin 31 if x is in bin 3, else 0bin 41 if x is in bin 4, else 0bin edgesbin edgesbin edgesbin edges
Which range of the original feature's values turns on which new column? Each bin edge maps every input value to exactly one indicator column, which is set to 1 while the others are 0.

The diagram shows the change in representation. Before binning, the model sees one column whose values can be any number in the feature's range. After binning, the same information is expressed as several columns, one per bin. Each column records whether the input falls inside that bin's range. This indicator description of each bin is the standard way binned features are read, and it is how the new columns are meant to be interpreted here.

AspectBefore binningAfter binning
Number of input columnsOne column for the featureSeveral columns, one per bin
Meaning of each columnThe raw value of the featureWhether the input falls inside that bin's range
Typical valuesAny number in the feature's rangeIndicator values that mark one bin per input
Shape a linear model can produceOne straight lineA step-like shape, with one weight per bin

How the same continuous feature is represented before and after binning

Why Binned Weights Form a Staircase

Once the input is represented by indicator columns, the linear model still computes a weighted sum, but now each bin has its own weight. For any input, exactly one indicator is on, so the prediction is the weight of the bin that input falls into. Moving from one bin to the next changes the output by a jump determined by the two weights. The overall curve becomes a staircase rather than a single slope.

Worked example

Following one input through binned features

A feature is split into four bins. The linear model learns one weight for each bin. Trace what happens for an input that lies in bin 2, and then for one that lies in bin 3.

  1. 1
    Encode the input

    For an input in bin 2, the indicator for bin 2 is 1 and the indicators for bins 1, 3 and 4 are 0.

  2. 2
    Combine the weights

    The weighted sum uses only the weight attached to the bin 2 column, because every other column is multiplied by 0.

  3. 3
    Repeat for bin 3

    For an input in bin 3, the bin 3 indicator is 1 and the others are 0, so the prediction now uses the bin 3 weight instead.

  4. 4
    Compare the two outputs

    Inputs inside the same bin share one output. The change between bin 2 and bin 3 is the difference between their two learned weights, which produces one step of the staircase.

Result

Each bin contributes one flat level, so the linear model now produces a step-like shape from one continuous feature, even though the model itself is still linear in its inputs.

Mistakes When Comparing Model Curves

  • Common mistake

    Assuming a linear model will fit a curved pattern if it is trained longer or with more data

    What it looks like

    Refitting linear regression on the wave data and expecting the curve to start bending.

    Why it fails

    The prediction is a weighted sum of the input plus an intercept, so its curve is always a straight line for one feature. More fitting only changes the slope and intercept.

    Do this instead

    Treat the model family as the first question. If the relationship is curved and the model is linear with one feature, a gap between curve and points is expected.

  • Common mistake

    Changing the fitting code when the curves look wrong

    What it looks like

    Tweaking hyperparameters for a while when the linear curve still looks like a single line.

    Why it fails

    The problem often lies in the representation of the feature or in the choice of model, not in the code that fits it.

    Do this instead

    Check the model family and the feature representation first, then revisit the fitting code only if those are correct.

  • Common mistake

    Assuming the tree is always correct because its curve follows the points

    What it looks like

    Reading a detailed tree curve as proof that the model captured the true relationship.

    Why it fails

    The decision tree's performance is strongly dependent on the representation of the data, and its detail is controlled by settings such as min_samples_split.

    Do this instead

    Judge the curve in light of how the input is represented and how the tree was allowed to grow.

  • Common mistake

    Reading one binned column as a continuous feature

    What it looks like

    Interpreting an indicator column for a bin as if it held the raw value of x.

    Why it fails

    After binning, each column records only whether the input falls inside a bin's range.

    Do this instead

    Read each binned column as an indicator for one range, and expect exactly one indicator to be on for any input.

Locating Where the Output Diverges

Suppose a linear model's curve does not match what you expected from the points. The fastest way to find the problem is to ask three questions in order. Each question checks a different part of the pipeline from raw feature to prediction.

  1. 1
    Is the model family correct for this relationship? A linear regression can only produce a straight line for one feature, so a curved pattern in the points will always leave a gap.
  2. 2
    Did both models receive the same X and y? If both fits use the same arrays, any difference between the curves comes from the model, not from the data.
  3. 3
    Was the prediction grid the same for both curves? If both predictions are made on the same line of input values, the curves can be compared directly.
  4. 4
    Is the feature representation what you intended? If the input was binned, check that each column is an indicator for one range and that the bin edges are the ones you meant.
Raw featureRepresentationModel typePredictionbins or rawlinear or treecurve shape
Read the pipeline from left to right. The first stage where the output no longer matches what you expected is the stage to examine. Changing the fitting code is the last step, not the first.
MEDIUM

A colleague fits a linear regression to a single continuous feature whose relationship with the output rises and falls. The predicted curve is one straight line, and the colleague changes the optimizer settings three times without any change in the curve. Using the three diagnostic questions, explain where the output diverges from expectations and what you would check first.

Hints
  • Start with the model family. What shape can a linear model produce from one feature?
  • Ask whether the representation has changed. Would binning the feature change which shapes the linear model can express?
  • The optimizer settings affect how the slope and intercept are chosen, but not whether the curve can bend.

Key Takeaways

  • A linear model on one feature can only produce a straight line, because its prediction is a weighted sum of the input plus an intercept, so fitting cannot make the curve bend.
  • A decision tree builds a more complex shape by partitioning the input range with tests on the feature, and each final region gets its own prediction.
  • Binning, also called discretization, splits one continuous feature into several indicator features, which lets a linear model assign a separate weight to each bin and produce a step-like shape.
  • The decision tree's performance depends strongly on how the data is represented, so the feature representation and the model family should be checked before changing the fitting code.
  • When a comparison diverges from expectations, locate the stage where the output stops matching: the raw feature, its representation, the model type, or the prediction.

Related concepts

More concepts starting with L