Python Tutorial: Decision-Tree for Regression

Learn to build a decision-tree regressor in scikit-learn to predict car MPG from engine displacement. The tree captures the non-linear relationship that linear regression misses, achieving a root-mean-squared error of about 5.1 on the UCI Auto MPG dataset.
Chapters
Welcome back. In this video, you'll learn how to train a decision tree for a regression problem. Recall that in regression the target variable is continuous. In other words, the output of your model is a real value. Let's motivate our discussion for regression by introducing the Automobile miles per gallon data set from the UCI Machine Learning repository. This DataSet consists of six features corresponding to the characteristics of a car and the continuous target variable labeled mpg, which stands for miles per gallon. Our task is to predict the MPG consumption of a car given these six features. To simplify the problem. Here the analysis is restricted only to one feature corresponding to the displacement of a car. This feature is denoted by Dispel. A 2D scatterplot of MPG versus Dispel shows that the MPG consumption decreases non linearly with displacement. Note that linear models such as linear regression would not be able to capture such a nonlinear trend. Let's see how you can train a decision tree with Scikit. Learn to solve this regression problem. Note that the features and the labels y are already loaded in the environment. First import decision tree regressor from Sklearn tree and the functions traintestsplit from sklearn model selection and mean squared error as MSE from sklearn metrics. Then split the data into 80% train and 20% test. Using traintestsplit, you can now instantiate the decision tree regressor with a maximum depth of 4 by setting the parameter maxdepth to 4. In addition, set the parameter minsample leaf to 0.1 to impose a stopping condition in which each leaf has to contain at least 10% of the training data. Now fit dt to the training set and predict the test set labels to obtain the root mean squared error of your model on the test set. Proceed as first evaluate the mean squared error. Then raise the obtained value to the power 1/2. Finally, print DT's test set RMSE to obtain a value of 5.1. Here it's important to note that when a regression tree is trained on a data set, the impurity of a node is measured using the mean squared error of the targets in that node. This means that the regression tree tries to find the splits that produce the leafs where in each leaf the target values are on average the closest possible to the mean value of the labels in that particular leaf. As a new instance traverses the tree and reaches a certain leaf, its target variable Y is computed as the average of the target variables contained in that leaf, as shown in this formula. To highlight the importance of the flexibility of regression trees. Take a look at this figure. On the left, we have a scatter plot of the data in blue, along with the predictions of a linear regression model shown in black. The linear model fails to capture the nonlinear trend exhibited by the data. On the right, we have the same scatter plot along with a red line corresponding to the predictions of the regression tree that you trained earlier. The regression tree shows a greater flexibility and is able to capture the nonlinearity, though not fully. In the next chapter, you'll aggregate the predictions of a set of trees that are trained differently to obtain better results. Now it's your turn to practice.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







