-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
92 lines (63 loc) · 2.28 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1VoCHyZAfikFTcceEcwLmU6CHPdRoOC7n
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from keras.datasets import cifar10
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import confusion_matrix
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout
from keras.models import Sequential, load_model
from keras.callbacks import EarlyStopping
#loading the CIFAR-10 dataset
from keras.datasets import cifar10
train, test = cifar10.load_data()
x_train=train[0]
y_train=train[1]
x_test=test[0]
y_test=test[1]
from PIL import Image
#concatenating the train and test data in order to split the data according to our liking
X=np.concatenate((x_train,x_test))
m=60000
Y=np.concatenate((y_train,y_test))
#splitting data
i = int((0.8) * X.shape[0])
o = np.random.permutation(X.shape[0])
train_x, test_x = np.split(np.take(X,o,axis=0), [i])
train_y,test_y = np.split(np.take(Y,o), [i])
plt.imshow(train_x[0])
train_x.shape
test_x.shape
train_y.shape
test_y.shape
#grayscaling the image
x_train=np.array([cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) for image in train_x])
x_test=np.array([cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) for image in test_x])
x_train.shape
labels = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
#Resizing the image
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
x_train.shape
#reshape to 2d array
nsamples, nx, ny, nrgb = x_train.shape
x_train2 = x_train.reshape((nsamples,nx*ny*nrgb))
#reshaping the test data to 2d array
nsamples, nx, ny, nrgb = x_test.shape
x_test2 = x_test.reshape((nsamples,nx*ny*nrgb))
#applying the naive bayes model on the data
from sklearn.naive_bayes import GaussianNB
nb=GaussianNB()
nb.fit(x_train2,train_y)
y_pred_nb=nb.predict(x_test2)
y_pred_nb
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
accuracy_score(y_pred_nb,test_y)
print(classification_report(y_pred_nb,test_y))
"""Here we are getting an accuracy of 27%."""
confusion_matrix(y_pred_nb,test_y)