100% found this document useful (1 vote)
46 views3 pages

Assignment No - 6-1

This document summarizes the use of Naive Bayes classification on the iris dataset. It loads and explores the iris data, splits it into training and test sets, fits a Gaussian Naive Bayes classifier to the training set, makes predictions on the test set, and evaluates the model's performance using a confusion matrix and classification report.

Uploaded by

Sid Chabukswar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
46 views3 pages

Assignment No - 6-1

This document summarizes the use of Naive Bayes classification on the iris dataset. It loads and explores the iris data, splits it into training and test sets, fits a Gaussian Naive Bayes classifier to the training set, makes predictions on the test set, and evaluates the model's performance using a confusion matrix and classification report.

Uploaded by

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

Naive Bayes Classification

In [ ]: # Assignment - A6 | Name :Chabukswar Siddhaarth S. | Roll No : 76

In [41]: # Importing the libraries


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import matplotlib.image as mpimg

In [4]: df = pd.read_csv('iris.csv')
df.head()

Out[4]: sepal_length sepal_width petal_length petal_width species

0 5.1 3.5 1.4 0.2 setosa

1 4.9 3.0 1.4 0.2 setosa

2 4.7 3.2 1.3 0.2 setosa

3 4.6 3.1 1.5 0.2 setosa

4 5.0 3.6 1.4 0.2 setosa

In [10]: %matplotlib inline


img=mpimg.imread('iris_types.jpg')
plt.figure(figsize=(20,40))
plt.axis('off')
plt.imshow(img)

<matplotlib.image.AxesImage at 0x2496367c070>
Out[10]:

In [6]: X = df.iloc[:, :4].values


Y = df['species'].values

In [7]: from sklearn.model_selection import train_test_split


from sklearn.preprocessing import StandardScaler
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
print(f'Train Dataset Size - X: {X_train.shape}, Y: {Y_train.shape}')
print(f'Test Dataset Size - X: {X_test.shape}, Y: {Y_test.shape}')
Loading [MathJax]/extensions/Safe.js
Train Dataset Size - X: (120, 4), Y: (120,)
Test Dataset Size - X: (30, 4), Y: (30,)

In [13]: from sklearn.naive_bayes import GaussianNB


classifier = GaussianNB()
classifier.fit(X_train, Y_train)
predictions = classifier.predict(X_test)
mapper = {'setosa': 0, 'versicolor': 1, 'virginica': 2}
predictions_ = [mapper[i] for i in predictions]
fig, axs = plt.subplots(2, 2, figsize = (12, 10), constrained_layout = True)
fig.suptitle('Regression Line Tracing')
for i in range(4):
x, y = i // 2, i % 2
sns.regplot(x = X_test[:, i], y = predictions_, ax=axs[x, y])
axs[x, y].scatter(X_test[:, i][::-1], Y_test[::-1], marker = '+', color="white")
axs[x, y].set_xlabel(df.columns[i + 1][:-2])

Confusion matrix
In [40]: from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
cm = confusion_matrix(Y_test, predictions)
print(f'''Confusion matrix :\n
| Positive Prediction\t| Negative Prediction
---------------+------------------------+----------------------
Positive Class | True Positive (TP) {cm[0, 0]}\t| False Negative (FN) {cm[0, 1]}
Loading [MathJax]/extensions/Safe.js
---------------+------------------------+----------------------
Negative Class | False Positive (FP) {cm[1, 0]}\t| True Negative (TN) {cm[1, 1]}\n''')
cm = classification_report(Y_test, predictions)
print('Classification report : \n', cm)

Confusion matrix :

| Positive Prediction | Negative Prediction


---------------+------------------------+----------------------
Positive Class | True Positive (TP) 11 | False Negative (FN) 0
---------------+------------------------+----------------------
Negative Class | False Positive (FP) 0 | True Negative (TN) 8

Classification report :
precision recall f1-score support

setosa 1.00 1.00 1.00 11


versicolor 0.89 0.89 0.89 9
virginica 0.90 0.90 0.90 10

accuracy 0.93 30
macro avg 0.93 0.93 0.93 30
weighted avg 0.93 0.93 0.93 30

In [ ]:

Loading [MathJax]/extensions/Safe.js

You might also like