0% found this document useful (0 votes)
3 views

Prac5 AAM

This document outlines a Python implementation of a Decision Tree classifier using the Iris dataset for classification tasks. It details the necessary libraries, the process of loading and splitting the dataset, fitting the classifier, making predictions, and evaluating the model's performance through accuracy and a classification report. The conclusion emphasizes the versatility of Decision Trees in handling various data types and their effectiveness in classification tasks.

Uploaded by

Khan Rahil Ahmed
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 views

Prac5 AAM

This document outlines a Python implementation of a Decision Tree classifier using the Iris dataset for classification tasks. It details the necessary libraries, the process of loading and splitting the dataset, fitting the classifier, making predictions, and evaluating the model's performance through accuracy and a classification report. The conclusion emphasizes the versatility of Decision Trees in handling various data types and their effectiveness in classification tasks.

Uploaded by

Khan Rahil Ahmed
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

Practical – 5:

Aim: - Write a python Programming code to implement decision tree for classification
using suitable data/dataset.

Introduction:
Decision Trees are a popular supervised learning algorithm used for classification and regression tasks. In this
Python programming code, we will implement a Decision Tree classifier using a suitable dataset. We'll explore
how Decision Trees can be used to classify instances into different classes based on the features available in the
dataset.

Code:
# Importing necessary libraries
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_iris

# Loading the dataset


data = load_iris()
X = data.data
y = data.target

# Splitting 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)

# Initializing the Decision Tree classifier


classifier = DecisionTreeClassifier()

# Fitting the classifier to the training data


classifier.fit(X_train, y_train)

# Making predictions on the testing data


y_pred = classifier.predict(X_test)

# Evaluating the model


accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)

print("Accuracy:", accuracy)
print("Classification Report:")
print(report)

Explanation:
In this code, we first import necessary libraries including train_test_split for splitting the dataset,
DecisionTreeClassifier for Decision Tree classification, accuracy_score and classification_report for
evaluating the model. We then load a suitable dataset (iris dataset in this case) using load_iris() function from
sklearn.datasets.

We split the dataset into training and testing sets using train_test_split() function. Next, we initialize a
Decision Tree classifier and fit it to the training data using fit() method. Then, we make predictions on the
testing data using predict() method.

Finally, we evaluate the performance of the model by calculating accuracy using accuracy_score() and
generating a classification report using classification_report().

Output:

Outcomes:
The outcomes of this code include the accuracy of the Decision Tree classifier and a classification report
providing details such as precision, recall, and F1-score for each class.

Conclusion:
Decision Trees are versatile classifiers that can effectively handle both numerical and categorical data. In this
code, we demonstrated how to implement a Decision Tree classifier for classification tasks using a suitable
dataset. By evaluating the accuracy and examining the classification report, we can assess the performance of
the model and gain insights into its effectiveness for the given task.

You might also like