Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 960:0c1d1eeef544 | 961:ec1682ed999f |
|---|---|
| 16 import utils | 16 import utils |
| 17 | 17 |
| 18 ##################### | 18 ##################### |
| 19 # OpenCV ML models | 19 # OpenCV ML models |
| 20 ##################### | 20 ##################### |
| 21 | |
| 22 def computeConfusionMatrix(model, samples, responses): | |
| 23 'computes the confusion matrix of the classifier (model)' | |
| 24 classifications = {} | |
| 25 for x,y in zip(samples, responses): | |
| 26 predicted = model.predict(x) | |
| 27 classifications[(y, predicted)] = classifications.get((y, predicted), 0)+1 | |
| 28 return classifications | |
| 21 | 29 |
| 22 class StatModel(object): | 30 class StatModel(object): |
| 23 '''Abstract class for loading/saving model''' | 31 '''Abstract class for loading/saving model''' |
| 24 def load(self, filename): | 32 def load(self, filename): |
| 25 if path.exists(filename): | 33 if path.exists(filename): |
| 44 # self.model.setCoef0(coef0) | 52 # self.model.setCoef0(coef0) |
| 45 # self.model.setC(Cvalue) | 53 # self.model.setC(Cvalue) |
| 46 # self.model.setNu(nu) | 54 # self.model.setNu(nu) |
| 47 # self.model.setP(p) | 55 # self.model.setP(p) |
| 48 | 56 |
| 49 def train(self, samples, responses): | 57 def train(self, samples, responses, computePerformance = False): |
| 50 self.model.train(samples, responses, params = self.params) | 58 self.model.train(samples, responses, params = self.params) |
| 59 if computePerformance: | |
| 60 return computeConfusionMatrix(self, samples, responses) | |
| 51 | 61 |
| 52 def predict(self, hog): | 62 def predict(self, hog): |
| 53 return self.model.predict(hog) | 63 return self.model.predict(hog) |
| 54 | 64 |
| 55 | 65 |
