0% found this document useful (0 votes)
2 views7 pages

Program 4

The document outlines the implementation of a Decision Tree Classifier, a supervised learning algorithm that predicts class labels by recursively splitting data based on feature values. It describes the algorithm's steps, advantages, and disadvantages, and provides a Python program using the Iris dataset to demonstrate its application. The program includes model training, prediction, and visualization of the decision tree.

Uploaded by

Amalendu Bag
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Program 4

The document outlines the implementation of a Decision Tree Classifier, a supervised learning algorithm that predicts class labels by recursively splitting data based on feature values. It describes the algorithm's steps, advantages, and disadvantages, and provides a Python program using the Iris dataset to demonstrate its application. The program includes model training, prediction, and visualization of the decision tree.

Uploaded by

Amalendu Bag
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AIM of The Experiment

Explain and implement a Decision Tree Classifier.


Decision Tree Classifier

A Decision Tree Classifier is a supervised learning algorithm that predicts a class by recursively
splitting data based on feature values—like a flowchart of decisions.

At each step, the model asks a question:


“Which feature split makes the data most pure?”
Purity is measured using Entropy or Gini Index.
Example

Dataset (Play Tennis)

Outlook Temperature Humidity Wind Play


Sunny Hot High Weak No
Sunny Hot High Strong No
Overcast Hot High Weak Yes
Rain Mild High Weak Yes
Rain Cool Normal Weak Yes
Rain Cool Normal Strong No
Overcast Cool Normal Strong Yes
Sunny Mild High Weak No
Sunny Cool Normal Weak Yes
Rain Mild Normal Weak Yes
Algorithm
1. Start with all data at the root
2. Compute impurity (Entropy/Gini)
3. Try splits on each feature
4. Pick the best split (max IG / min Gini)
5. Recurse on each branch
6. Stop when:
o Node is pure, or
o Max depth reached, or
o No improvement

Advantages
• Easy to understand & interpret
• Handles non-linear relationships
• Works with both numeric & categorical data
• No feature scaling required

Disadvantages
• Prone to overfitting (deep trees)
• Sensitive to small data changes
• Can become complex
Program-1

# Import libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import DecisionTreeClassifier

from [Link] import accuracy_score, classification_report

# Load dataset

data = load_iris()

X = [Link]

y = [Link]

# Split data

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.3, random_state=42

# Create model

model = DecisionTreeClassifier(criterion='entropy', max_depth=3)

# Train

[Link](X_train, y_train)

# Predict

y_pred = [Link](X_test)

# Results

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n")
print(classification_report(y_test, y_pred))

# User input prediction

print("\nEnter flower features:")

sl = float(input("Sepal Length: "))

sw = float(input("Sepal Width: "))

pl = float(input("Petal Length: "))

pw = float(input("Petal Width: "))

sample = [[sl, sw, pl, pw]]

pred = [Link](sample)

print("\nPredicted Class:", pred[0])

print("Flower Name:", data.target_names[pred[0]])

from [Link] import plot_tree

import [Link] as plt

[Link](figsize=(12,8))

plot_tree(model,

feature_names=data.feature_names,

class_names=data.target_names,

filled=True)

[Link]()
Output

You might also like