-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfusionMatrix.py
62 lines (49 loc) · 2.3 KB
/
confusionMatrix.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
import h5py
import os
import pickle
from matplotlib import pyplot as plt
from tensorflow import keras
import h5py
import numpy as np
def printConfusionMatrix(kerasModel, datagroup, dataset):
predictions = kerasModel.predict(dataset[0]) # feed the numpy arrays to predict
confMatrix = np.zeros([2, 2]) # rows are actual values
for i in range(len(predictions)): # cols are predictions
pred = predictions[i]
lbl = dataset[1][i]
# print(lbl, pred)
if lbl == 0: # a row of true backgrounds
if pred < 0.5:
confMatrix[0, 0] += 1 # True negative
else:
confMatrix[0, 1] += 1 # False negative
if lbl == 1: # a row of true signals
if pred < 0.5:
confMatrix[1, 0] += 1 # False positive
else:
confMatrix[1, 1] += 1 # True positive
print("Confusion matrix fractions for predictions on dataset", datagroup, "of length", len(dataset[0]))
confMatrix = confMatrix / np.sum(confMatrix) # normalize to fractions
print(' PREDICT 0 1')
print(' Label = 0 TN {:0.3f} FN {:0.3f}'.format(confMatrix[0,0], confMatrix[0,1]))
print(' Label = 1 FP {:0.3f} TP {:0.3f}'.format(confMatrix[1,0], confMatrix[1,1]))
#################################################################
baseDir = "/home/val/PycharmProjects/github/signal-classifier/modelOutput/"
classifierModelFilename = "models/Classifier_0_10_epochs"
classifier_Model = keras.models.load_model(classifierModelFilename, compile = False)
h5filename = "h5fileHWcalls_1110records.h5"
#h5filename = "/home/val/PycharmProjects/github/signal-annotation/h5fakeSpecs.h5"
h5db = h5py.File(h5filename, mode='r')
train_specs = h5db['train_specs']
train_labels = h5db['train_labels']
test_specs = h5db['test_specs']
test_labels = h5db['test_labels']
eval_specs = h5db['eval_specs']
eval_labels = h5db['eval_labels']
#
# print("num of eval_specs records is", eval_specs.shape[0])
print("Classifier Model:")
print(classifier_Model.summary())
printConfusionMatrix(classifier_Model, "train dataset", (train_specs, train_labels))
printConfusionMatrix(classifier_Model, "test dataset", (test_specs, test_labels))
printConfusionMatrix(classifier_Model, "eval dataset", (eval_specs, eval_labels))