-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
126 lines (109 loc) · 5.44 KB
/
analyzer.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
##
## analyzer.py
##
## Made by xsyann
## Contact <contact@xsyann.com>
##
## Started on Wed Apr 2 14:52:28 2014 xsyann
## Last update Fri Apr 4 19:01:25 2014 xsyann
##
"""
Optical Character Recognition Analyzer.
Authors:
Nicolas PELICAN
Yann KOETH
"""
import timeit
import numpy as np
from collections import Counter
from collections import defaultdict
class Analyzer(object):
def __init__(self, model, dataset, trainRatio):
self.__model = model
self.__dataset = dataset
self.__trainRatio = trainRatio
self.__start = 0
self.__elapsed = 0
self.trainMedian = 0
self.trainMean = 0
self.trainMAD = 0
self.trainVar = 0
self.trainStd = 0
self.trainVarCoeff = 0
def start(self):
self.__start = timeit.default_timer()
def stop(self):
self.__elapsed = timeit.default_timer() - self.__start
def __str__(self):
res = "\tTrain samples\t\tTest samples"
for label, ((tTrain, nTrain, pTrain), (tTest, nTest, pTest)) in sorted(self.classifications.items()):
res += "\n {0}:\t{1} / {2}\t({3} %)\t - {4} / {5}\t({6} %)".format(label,
tTrain, nTrain, pTrain,
tTest, nTest, pTest)
res += "\n\n Best recognized in train set : %s (%d %%)\n" % self.maxTrain
res += " Worst recognized in train set : %s (%d %%)\n" % self.minTrain
res += "\n Best recognized in test set : %s (%d %%)\n" % self.maxTest
res += " Worst recognized in test set : %s (%d %%)\n" % self.minTest
res += '\n ------------------\n'
res += '| Training samples |\n'
res += ' ------------------\n'
res += " Mean : %d\n" % self.trainMean
res += " Median : %d\n" % self.trainMedian
res += " Median absolute deviation : %.2f\n" % self.trainMAD
# res += "Variance : %2.f\n" % self.trainVar
res += " Standard deviation : %.2f\n" % self.trainStd
res += " Coefficient of variation : %.2f %%\n" % self.trainVarCoeff
res += '\nTrain set: %d samples | Test set: %d samples\n' % (self.trainCount, self.testCount)
res += 'Training time: %.4f s\n' % self.__elapsed
res += '\nTrain accuracy: %.2f %% | Test accuracy %.2f %%\n' % (self.trainRate * 100, self.testRate * 100)
return res
def __mapDict(self, dict, func):
res = func(dict, key=dict.get)
return (res, dict[res])
def analyze(self):
"""Analyze dataset repartition and model performance.
"""
self.trainCount = self.__dataset.trainSampleCount
self.testCount = self.__dataset.testSampleCount
trainSamples, trainResponses = self.__dataset.trainSamples, self.__dataset.trainResponses
testSamples, testResponses = self.__dataset.testSamples, self.__dataset.testResponses
truthTableTrain, self.trainRate = (defaultdict(int), 0)
truthTableTest, self.testRate = (defaultdict(int), 0)
if 0 < self.__trainRatio:
truthTableTrain, self.trainRate = self.__analyzePredict(trainSamples, trainResponses)
if 0 < self.__trainRatio < 1:
truthTableTest, self.testRate = self.__analyzePredict(testSamples, testResponses)
countTrain = Counter([self.__dataset.getResponse(i) for i in trainResponses])
countTest = Counter([self.__dataset.getResponse(i) for i in testResponses])
# Create a dict { label1: (train, test), label2: ... }
# where train = (wellPredicted, total, percentage)
# and test = (wellPredicted, total, percentage)
trainPercents, testPercents = {}, {}
self.classifications = {}
for k in list(set(countTrain.keys()) | set(countTest.keys())):
trainPercents[k] = int(truthTableTrain[k] / float(countTrain[k]) * 100) if countTrain[k] > 0 else 100
testPercents[k] = int(truthTableTest[k] / float(countTest[k]) * 100) if countTest[k] > 0 else 100
self.classifications[k] = ((truthTableTrain[k], countTrain[k], trainPercents[k]),
(truthTableTest[k], countTest[k], testPercents[k]))
self.minTrain, self.maxTrain = self.__mapDict(trainPercents, min), self.__mapDict(trainPercents, max)
self.minTest, self.maxTest = self.__mapDict(testPercents, min), self.__mapDict(testPercents, max)
self.__analyzeTrainingSamples([v for k, v in countTrain.iteritems()])
def __analyzeTrainingSamples(self, trainingSamples):
"""Analyze training samples distribution.
"""
if trainingSamples:
self.trainMedian = np.median(trainingSamples)
self.trainMean = np.mean(trainingSamples)
self.trainMAD = np.median(np.absolute(trainingSamples - self.trainMedian))
self.trainVar = np.var(trainingSamples)
self.trainStd = np.std(trainingSamples)
if np.mean(trainingSamples) > 0:
self.trainVarCoeff = (self.trainStd / self.trainMean * 100)
def __analyzePredict(self, samples, responses):
"""Analyze the rate of true predictions.
"""
predict = self.__model.predict(samples)
trueTable = (predict == responses)
truePredict = Counter([self.__dataset.getResponse(c) for i, c in enumerate(predict) if trueTable[i]])
rate = np.mean(predict == responses)
return truePredict, rate