You are on page 1of 6

Intelligent Systems Lab Examination

Name- Aman Agarwal


Section C
Roll No. 19
Reg No. 201800119
7th Semester

Question Set: 4
Question: Write a program to implement the Naïve Bayesian
classifier for a sample training data set stored as a
wbdc_NB.CSV file. Compute the accuracy of the classifier.

Solution:
Algorithm:
Step 1: Start
Step 2: Importing the libraries
Step 3: Importing the dataset
Step 4: Splitting the dataset into the Training set and Test
set
Step 5: Feature Scaling
Step 6: Training the Naive Bayes model on the Trainingset
Step 7: Predicting the Test set results
Step 8: Making the Confusion Matrix
Step 9: Expected Output
Step 10: End

Source Code:
# Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

from sklearn.metrics import plot_confusion_matrix

# Importing the dataset

dataset = pd.read_excel('wbdc_NB.xlsx')

X = dataset.iloc[:, [0, 29]].values

y = dataset.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)

# Feature Scaling

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

X_train = sc.fit_transform(X_train)

X_test = sc.transform(X_test)

# Training the Naive Bayes model on the Training set

from sklearn.naive_bayes import GaussianNB

classifier = GaussianNB()

classifier.fit(X_train, y_train)
# Predicting the Test set results

y_pred = classifier.predict(X_test)

# Making the Confusion Matrix

from sklearn.metrics import confusion_matrix, accuracy_score

ac = accuracy_score(y_test,y_pred)

cm = confusion_matrix(y_test, y_pred)

print("Accuarcy: ",ac,"\n")

print("Confusion Matrix: ")

sns.heatmap(cm, annot=True)
Output:
Accuarcy: 0.9122807017543859

Confusion Matrix:
<matplotlib.axes._subplots.AxesSubplot at 0x7fbd117b6a50>

You might also like