Prediction Using Classification and Regression Trees in MATLAB
Last Updated :
24 Apr, 2025
A Classification and Regression Tree(CART) is a Machine learning algorithm to predict the labels of some raw data using the already trained classification and regression trees. Initially one needs enough labelled data to create a CART and then, it can be used to predict the labels of new unlabeled raw data.
MATLAB is amongst the best tools to execute Machine Learning algorithms due to its fast matrix computations, and MATLAB also provides a great number of inbuilt tools and functions for machine learning. In this article, we shall see how to predict the labels of some new data using Classification and Regression Trees in MATLAB.
The Predict Function
MATLAB provides the predict() function which takes a CART and the new data whose label is to be predicted then, it returns the best fit label for the new data on the trend generated by the CART.
Syntax
Y = predict(CART, X)
where Y is the label of unlabeled data X. CART is the name of the Classification or Regression model used to predict Y for X. Considering that X is an array, the predict function will predict labels for each dimension of X using the CART.
Predictions using Classification and Regression Trees
In this section, we shall predict using CARTs on the available data in MATLAB library using some examples.
We shall use one of the 33 sample datasets provided by MATLAB for Data Science, the carbig.mat dataset. The classes in this dataset are as follows:
Once you load the dataset using the load statement, these arrays will become available in your workspace.
Note: When loading any sample dataset, it must be noted that you require the Statistics and Machine Learning Toolbox in the offline installation of MATLAB. These sample datasets are not available in ordinary installation of MATLAB. However, they are available in MATLAB online.
Now, let us start predicting some results.
Example 1:
Matlab
load carbig.mat
Xraw = [Horsepower,Displacement];
CM = fitctree(Xraw,Origin);
Yraw = predict(CM,mean(Xraw))
|
Output:
Explanation
In this example, we load the car big dataset which contains many variables. From them, we create a sub-array that only contains the organized data of the Horsepower of a car with displacement capacity. Then, we will predict the country of origin(Origin) of the cars whose horsepower and displacement are the mean of the same entities in the car big data.
Thus. we create a new sub-array Xnew and make two columns in it, Horsepower and Displacement. Then we generate a classification tree using the fitctree function and give them the label, and the country of their origin; which is also given in the dataset.
As we can see, the cars with the mean value of Horsepower and Displacement, belong to the country USA.
Now, we use the predict function to on the mean value of Xraw sub-array and predict its country of origin.
Let’s see another example, we shall use a regression tree to predict a label for some raw data. We shall use the same dataset as previous example but, now we shall predict the acceleration of cars whose weight is the mean of ‘Car Weight in carbig dataset’. This shall provide us a good understanding of Regression Trees.
Example 2:
Matlab
load carbig.mat
Xraw = Weight;
CM = fitrtree(Xraw,Acceleration);
Yraw = predict(CM,mean(Xraw))
|
Output:
Explanation
As done in the previous example, we take a feature from the car big dataset (Weight) and then, generate a regression tree using the fitrtree function between Weight and Acceleration. Then we use the predict function to predict the acceleration of cars whose weight is the mean weight of cars present in the car big dataset on the basis of the trend fitted by the RM regression tree.
Conclusion
In this article, we studied how to use Classification and Regression Trees in MATLAB to predict some features. We used both classification and regression on the same dataset to predict different results. However, this choice of whether to use a classification tree or a regression tree completely depends on the user’s needs. If one needs to classify some data into categories (as shown in Example 1) one could use a Classification Tree, and if one needs to predict some feature based on the trend (as done in Example 2), they could use a Regression tree which uses trends to predict new labels.
Similar Reads
Text Classification using Logistic Regression
Text classification is a fundamental task in Natural Language Processing (NLP) that involves assigning predefined categories or labels to textual data. It has a wide range of applications, including spam detection, sentiment analysis, topic categorization, and language identification. Logistic Regre
4 min read
Implementing CART (Classification And Regression Tree) in Python
Classification and Regression Trees (CART) are a type of decision tree algorithm used in machine learning and statistics for predictive modeling. CART is versatile, used for both classification (predicting categorical outcomes) and regression (predicting continuous outcomes) tasks. Here we check the
6 min read
Text Classification using Decision Trees in Python
Text classification is the process of classifying the text documents into predefined categories. In this article, we are going to explore how we can leverage decision trees to classify the textual data. Text Classification and Decision Trees Text classification involves assigning predefined categori
5 min read
Linear Regression for Single Prediction
Linear regression is a statistical method and machine learning foundation used to model relationship between a dependent variable and one or more independent variables. The primary goal is to predict the value of the dependent variable based on the values of the independent variables. Predicting a S
6 min read
Heart Disease Prediction Using Logistic Regression in R
Machine learning can effectively identify patterns in data, providing valuable insights from this data. This article explores one of these machine learning techniques called Logistic regression and how it can analyze the key patient details and determine the probability of heart disease based on pat
13 min read
Data Prediction using Decision Tree of rpart
Decision trees are a popular choice due to their simplicity and interpretation, and effectiveness at handling both numerical and categorical data. The rpart (Recursive Partitioning) package in R specializes in constructing these trees, offering a robust framework for building predictive models. Over
3 min read
Multiple Linear Regression using R to predict housing prices
Predicting housing prices is a common task in the field of data science and statistics. Multiple Linear Regression is a valuable tool for this purpose as it allows you to model the relationship between multiple independent variables and a dependent variable, such as housing prices. In this article,
12 min read
Tree-Based Models for Classification in Python
Tree-based models are a cornerstone of machine learning, offering powerful and interpretable methods for both classification and regression tasks. This article will cover the most prominent tree-based models used for classification, including Decision Tree Classifier, Random Forest Classifier, Gradi
8 min read
Classification Metrics using Sklearn
Machine learning classification is a powerful tool that helps us make predictions and decisions based on data. Whether it's determining whether an email is spam or not, diagnosing diseases from medical images, or predicting customer churn, classification algorithms are at the heart of many real-worl
14 min read
Predict default payments using decision tree in R
Predicting default payments is a common task in finance, where we aim to identify whether a customer is likely to default on their loan based on various attributes. Decision trees are a popular choice for this task due to their interpretability and simplicity. In this article, we will demonstrate ho
5 min read