9. Develop a program to implement the Naive Bayesian classifier considering Olivetti Face Data set for training. Compute the accuracy of the classifier, considering a few test data sets.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
faces = fetch_olivetti_faces(shuffle=True, random_state=42)
X, y = faces.data, faces.target
fig, axes = plt.subplots(2, 5, figsize=(10, 5))
for i, ax in enumerate(axes.flat):
ax.imshow(X[i].reshape(64, 64), cmap='gray')
ax.set_title(f"Person {y[i]}")
ax.axis('off')
plt.suptitle("Sample Images from Olivetti Faces Dataset")
plt.show()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
model = GaussianNB()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"\nNaive Bayes Classifier Accuracy: {accuracy:.2f}")
print("\nClassification Report:\n", classification_report(y_test, y_pred))
fig, axes = plt.subplots(2, 5, figsize=(10, 5))
for i, ax in enumerate(axes.flat):
ax.imshow(X_test[i].reshape(64, 64), cmap='gray')
ax.set_title(f"Pred: {y_pred[i]}\nActual: {y_test[i]}")
ax.axis('off')
plt.suptitle("Predictions on Test Images")
plt.show()
Naive Bayes Classifier Accuracy: 0.91
Classification Report:
precision recall f1-score support
0 1.00 0.50 0.67 2
1 1.00 0.50 0.67 2
2 0.50 0.50 0.50 2
3 0.50 1.00 0.67 2
4 1.00 1.00 1.00 2
5 1.00 1.00 1.00 2
6 1.00 1.00 1.00 2
7 0.67 1.00 0.80 2
8 1.00 1.00 1.00 2
9 1.00 0.50 0.67 2
10 1.00 1.00 1.00 2
11 1.00 1.00 1.00 2
12 1.00 0.50 0.67 2
13 1.00 1.00 1.00 2
14 1.00 1.00 1.00 2
15 1.00 1.00 1.00 2
16 0.50 1.00 0.67 2
17 1.00 1.00 1.00 2
18 1.00 1.00 1.00 2
19 1.00 1.00 1.00 2
20 1.00 1.00 1.00 2
21 1.00 1.00 1.00 2
22 1.00 1.00 1.00 2
23 1.00 1.00 1.00 2
24 1.00 1.00 1.00 2
25 1.00 0.50 0.67 2
26 1.00 1.00 1.00 2
27 1.00 1.00 1.00 2
28 1.00 1.00 1.00 2
29 1.00 1.00 1.00 2
30 1.00 1.00 1.00 2
31 1.00 0.50 0.67 2
32 1.00 1.00 1.00 2
33 1.00 1.00 1.00 2
34 1.00 1.00 1.00 2
35 1.00 1.00 1.00 2
36 1.00 1.00 1.00 2
37 1.00 1.00 1.00 2
38 1.00 1.00 1.00 2
39 0.67 1.00 0.80 2
accuracy 0.91 80
macro avg 0.95 0.91 0.91 80
weighted avg 0.95 0.91 0.91 80