0% found this document useful (0 votes)
7 views3 pages

Program 4

This document outlines the implementation of a Naive Bayes classifier to predict Iris species based on morphological features from a dataset of 150 samples. The classifier was trained on 80% of the data and tested on 20%, achieving high accuracy, demonstrating its effectiveness for classification tasks. The methodology includes data preparation, model selection, training, prediction, and evaluation of accuracy.

Uploaded by

24pg1bymca040
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)
7 views3 pages

Program 4

This document outlines the implementation of a Naive Bayes classifier to predict Iris species based on morphological features from a dataset of 150 samples. The classifier was trained on 80% of the data and tested on 20%, achieving high accuracy, demonstrating its effectiveness for classification tasks. The methodology includes data preparation, model selection, training, prediction, and evaluation of accuracy.

Uploaded by

24pg1bymca040
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

Program-4: Write a program to implement the naive Bayesian classifier for

a sample training data set stored as a .CSV file. Compute the accuracy of the
classifier, considering few test data sets.
Title
Species Classification of Iris Flowers Using a Naive Bayes Classifier

Abstract
This study implements a Naive Bayes classifier to predict Iris species based on four morphological
features: sepal length, sepal width, petal length, and petal width. The Iris dataset, comprising 150
samples from three species, is divided into training and test sets. The classifier achieves high accuracy,
demonstrating its effectiveness for simple, real-valued feature classification tasks

Introduction
The Iris flower dataset is a foundational resource in machine learning and statistics, widely used for
classification model demonstrations. The dataset contains measurements from three Iris species: Iris
setosa, Iris versicolor, and Iris virginica, with 50 samples each. Each sample includes four features: sepal
length, sepal width, petal length, and petal width, all measured in centimeters. The goal of this work is
to implement and evaluate a Naive Bayes classifier for species prediction using these features.

Methodology

Data Preparation:

Features: sepal length, sepal width, petal length, petal width.

Target: Iris species (setosa, versicolor, virginica).

Preprocessing: No missing values; features are continuous and real-valued.

Model Selection:

Algorithm: Gaussian Naive Bayes, suitable for continuous features.

Training/Test Split: 80% training, 20% testing.

Training:

Prior Probability Estimation: Based on class frequency in training data.

Likelihood Calculation: Gaussian distribution for each feature per class.

Prediction and Evaluation:

Prediction: Assign class with highest posterior probability.

Evaluation Metric: Accuracy (proportion of correct predictions).


Dataset Description

Number of Samples: 150 (50 per species).

Features:

Sepal length (cm)

Sepal width (cm)

Petal length (cm)

Petal width (cm)

Target Variable: Species (setosa, versicolor, virginica).

No Missing Values: All samples are complete2.

Implementation Steps

 Load the Iris dataset:


Begin by loading the Iris dataset, which contains measurements of sepal length, sepal width,
petal length, and petal width for three types of Iris flowers.
 Prepare the features and target variable:
Assign the four measurements (sepal length, sepal width, petal length, and petal width) as your
input features. Assign the species labels as your target variable.
 Split the dataset into training and testing sets:
Divide the dataset so that 80% of the data is used for training the model and the remaining 20%
is used for testing its accuracy.
 Initialize the Naive Bayes classifier:
Choose the Gaussian Naive Bayes classifier, which is suitable for datasets with continuous
features like those in the Iris dataset.
 Train the classifier:
Fit the Naive Bayes classifier to the training data, allowing it to learn the statistical
characteristics of each feature for each species.
 Predict species on the test set:
Use the trained classifier to predict the species of Iris flowers in the test set based on their sepal
and petal measurements.
 Evaluate the classifier’s accuracy:
Compare the predicted species to the actual species in the test set and calculate the proportion
of correct predictions (accuracy).
 Output the results:
Display the accuracy of the classifier on the test data, which typically shows high performance
for the Iris dataset.

Conclusion
The Gaussian Naive Bayes classifier is highly effective for classifying Iris species using sepal and petal
dimensions, achieving high accuracy on the Iris dataset. Its simplicity and computational efficiency make
it suitable for similar small-scale classification tasks in botany and beyond.

CODE:

import pandas as pd

import numpy as np

df=pd.read_csv('[Link]')

X=[Link](['Species'],axis=1)

Y=df['Species']

'''X=[Link][:,:-1]

Y=[Link][:-1]

from [Link] import MinMaxScaler

scaler=MinMaxScaler() Preprocessing

X_scaled= scaler.fit_transform(X)

X_scaled

from sklearn.model_selection import train_test_split Train-Test Splitting

X_train, X_test, Y_train, Y_test = train_test_split(X_scaled, Y, test_size=0.2, random_state=42)

from sklearn.naive_bayes import GaussianNB

clf = GaussianNB()

[Link](X_train, Y_train) Naïve Bayes module

Y_pred=[Link](X_test)

Y_pred

Y_train

from [Link] import confusion_matrix, classification_report, accuracy_score

cm = confusion_matrix(Y_test, Y_pred)

print("Confusion Matrix:\n", cm) confusion metrics

print("Accuracy:", accuracy_score(Y_test, Y_pred))

print("\nClassification Report:\n")

print(classification_report(Y_test, Y_pred))

You might also like