Prac5 AAM
Prac5 AAM
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
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.