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

Prac4 AAM

This document outlines a practical implementation of the Naive Bayes classification algorithm using Python and the Iris dataset. It details the steps of importing necessary libraries, splitting the dataset, fitting the model, making predictions, and evaluating the model's performance. The conclusion emphasizes the effectiveness and simplicity of Naive Bayes for classification tasks in machine learning.

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)
0 views

Prac4 AAM

This document outlines a practical implementation of the Naive Bayes classification algorithm using Python and the Iris dataset. It details the steps of importing necessary libraries, splitting the dataset, fitting the model, making predictions, and evaluating the model's performance. The conclusion emphasizes the effectiveness and simplicity of Naive Bayes for classification tasks in machine learning.

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 – 4:

Aim: - write a python programming code applying naive Bayesian for classification using
suitable dataset.

Introduction:
Naive Bayes is a simple yet effective probabilistic classifier based on Bayes' theorem with the assumption of
independence between features.
In this Python programming code, we'll implement Naive Bayes for classification using a suitable dataset. We'll
explore how Naive Bayes can be applied 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.naive_bayes import GaussianNB
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 Naive Bayes classifier


classifier = GaussianNB()

# 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)
Output:

Explanation:
In this code, we first import necessary libraries including train_test_split for splitting the dataset, GaussianNB
for Naive Bayes 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
Gaussian Naive Bayes 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().

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

Conclusion:
Naive Bayes is a simple yet powerful classification algorithm that can be effectively applied to various datasets.
In this code, we demonstrated how to implement Naive Bayes for classification using a suitable dataset and
evaluated its performance. With its simplicity and efficiency, Naive Bayes can be a valuable tool for
classification tasks in machine learning.

You might also like