lab9

 import numpy as np

from sklearn.datasets import fetch_olivetti_faces

from sklearn.model_selection import train_test_split, cross_val_score

from sklearn.naive_bayes import GaussianNB

from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

import matplotlib.pyplot as plt


data = fetch_olivetti_faces(shuffle=True, random_state=42)

X, y = data.data, data.target


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)


gnb = GaussianNB()

gnb.fit(X_train, y_train)

y_pred = gnb.predict(X_test)


print(f'Accuracy: {accuracy_score(y_test, y_pred) * 100:.2f}%')

print("\nClassification Report:")

print(classification_report(y_test, y_pred, zero_division=0))

print("\nConfusion Matrix:")

print(confusion_matrix(y_test, y_pred))


missing_labels = set(np.unique(y_test)) - set(np.unique(y_pred))

if missing_labels:

    print(f"\n⚠️ Labels not predicted at all: {missing_labels}")


cv_accuracy = cross_val_score(gnb, X, y, cv=5).mean()

print(f'\nCross-validation accuracy: {cv_accuracy * 100:.2f}%')


fig, axes = plt.subplots(3, 5, figsize=(12, 8))

for ax, image, label, prediction in zip(axes.ravel(), X_test, y_test, y_pred):

    ax.imshow(image.reshape(64, 64), cmap=plt.cm.gray)

    ax.set_title(f"True: {label}, Pred: {prediction}")

    ax.axis('off')


plt.tight_layout()

plt.show()


Comments

Popular posts from this blog

lab2

lab10