-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
74 lines (58 loc) · 2.17 KB
/
metrics.py
File metadata and controls
74 lines (58 loc) · 2.17 KB
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
import numpy as np
import matplotlib.pyplot as plt
def matrizConfusion(evData, classNameDict, model):
values = evData[:,-1].astype(np.int)
countPerValue = np.bincount(values)
existingClasses = np.nonzero(countPerValue)[0]
countPerValue = countPerValue[existingClasses]
# existingClasses = map(lambda x: classNameDict[x], existingClasses)
accuracyCounter = [0 for i in range(1, len(classNameDict)+1)]
confussionMatrix = {
i+1: { it+1:0 for it in range(len(classNameDict)) } for i in range(len(classNameDict))
}
# Evaluate model
for elem in evData:
res = model.classify(elem[:-1])
confussionMatrix[elem[-1]][res] += 1
# if res == elem[-1]:
# accuracyCounter[int(elem[-1]-1)] += 1
print(confussionMatrix)
print('|-|', end="")
for i in range(len(classNameDict)):
print(classNameDict[i+1], "|", end="")
print()
print('|---:|', end="")
for i in range(len(classNameDict)):
print('---:|', end="")
print()
for row in confussionMatrix:
print('|', classNameDict[row], '|', end='')
for col in confussionMatrix[row]:
print(confussionMatrix[row][col], '|', end="")
print()
# accuracyPercentages = {}
# for ind, res in enumerate(accuracyCounter):
# accuracyPercentages[ind+1] = res*100/countPerValue[ind]
# print('|Class|Count|Count Predicted|Percentage Predicted|')
# print('|----:|----:|--------------:|-------------------:|')
# for i, c in enumerate(accuracyCounter):
# print('|', classNameDict[i+1], '|', countPerValue[i], '|', c, '|', accuracyPercentages[i+1], '%', '|')
# print('Cleaning: ...')
# print(cleanTree(model))
def isLeaf(root):
return not root.false_branch and not root.true_branch
def MCV(root):
return root.mostCommonValue
def cleanTree(root):
if (isLeaf(root)):
return root
root.false_branch = cleanTree(root.false_branch)
root.true_branch = cleanTree(root.true_branch)
if (isLeaf(root.false_branch) and isLeaf(root.true_branch)):
if (
root.percentage == root.false_branch.percentage == root.true_branch.percentage
and MCV(root) == MCV(root.false_branch) == MCV(root.true_branch)
):
root.false_branch = None
root.true_branch = None
return root