Regression With Tree-Based Models
A regression tree's output is flat over each region of the feature and jumps at the cutoffs. It is not a smooth function that rises or falls steadily as the feature grows.
Predicting From Past Observations
Suppose you have a table of past observations and you want a program to predict something about a new case. One family of methods for doing this is the decision tree. A decision tree makes its prediction by asking a sequence of questions about the input values, and each answer narrows down what the prediction should be. This article follows one idea through the lesson: the same tree can handle categories and numbers, its output is not a simple ordered function of a feature, and it has a limit you must keep in mind when predicting numbers.
What do you think happens?
A regression tree was trained on values of a feature that ranged from 2 to 10. You now ask it to predict for an input of 50. What do you think it will do?
Reveal answer
It returns a value based only on what it learned from the training range, because it cannot extrapolate.
The source states that DecisionTreeRegressor, and all other tree-based regression models, make predictions only within the range of the training data. They are not able to extrapolate, so an input of 50 is answered using patterns learned from the 2 to 10 range rather than by continuing a trend. The answer is not a refusal; the model still returns a number, but that number is not a trustworthy continuation of the trend.
Questions That Narrow Down an Answer
A decision tree is built from questions about feature values. Each question has a yes or no style answer, such as whether a value is below a cutoff. Starting at the top of the tree, the program follows one branch according to the answer, asks the next question on that branch, and continues until it reaches an end point. The end point is where the tree gives its final answer for that input.
A decision tree is a model that makes its prediction by asking a sequence of questions about the values of the input features. Each path from the top of the tree to an end point is a combination of conditions that leads to one final answer.
Think of the tree as a set of nested cutoffs. The first question divides all possible inputs into two groups. Each later question divides one of those groups again. Because of this nesting, every input ends up in exactly one end point, and each end point holds one answer. The rest of the article looks at what those answers look like when the tree is used to predict a number.
Splitting Works the Same for Classes and Numbers
The source introduces decision trees in the context of classification first. In classification, each input is assigned to a class by following a path of questions about feature values. Then it explains that everything said about classification also applies to regression. Keeping the two tasks separate in your head makes the rest of the material easier to follow.
The difference between the two tasks is in what the final answer is. A classification tree ends in a class label. A regression tree ends in a numeric prediction. You can think of a regression tree as answering a sequence of questions about the input and then returning a number that corresponds to where the input landed. The questions and the way the tree is grown are the same idea in both cases; only the value stored at the end point changes.
| Aspect | Classification tree | Regression tree (DecisionTreeRegressor) |
|---|---|---|
| Question asked at each node | A test on a feature value | A test on a feature value |
| What the end point holds | A class label | A numeric prediction |
| Typical target | A category | A number that can change over time |
| Shape of output as one feature grows | Not a simple ordered pattern; classes can change direction | Stays flat over ranges and changes at cutoffs, not in a smooth ordered slope |
| Ability to extrapolate | Only predicts within the training data | Cannot extrapolate beyond the range of its training data |
The same tree idea applied to two kinds of target
Tracing Predictions Across a Feature
Now trace what happens when you slowly increase one feature and watch the prediction. The tree does not produce a smooth curve. It asks its cutoff questions in order, so as the feature moves from one range into the next, the answer stays the same across an entire range and then jumps to a different value when a cutoff is crossed. The output is piecewise constant: flat over each region, with steps between regions.
Read the diagram from left to right as the feature grows. Inside the band from 2 to 5, every input gets the same prediction, 14, no matter where it sits in that band. The prediction only changes when the input crosses a cutoff. This is why the output does not move in one direction: it goes from 10 up to 14, down to 9, and up to 20 as the feature increases. The numbers here are illustrative, chosen to show the shape a tree can produce, not values from a specific model.
A regression tree's output is flat over each region of the feature and jumps at the cutoffs. It is not a smooth function that rises or falls steadily as the feature grows.
Why Outputs Are Not Monotonic
The source notes that classification tree outputs are not monotonous, which means you cannot summarize them by a simple ordering of a feature. In plain terms, as a feature increases, the predicted class does not have to move steadily in one direction. Each path from the top of the tree to a final answer is a combination of conditions, and different paths can lead to the same class or to different classes.
Imagine a tree whose first question separates the lowest values of a feature into one class. Its second question applies only to values above that first cutoff, and it sends those values into two different outcomes depending on where they fall. The class does not move in one direction as the feature grows: low values belong to the first class, the middle values can belong to a second class, and the highest values can return to the first class. This tree is an illustrative construction showing the shape that non-monotonic behavior allows, not a specific model from the source.
How a Logarithmic Scale Reshapes a Trend
The source illustrates regression with a dataset of historical computer memory (RAM) prices. Each point records a year on the horizontal axis and the price of one megabyte of RAM in that year on the vertical axis. The target, price, is a number that changes over time, which makes it a natural fit for a regression tree.
The price axis in the source is drawn on a logarithmic scale. A logarithmic scale spaces values by their ratio rather than their difference, so equal vertical distances represent equal multiplicative changes. A price that doubles and a price that doubles again look equally far apart on a log axis, even if the absolute price changes are very different. This choice changes how the data looks, and the source uses it on purpose.
Because a log axis converts steady ratio changes into steady visual distances, a curved pattern on a linear axis can look close to a straight line on a log axis. That makes the relationship easier to see. However, the change in appearance is only a change in how the data is displayed. It does not change the underlying values, and it does not give the tree any new ability to continue the pattern past its training data.
A logarithmic scale can make a relationship such as RAM price over time look more linear, but it does not remove the extrapolation limitation.
The Extrapolation Limit
Extrapolation means making predictions outside the range of the data the model was trained on. The source states that DecisionTreeRegressor, and all other tree-based regression models, are not able to extrapolate. They make predictions only within the range of the training data. The source treats this as the single most important caveat about regression trees.
The consequence is that a regression tree cannot follow a trend past the edge of its training data. Even when the RAM price data looks linear on a log scale, a tree trained on that history will not simply continue the line into dates it has never seen. Its predictions for those dates are limited by the values it learned from the training range.
Worked Scenario: Predicting Beyond the Data
Checking whether a tree can be trusted for a new input
A regression tree is trained on a hypothetical RAM price dataset covering the years 1980 to 2010, with the year as the feature. You want the tree's prediction for the year 2030. Should you read the output as a continuation of the price trend?
- 1Identify the training range
The training data covers 1980 to 2010, so the range the tree learned from runs from 1980 to 2010.
- 2Compare the new input to that range
The input 2030 lies after 2010, which is beyond the largest year seen in training. It is outside the training range.
- 3Apply the extrapolation rule
A regression tree cannot extrapolate, so its prediction for 2030 is limited to values learned from the 1980 to 2010 data rather than a continuation of a trend.
- 4Decide how to read the output
Because the input is outside the training range, the output should not be treated as a trustworthy forecast of the price in 2030, even if the training data looked linear on a log scale.
The tree will return a number for 2030, but that number reflects only the training range. It is not a reliable continuation of the trend.
Common Mistakes With Tree Regression
Common mistake Assuming a regression tree will continue a linear trend past the data
What it looks likeA tree trained on years up to 2010 is asked for a value in 2030 and the output is assumed to follow the straight line seen in the plot.
Why it failsThe source states that tree-based regression models cannot extrapolate and make predictions only within the range of the training data.
Do this insteadCheck whether the input lies inside the training range before trusting the output, and use a model that can extend a trend if extrapolation is needed.
Common mistake Expecting the prediction to change smoothly as a feature increases
What it looks likeA user expects the predicted value to rise a little with each unit increase, but the output stays the same across a range and then jumps.
Why it failsA regression tree's output is piecewise constant, flat over regions of the feature with jumps at cutoffs.
Do this insteadExpect steps at the cutoff points rather than a smooth curve, and inspect the thresholds the tree actually creates.
Common mistake Treating a log scale as a fix for the extrapolation problem
What it looks likeBecause the price data looks almost straight on a log axis, a user believes the tree will extend that line into future years.
Why it failsA logarithmic scale changes how the relationship looks but does not remove the extrapolation limitation.
Do this insteadUse the log scale to see the pattern more clearly, and separately recognize that the tree still cannot predict beyond its training range.
Common mistake Believing that the classification and regression versions of a tree use different splitting procedures
Practice Reasoning About the Training Range
A regression tree was trained on feature values between 3 and 12. For each of these inputs, say whether the tree is working inside or beyond its training range, and state how far you should trust its output: 5, 12.5, 20, and 8. Then explain in one sentence why a flat, step-like output is expected across the range.
Hints
- Compare each input with the smallest and largest feature values in training, which are 3 and 12.
- Inputs at or between 3 and 12 are inside the training range; inputs above 12 or below 3 are beyond it.
- The source says a regression tree's predictions are limited to the range of its training data, and its output changes at cutoffs rather than along a smooth slope.
Key Takeaways
- A decision tree makes its prediction by asking a sequence of questions about feature values, and the same tree idea covers classification (ending in a class label) and regression (ending in a numeric prediction).
- A regression tree's output is piecewise constant: it stays flat over ranges of a feature and changes at cutoffs, so it does not change in a simple, ordered way as the feature increases.
- DecisionTreeRegressor, like all tree-based regression models, cannot extrapolate; its predictions are limited to the range of its training data, so outputs beyond that range should not be read as a continuation of a trend.
- A logarithmic scale spaces values by ratio, so a curved relationship such as RAM price over time can look close to a straight line, but the log scale does not remove the extrapolation limitation.