Machine Learning

Decision Tree: Example with python

A decision tree algorithm is a popular supervised machine learning algorithm used for both classification and regression tasks. It creates a tree-like model of decisions and their possible consequences based on the features of the input data.

In a decision tree, each internal node represents a feature or attribute, each branch represents a decision or rule based on that feature, and each leaf node represents the outcome or prediction. The tree is constructed by recursively partitioning the data based on the values of the features until a certain stopping criterion is met.

During the construction of a decision tree, the algorithm aims to find the best splits or decisions that maximize the separation of the classes or minimize the impurity of the target variable. Some common impurity measures used in decision trees are Gini impurity and entropy.

Once the decision tree is built, it can be used to make predictions by traversing the tree from the root node to a leaf node based on the feature values of a new instance. The predicted outcome is then determined by the majority class of the instances falling into that leaf node for classification tasks or the average value for regression tasks.

Decision trees have several advantages, including interpretability, as the resulting tree structure is easy to understand and visualize. They can handle both numerical and categorical features and are robust to outliers. Decision trees also provide feature importance rankings, which can be useful for feature selection.

However, decision trees are prone to overfitting if they are allowed to grow too deep and become overly complex. To address this, techniques such as pruning, setting a maximum depth, or using ensemble methods like random forests or gradient boosting can be employed.

Overall, decision tree algorithms are versatile and widely used in various domains due to their simplicity, interpretability, and ability to handle both classification and regression tasks.

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the dataset
iris = datasets.load_iris()
X =
y =

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a decision tree classifier
clf = DecisionTreeClassifier()

# Train the decision tree classifier, y_train)

# Make predictions on the test set
y_pred = clf.predict(X_test)

# Evaluate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

In this example, we use the popular Iris dataset, which is available in scikit-learn. The dataset contains measurements of sepal length, sepal width, petal length, and petal width for three different iris species.

We split the dataset into training and testing sets using the train_test_split function. Then, we create an instance of the DecisionTreeClassifier class and train it using the training data. Next, we use the trained model to make predictions on the test set.

Finally, we evaluate the accuracy of the model by comparing the predicted labels (y_pred) with the true labels (y_test). The accuracy score is a common metric for classification problems, which measures the proportion of correctly predicted instances.

Remember to have scikit-learn installed (pip install scikit-learn) before running this code.

Leave a Reply

Your email address will not be published. Required fields are marked *