Professional Documents
Culture Documents
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 pandas as pd
dataset = pd.read_excel('wbdc_NB.xlsx')
y = dataset.iloc[:, -1].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
classifier = GaussianNB()
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
ac = accuracy_score(y_test,y_pred)
cm = confusion_matrix(y_test, y_pred)
print("Accuarcy: ",ac,"\n")
sns.heatmap(cm, annot=True)
Output:
Accuarcy: 0.9122807017543859
Confusion Matrix:
<matplotlib.axes._subplots.AxesSubplot at 0x7fbd117b6a50>