0% found this document useful (0 votes)
8 views

Program -9

The document describes a program that implements a Naive Bayesian classifier using the Olivetti Face Data set for training and evaluates its accuracy on test data. The classifier achieved an accuracy of 80.83% and a cross-validation accuracy of 87.25%. Additionally, it includes a classification report and confusion matrix for further analysis of the model's performance.

Uploaded by

sadiqhamuskan4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Program -9

The document describes a program that implements a Naive Bayesian classifier using the Olivetti Face Data set for training and evaluates its accuracy on test data. The classifier achieved an accuracy of 80.83% and a cross-validation accuracy of 87.25%. Additionally, it includes a classification report and confusion matrix for further analysis of the model's performance.

Uploaded by

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

Program 9

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.

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 = data.data
y = 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)

accuracy = accuracy_score(y_test, y_pred)


print(f'Accuracy: {accuracy * 100:.2f}%')

print("\nClassification Report:")
print(classification_report(y_test, y_pred, zero_division=1))

print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))

cross_val_accuracy = cross_val_score(gnb, X, y, cv=5, scoring='accuracy')


print(f'\nCross-validation accuracy: {cross_val_accuracy.mean() * 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.show()
output :

Accuracy: 80.83%

Classification Report:
precision recall f1-score support

0 0.67 1.00 0.80 2


1 1.00 1.00 1.00 2
2 0.33 0.67 0.44 3
3 1.00 0.00 0.00 5
4 1.00 0.50 0.67 4
5 1.00 1.00 1.00 2
7 1.00 0.75 0.86 4
8 1.00 0.67 0.80 3
9 1.00 0.75 0.86 4
10 1.00 1.00 1.00 3
11 1.00 1.00 1.00 1
12 0.40 1.00 0.57 4
13 1.00 0.80 0.89 5
14 1.00 0.40 0.57 5
15 0.67 1.00 0.80 2
16 1.00 0.67 0.80 3
17 1.00 1.00 1.00 3
18 1.00 1.00 1.00 3
19 0.67 1.00 0.80 2
20 1.00 1.00 1.00 3
21 1.00 0.67 0.80 3
22 1.00 0.60 0.75 5
23 1.00 0.75 0.86 4
24 1.00 1.00 1.00 3
25 1.00 0.75 0.86 4
26 1.00 1.00 1.00 2
27 1.00 1.00 1.00 5
28 0.50 1.00 0.67 2
29 1.00 1.00 1.00 2
30 1.00 1.00 1.00 2
31 1.00 0.75 0.86 4
32 1.00 1.00 1.00 2
34 0.25 1.00 0.40 1
35 1.00 1.00 1.00 5
36 1.00 1.00 1.00 3
37 1.00 1.00 1.00 1
38 1.00 0.75 0.86 4
39 0.50 1.00 0.67 5

accuracy 0.81 120


macro avg 0.89 0.85 0.83 120
weighted avg 0.91 0.81 0.81 120

Confusion Matrix:
[[2 0 0 ... 0 0 0]
[0 2 0 ... 0 0 0]
[0 0 2 ... 0 0 1]
...
[0 0 0 ... 1 0 0]
[0 0 0 ... 0 3 0]
[0 0 0 ... 0 0 5]]

Cross-validation accuracy: 87.25%


downloading Olivetti faces from https://ndownloader.figshare.com/files/5976027 to C:\
Users\gecrcse\scikit_learn_data
Traceback (most recent call last):

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 1346, in do_open


h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 1285, in request
self._send_request(method, url, body, headers, encode_chunked)

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 1331, in _send_request


self.endheaders(body, encode_chunked=encode_chunked)

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 1280, in endheaders


self._send_output(message_body, encode_chunked=encode_chunked)

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 1040, in _send_output


self.send(msg)

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 980, in send


self.connect()

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 1447, in connect


super().connect()

File "C:\Users\gecrcse\anaconda3\gec\lib\http\client.py", line 946, in connect


self.sock = self._create_connection(

File "C:\Users\gecrcse\anaconda3\gec\lib\socket.py", line 823, in create_connection


for res in getaddrinfo(host, port, 0, SOCK_STREAM):

File "C:\Users\gecrcse\anaconda3\gec\lib\socket.py", line 954, in getaddrinfo


for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\Users\gecrcse\.spyder-py3\untitled1.py", line 7, in <module>


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

File "C:\Users\gecrcse\anaconda3\gec\lib\site-packages\sklearn\datasets\
_olivetti_faces.py", line 115, in fetch_olivetti_faces
mat_path = _fetch_remote(FACES, dirname=data_home)

File "C:\Users\gecrcse\anaconda3\gec\lib\site-packages\sklearn\datasets\_base.py",
line 1454, in _fetch_remote
urlretrieve(remote.url, file_path)

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 239, in urlretrieve


with contextlib.closing(urlopen(url, data)) as fp:

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 214, in urlopen


return opener.open(url, data, timeout)

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 517, in open


response = self._open(req, data)
File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 494, in _call_chain


result = func(*args)

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 1389, in https_open


return self.do_open(http.client.HTTPSConnection, req,

File "C:\Users\gecrcse\anaconda3\gec\lib\urllib\request.py", line 1349, in do_open


raise URLError(err)

URLError: <urlopen error [Errno 11001] getaddrinfo failed>

You might also like