-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_file.py
609 lines (377 loc) · 16.4 KB
/
testing_file.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 17:28:10 2023
@author: Hamza
"""
import pickle
import cv2
import numpy as np
import os
from glob import glob
import matplotlib.pyplot as plt
#lib for lazy
from lazypredict.Supervised import LazyClassifier
#from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
import matplotlib.pyplot as plt
import numpy
from sklearn import metrics
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.calibration import CalibratedClassifierCV
import lightgbm as lgb
from sklearn.linear_model import RidgeClassifier
from sklearn.metrics import confusion_matrix
import cv2
import numpy as np
import os
from glob import glob
import matplotlib.pyplot as plt
#lib for lazy
from lazypredict.Supervised import LazyClassifier
#from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import time
import datetime
import numpy as np
#z=np.load('C:/Users/Hamza/Desktop/dtsavedfiles/label5k.npy'
import numpy as np
from skimage.feature import hog
# Load the numpy file containing the images
#images = np.load('C:/Users/Hamza/Desktop/saved_dataset/image5k.npy')
images = np.load('C:/Users/Hamza/Desktop/experiments/expl.npy')
# Initialize an empty list to store the HOG features
hog_features = []
# Iterate through the images
for image in images:
# Apply HOG to the current image and append the resulting feature vector to the list
hog_features.append(hog(image, channel_axis=2))
#fd, hog_image = hog(image, visualise = True)
X=np.array(hog_features)
#y=np.load('C:/Users/Hamza/Desktop/saved_dataset/label5k.npy')
y=np.load('C:/Users/Hamza/Desktop/experiments/expi.npy')
start=time.time()
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.2,random_state =123)
lda=LinearDiscriminantAnalysis()
lda.fit(X_train,y_train)
accuracy=lda.score(X_test,y_test)
print("Accuracy of LDA: {:.2f}%".format(accuracy * 100))
with open('lda.pkl', 'wb') as file:
model=pickle.dump(lda, file)
y_pred = lda.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_lda = plt.gcf()
confusion_matrix_plot = fig_lda
plt.show()
rf=RandomForestClassifier()
rf.fit(X_train,y_train)
accuracy=rf.score(X_test,y_test)
print("Accuracy of random forest: {:.2f}%".format(accuracy * 100))
with open('random_forest.pkl', 'wb') as file:
model=pickle.dump(rf, file)
y_pred = rf.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_rf = plt.gcf()
confusion_matrix_plot = fig_rf
plt.show()
sv=svm.SVC(kernel='sigmoid')
sv.fit(X_train,y_train)
accuracy=sv.score(X_test,y_test)
print("Accuracy of svc: {:.2f}%".format(accuracy * 100))
with open('svc.pkl', 'wb') as file:
model=pickle.dump(sv, file)
y_pred = sv.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_svm = plt.gcf()
confusion_matrix_plot = fig_svm
plt.show()
dt=DecisionTreeClassifier()
dt.fit(X_train,y_train)
accuracy=dt.score(X_test,y_test)
print("Accuracy of decision tree: {:.2f}%".format(accuracy * 100))
with open('decisionTree.pkl', 'wb') as file:
model=pickle.dump(dt, file)
y_pred = dt.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_dt = plt.gcf()
confusion_matrix_plot = fig_dt
plt.show()
knn = KNeighborsClassifier(n_neighbors=5)
# Train the classifier on the training data
knn.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = knn.score(X_test, y_test)
print("Accuracy of KNN: {:.2f}%".format(accuracy * 100))
with open('knn.pkl', 'wb') as file:
model=pickle.dump(knn, file)
y_pred = knn.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_knn = plt.gcf()
confusion_matrix_plot = fig_knn
plt.show()
gnb = GaussianNB()
# Train the classifier on the training data
gnb.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = gnb.score(X_test, y_test)
print("Accuracy of guassian NB: {:.2f}%".format(accuracy * 100))
with open('GuassianNB.pkl', 'wb') as file:
model=pickle.dump(gnb, file)
y_pred = gnb.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_gnb = plt.gcf()
confusion_matrix_plot = fig_gnb
plt.show()
prcp = Perceptron()
# Train the classifier on the training data
prcp.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = prcp.score(X_test, y_test)
print("Accuracy of perceptron: {:.2f}%".format(accuracy * 100))
with open('perceptron.pkl', 'wb') as file:
model=pickle.dump(prcp, file)
y_pred = prcp.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_prcp = plt.gcf()
confusion_matrix_plot = fig_prcp
plt.show()
lr = LogisticRegression()
# Train the model on the training data
lr.fit(X_train, y_train)
# Evaluate the model on the test data
accuracy = lr.score(X_test, y_test)
print("Accuracy of logistic regression : {:.2f}%".format(accuracy * 100))
with open('logistic_regression.pkl', 'wb') as file:
model=pickle.dump(lr, file)
y_pred = lr.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_lr = plt.gcf()
confusion_matrix_plot = fig_lr
plt.show()
sgdc = SGDClassifier(loss='log', max_iter=1000, tol=1e-3, random_state=42)
# Train the model on the training data
sgdc.fit(X_train, y_train)
# Evaluate the model on the test data
accuracy = sgdc.score(X_test, y_test)
print("Accuracy of SGDC: {:.2f}%".format(accuracy * 100))
with open('sgdc.pkl', 'wb') as file:
model=pickle.dump(sgdc, file)
y_pred = sgdc.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_sgdc = plt.gcf()
confusion_matrix_plot = fig_sgdc
plt.show()
nc = NearestCentroid()
# Train the classifier on the training data
nc.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = nc.score(X_test, y_test)
print("Accuracy of nearest centroid: {:.2f}%".format(accuracy * 100))
with open('nearest_centroid.pkl', 'wb') as file:
model=pickle.dump(nc, file)
y_pred = nc.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_nc = plt.gcf()
confusion_matrix_plot = fig_nc
plt.show()
pac = PassiveAggressiveClassifier()
# Train the classifier on the training data
pac.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = pac.score(X_test, y_test)
print("Accuracy of passive aggressive classifier: {:.2f}%".format(accuracy * 100))
with open('passive_ac.pkl', 'wb') as file:
model=pickle.dump(pac, file)
y_pred = pac.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_pac = plt.gcf()
confusion_matrix_plot = fig_pac
plt.show()
bnb = BernoulliNB()
# Train the classifier on the training data
bnb.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = bnb.score(X_test, y_test)
print("Accuracy of bernoulliNB: {:.2f}%".format(accuracy * 100))
with open('bernoulli.pkl', 'wb') as file:
model=pickle.dump(bnb, file)
y_pred = bnb.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_bnb = plt.gcf()
confusion_matrix_plot = fig_bnb
plt.show()
rc = RidgeClassifier()
# Train the classifier on the training data
rc.fit(X_train, y_train)
# Evaluate the classifier on the test data
accuracy = rc.score(X_test, y_test)
print("Accuracy of ridge classifier: {:.2f}%".format(accuracy * 100))
with open('ridge.pkl', 'wb') as file:
model=pickle.dump(rc, file)
y_pred = rc.predict(X_test)
#y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig_rc = plt.gcf()
confusion_matrix_plot = fig_rc
plt.show()
#%%
from sklearn.metrics import confusion_matrix
y_pred = rf.predict(X_test)
y_pred= np.expand_dims(y_pred, axis=-1)
conf_matrix = confusion_matrix(y_test, y_pred)
print(conf_matrix)
#This code will predict the labels for the test data using the trained Random Forest model and then calculate the confusion matrix by comparing the predicted labels with the true labels stored in y_test. You can also use other visualization libraries like matplotlib or seaborn to plot the confusion matrix in a more graphical format.
import matplotlib.pyplot as plt
plt.imshow(conf_matrix, cmap='Blues')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the figure
fig = plt.gcf()
confusion_matrix_plot = fig
plt.show()
'''
from sklearn.metrics import classification_report
y_pred = rf.predict(X_test)
y_pred= np.expand_dims(y_pred, axis=-1)
print(classification_report(y_test, y_pred))
accuracy = (conf_matrix[0][0] + conf_matrix[1][1]) / conf_matrix.sum()
print("Accuracy: {:.2f}%".format(accuracy * 100))
precision = conf_matrix[0][0] / (conf_matrix[0][0] + conf_matrix[0][1])
recall = conf_matrix[0][0] / (conf_matrix[0][0] + conf_matrix[1][0])
f1_score = 2 * (precision * recall) / (precision + recall)
print("Precision: {:.2f}%".format(precision * 100))
print("Recall: {:.2f}%".format(recall * 100))
print("F1-Score: {:.2f}%".format(f1_score * 100))
'''