Decision Tree Regression: What You Need to Know
Updated on Mar 28, 2025 | 7 min read | 6.8k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 7 min read | 6.8k views
Share:
Table of Contents
To begin with, a regression model is a model that gives as output a numeric value when given some input values that are also numeric. This differs from what a classification model does. It classifies the test data into various classes or groups involved in a given problem statement.
The size of the group can be as small as 2 and as big as 1000 or more. There are multiple regression models like linear regression, multivariate regression, Ridge regression, logistic regression, and many more.
Decision tree regression models also belong to this pool of regression models. The predictive model will either classify or predict a numeric value that makes use of binary rules to determine the output or target value.
The decision tree model, as the name suggests, is a tree-like model that has leaves, branches, and nodes.
Before we delve into the algorithm, here are some important terminologies that you all should be aware of.
1. Root node: It is the topmost node from which the splitting begins.
2. Splitting: Process of subdividing a single node into multiple sub-nodes.
3. Terminal node or leaf node: Nodes that don’t split further are called terminal nodes.
4. Pruning: The process of removal of sub-nodes.
5. Parent node: The node that splits further into sub-nodes.
6. Child node: The sub-nodes that have emerged from the parent node.
Read: Guide to Decision Tree Algorithm
The decision tree breaks down the dataset into smaller subsets. A decision leaf splits into two or more branches that represent the value of the attribute under examination. The topmost node in the decision tree is the best predictor, called the root node. ID3 is the algorithm that builds the decision tree.
It employs a top to down approach and splits are made based on standard deviation. Just for a quick revision, Standard deviation is the degree of distribution or dispersion of a set of data points from its mean value. Another widely used criterion for splitting is Gini impurity, which determines how often a randomly chosen element would be misclassified if randomly labeled.
Key Features of Decision Trees:
Interpretability: Decision trees offer an unambiguous and straightforward picture of the decision-making process.
Nonlinearity: Decision trees are capable of capturing nonlinear connections between input data and the target variables.
Missing data: Decision trees are capable of handling missing data without the need for imputation.
Feature Importance: Decision trees can provide knowledge regarding the relative value of several characteristics in forecasting the target variable.
Outlier Sensitivity: Decision trees are less susceptible to outliers than other regression techniques.
It quantifies the overall variability of the data distribution. A higher value of dispersion or variability means greater is the standard deviation indicating the greater spread of the data points from the mean value. We use standard deviation to measure the uniformity of the sample.
If the sample is totally homogeneous, its standard deviation is zero. And similarly, higher is the degree of heterogeneity, greater will be the standard deviation. Mean of the sample and the number of samples are required to calculate standard deviation.
We use a mathematical function — Coefficient of Deviation that decides when the splitting should stop It is calculated by dividing the standard deviation by the mean of all the samples.
The final value would be the average of the leaf nodes. Say, for example, if the month of November is the node that splits further into various salaries over the years in the month of November (until 2021). For the year 2022, the salary for the month of November would be the average of all the salaries under the node November.
Moving on to the standard deviation of two classes or attributes(like for the above example, salary can be based either on an hourly basis or a monthly basis).
To construct an accurate decision tree, the goal should be to find attributes that return upon calculation and return the highest standard deviation reduction. In simple words, the most homogeneous branches.
The process of creating a Decision tree for regression covers these important steps.
1. Firstly, we calculate the standard deviation of the target variable. Consider the target variable to be salary, like in previous examples. With the example in place, we will calculate the standard deviation of the set of salary values.
2. In step 2, the data set is further split into different attributes. talking about attributes, as the target value is salary, we can think of the possible attributes as — months, hours, the mood of the boss, designation, year in the company, and so on. Then, the standard deviation for each branch is calculated using the above formula. the standard deviation so obtained is subtracted from the standard deviation before the split. The result at hand is called the standard deviation reduction.
Check out: Types of Binary Tree
3. Once the difference has been calculated as mentioned in the previous step, the best attribute is the one for which the standard deviation reduction value is largest. That means the standard deviation before the split should be greater than the standard deviation after the split. Actually, most of the difference is taken, and so vice versa is also possible.
4. The entire dataset is classified based on the importance of the selected attribute. On the non-leaf branches, this method is continued recursively till all the available data is processed. Now consider month is selected as the best splitting attribute based on the standard deviation reduction value. So we will have 12 branches for each month. These branches will further split to select the best attribute from the remaining set of attributes. In some cases, Gini impurity can also be used to determine the best split, especially in classification problems.
5. In reality, we require some finishing criteria. For this, we make use of the coefficient of deviation or CV for a branch that becomes smaller than a certain threshold like 10%. When we achieve this criterion we stop the tree building process. Because no further splitting happens, the value that falls under this attribute will be the average of all the values under that node.
Must Read: Decision Tree Classification
Decision Tree Regression can be implemented using Python language and scikit-learn library. It can be found under the sklearn.tree.DecisionTreeRegressor.
1.criterion: To measure the quality of a split. It’s value can be “mse” or the mean squared error, “friedman_mse”, and “mae” or the mean absolute error. Default value is mse.
2.max_depth: It represents the maximum depth of the tree. Default value is None.
3.max_features: It represents the number of features to look for when deciding the best split. Default value is None.
4.splitter: This parameter is used to choose the split at each node. Available values are “best” and “random”. Default value is best.
Setting a Maximum Depth Limit: The decision tree’s depth is constrained by the max_depth parameter, which keeps it from overcomplicating and overfitting the training set of data.
Pruning: After the decision tree has been constructed, pruning procedures may be used to eliminate pointless branches or nodes that don’t substantially improve the predicted performance.
Decision tree regression in machine learning can be used with ensemble techniques to boost forecasting precision. Both Random Forest and Gradient Boosting, two well-liked ensemble approaches, make use of many decision trees.
>>> from sklearn.datasets import load_diabetes
>>> from sklearn.model_selection import cross_val_score
>>> from sklearn.tree import DecisionTreeRegressor
>>> X, y = load_diabetes(return_X_y=True)
>>> regressor = DecisionTreeRegressor(random_state=0)
>>> cross_val_score(regressor, X, y, cv=10)
… # doctest: +SKIP
array([-0.39…, -0.46…, 0.02…, 0.06…, -0.50…,
0.16…, 0.11…, -0.73…, -0.30…, -0.00…])
Overfitting: Decision trees are vulnerable to overfitting, particularly when they grow too deep or complicated. Poor generalizations based on unknown data may result from this. Overfitting can be reduced using methods like pruning, regularization, and establishing a limit depth.
Instability: Decision trees are unstable and sensitive to even minor modifications in the training set of data. Adding or deleting a few data points can drastically alter a tree’s structure. Random forests and other ensemble techniques can aid in enhancing stability.
Relationships in Linear Form: Decision trees are not very good at capturing relationships in linear form between attributes and the target variable. They work better for issues with complicated or non-linear relationships.
Decision tree regression is capable of handling both categorical as well as numerical information in the section on attributes and attribute selection. Before being used in the procedure, categorical variables must be converted into numerical form. One-hot encoding and label encoding are examples of common encoding methods.
The structure of the Data Science Program designed to facilitate you in becoming a true talent in the field of Data Science, which makes it easier to bag the best employer in the market. Register today to begin your learning path journey with upGrad!
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources