0% found this document useful (0 votes)
3 views2 pages

Practical - 4 4.2 Decision Tree Implementation

The document outlines the implementation of a Decision Tree classifier using the Iris dataset, which is a supervised learning algorithm for classification and regression tasks. It includes Python code for loading the dataset, splitting it into training and testing sets, training the classifier, making predictions, and visualizing the decision tree. The accuracy of the model is also printed after predictions are made.

Uploaded by

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

Practical - 4 4.2 Decision Tree Implementation

The document outlines the implementation of a Decision Tree classifier using the Iris dataset, which is a supervised learning algorithm for classification and regression tasks. It includes Python code for loading the dataset, splitting it into training and testing sets, training the classifier, making predictions, and visualizing the decision tree. The accuracy of the model is also printed after predictions are made.

Uploaded by

Krishna Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CS3EL15 MACHINE LEARNING

Practical – 4
4.2 Decision Tree Implementation
A Decision Tree is a supervised learning algorithm used for both classification and regression tasks. It
works by splitting the dataset into branches based on feature values, leading to a tree-like structure where
each internal node represents a decision rule, and each leaf node represents an outcome.

Python implementation of a Decision Tree classifier using the Iris dataset.


Code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the Iris dataset


iris = datasets.load_iris()
X = iris.data[:, :2] # Taking only first two features for visualization
y = iris.target

# Split into training and testing data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train Decision Tree Classifier


clf = DecisionTreeClassifier(max_depth=3, random_state=42)
clf.fit(X_train, y_train)

# Make Predictions
y_pred = clf.predict(X_test)

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

# Visualize Decision Tree


EN23CS3T1005 ADITYA SHUKLA
CS3EL15 MACHINE LEARNING

plt.figure(figsize=(10, 6))
plot_tree(clf, feature_names=iris.feature_names[:2], class_names=iris.target_names, filled=True)
plt.title("Decision Tree Visualization on Iris Dataset")
plt.show()

Output:

EN23CS3T1005 ADITYA SHUKLA

You might also like