How To Build Decision Tree in MATLAB? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report MATLAB is a numerical and programming computation platform that is primarily used for research, modeling, simulation, and analysis in academics, engineering, physics, finance, and biology. MATLAB, which stands for "MATrix LABoratory," was first trying out typical tasks such as matrices operations, linear algebra, and signal processing It has additional use in artificial intelligence, deep learning, and machine learning, and it contains a variety of toolboxes for certain applications including control systems, optimization, and image processing. How to build a decision tree in MATLAB?For this demonstration, we make use of the MATLAB dataset fisheriris which is pre-defined.The fisheriris dataset comprises measurements of iris flowers.PetalLength and PetalWidth are the two parameters we choose to act as predictors, and then we develop a categorical response variable depending on the iris species.Then, use cvpartition function to divide the data into training and testing sets.In order to predict the class labels of the test data, first utilize the training data to build a decision tree using the fitctree function.The classification accuracy is then calculated by contrasting the anticipated labels with the actual labels of the test data.Example 1: Matlab % Loading the Iris dataset. load fisheriris % Splitting the data into training and testing. cv = cvpartition(species,'HoldOut',0.3); Xtrain = meas(cv.training,:); Ytrain = species(cv.training); Xtest = meas(cv.test,:); Ytest = species(cv.test); % Training the decision tree model. tree = fitctree(Xtrain, Ytrain); % Viewing the decision tree. view(tree,'Mode','graph'); % Predicting the classes of the testing set. Ypred = predict(tree, Xtest); % Calculate the accuracy of the model. accuracy = sum(Ypred == Ytest)/length(Ytest); disp(['Accuracy: ' num2str(accuracy)]); Output: Comment More infoAdvertise with us Next Article How To Build Decision Tree in MATLAB? S shivatiwari1003 Follow Improve Article Tags : Software Engineering MATLAB-graphs Similar Reads How to build classification trees in R? In this article, we will discuss What is a Classification Tree and how we create a Classification Tree in the R Programming Language. What is a Classification Tree?Classification trees are powerful tools for predictive modeling in machine learning, particularly for categorical outcomes. In R, the rp 3 min read How to Avoid Common Mistakes in Decision Trees Decision trees are powerful tools in machine learning, but they can easily fall prey to common mistakes that can undermine their effectiveness. In this article, we will discuss 10 common mistakes in Decision Tree Modeling and provide practical tips for avoiding them.Technique to Avoid Common Mistake 6 min read How Decision Tree Depth Impact on the Accuracy Decision trees are a popular machine learning model due to its simplicity and interpretation. They work by recursively splitting the dataset into subsets based on the feature that provides the most information gain. One key parameter in decision tree models is the maximum depth of the tree, which de 6 min read Passing categorical data to Sklearn Decision Tree Theoretically, decision trees are capable of handling numerical as well as categorical data, but, while implementing, we need to prepare the data for classification. There are two methods to handle the categorical data before training: one-hot encoding and label encoding. In this article, we underst 5 min read How to Specify Split in a Decision Tree in R Programming? Decision trees are versatile and widely used machine learning algorithms for both classification and regression tasks. A fundamental aspect of building decision trees is determining how to split the dataset at each node effectively. In this comprehensive guide, we will explore the theory behind deci 6 min read Like