6.
Write a program to demonstrate the working of the decision tree based
ID3 algorithm
Aim
To implement and demonstrate the working of the ID3 (Iterative Dichotomiser
3) Algorithm, a decision tree algorithm used for classification based on entropy
and information gain.
Algorithm
Step 1: Load the Dataset
● Load the Iris dataset from load_iris().
● Extract features (X) and target labels (y).
Step 2: Split the Dataset
● Split X and y into training and testing sets using train_test_split().
● Use test_size=0.2 (80% training, 20% testing).
● Set random_state=42 to ensure reproducibility.
Step 3: Train the Decision Tree Model
● Initialize a Decision Tree Classifier with random_state=42.
● Fit the model on the training data (X_train, y_train).
Step 4: Make Predictions
● Use the trained model to predict labels for X_test.
● Store predictions in y_pred.
Step 5: Evaluate Model Performance
● Compute accuracy score using accuracy_score(y_test, y_pred).
● Generate classification report (precision, recall, f1-score).
● Compute confusion matrix to analyze misclassifications.
Step 6: Visualize the Decision Tree
● Use tree.plot_tree() to plot the decision tree.
● Display feature names (iris.feature_names) and class names
(iris.target_names).
● Show the visualization using plt.show().
Step 7: Output the Results
● Accuracy
● Classification Report
● Confusion Matrix
● Display the Decision Tree Visualization.
Program
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix
import matplotlib.pyplot as plt
from sklearn import tree
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
print('Classification Report:')
print(classification_report(y_test, y_pred))
print('Confusion Matrix:')
print(confusion_matrix(y_test, y_pred))
plt.figure(figsize=(12,8))
tree.plot_tree(clf, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names)
plt.title("Decision Tree Visualization")
plt.show()
Output
Accuracy:
Decision tree diagram
Result
The Decision Tree Classifier achieves ~ accuracy and decision tree structure drawn for
the iris dataset.