-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfusionMatrix.py
69 lines (53 loc) · 2.06 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
62
63
64
65
# importing the dependencies
#import the system module for standered input output
import sys
from keras.models import load_model
import numpy as np
from sklearn import metrics
# ***************Main Code ************************************
#set the threshhold of prinitng data to console to maximum value
#so avoid the loss of data on console while displaying
np.set_printoptions(threshold=sys.maxsize)
#loading the pretrained model
model = load_model('model.h5')
#loading the testData and groundTruth data
testData = np.load('testData.npy')
groundTruth = np.load('groundTruth.npy')
# TEST
# print(testData)
# print(groundTruth)
# defining the class labels
#labelsRow = np.array(['Fault1','Fault2','Fault3','Fault4','No Operation','Normal'])
#labelsColumn = np.array(['Fault1','Fault2','Fault3','Fault4','No Operation','Normal',' '])
labelsRow = np.array(['Fault1','Fault2','Fault3','Normal'])
labelsColumn = np.array(['Fault1','Fault2','Fault3','Normal',' '])
# predicting the classes
predictions = model.predict(testData,verbose=2)
# TEST
# print(predictions)
# getting the class predicted and class in ground truth for creation of confusion matrix
predictedClass = np.zeros((predictions.shape[0]))
# TEST
# print(predictedClass)
groundTruthClass = np.zeros((groundTruth.shape[0]))
# TEST
# print(groundTruthClass)
for case in range (groundTruth.shape[0]):
predictedClass[case] = np.argmax(predictions[case,:])
groundTruthClass[case] = np.argmax(groundTruth[case,:])
# TEST
# print(predictedClass)
# print(groundTruthClass)
# obtaining a confusion matrix
ConfusionMatrix = metrics.confusion_matrix(groundTruthClass,predictedClass)
# TEST
# print(ConfusionMatrix)
# Calculate the normalized confusion matrix
cmNormalize = np.around((ConfusionMatrix/ConfusionMatrix.sum(axis=1)[:,None])*100,2)
#print the normalized confusion marix
print(cmNormalize)
# Add labels to row and column
cmNormalizeFinal = np.row_stack((cmNormalize ,labelsRow))
cmNormalizeFinal = np.column_stack((cmNormalizeFinal ,labelsColumn))
#print final confusion matrix
print(cmNormalizeFinal)