Mercurial Hosting > traffic-intelligence
diff python/ml.py @ 961:ec1682ed999f
added computation of confusion matrix and improved default parameter for block normalization for SVM classification
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 05 Nov 2017 23:45:47 -0500 |
parents | 989917b1ed85 |
children | 184f1dd307f9 |
line wrap: on
line diff
--- a/python/ml.py Fri Oct 20 16:59:32 2017 -0400 +++ b/python/ml.py Sun Nov 05 23:45:47 2017 -0500 @@ -19,6 +19,14 @@ # OpenCV ML models ##################### +def computeConfusionMatrix(model, samples, responses): + 'computes the confusion matrix of the classifier (model)' + classifications = {} + for x,y in zip(samples, responses): + predicted = model.predict(x) + classifications[(y, predicted)] = classifications.get((y, predicted), 0)+1 + return classifications + class StatModel(object): '''Abstract class for loading/saving model''' def load(self, filename): @@ -46,8 +54,10 @@ # self.model.setNu(nu) # self.model.setP(p) - def train(self, samples, responses): + def train(self, samples, responses, computePerformance = False): self.model.train(samples, responses, params = self.params) + if computePerformance: + return computeConfusionMatrix(self, samples, responses) def predict(self, hog): return self.model.predict(hog)