Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 380:adfd4f70ee1d
added SVM
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 19 Jul 2013 09:11:57 -0400 |
| parents | 6c068047edbf |
| children | ba813f148ade |
comparison
equal
deleted
inserted
replaced
| 379:f1a1923ddff9 | 380:adfd4f70ee1d |
|---|---|
| 2 '''Libraries for machine learning algorithms''' | 2 '''Libraries for machine learning algorithms''' |
| 3 | 3 |
| 4 import numpy as np | 4 import numpy as np |
| 5 | 5 |
| 6 __metaclass__ = type | 6 __metaclass__ = type |
| 7 | |
| 8 class Model(object): | |
| 9 '''Abstract class for loading/saving model''' | |
| 10 def load(self, fn): | |
| 11 self.model.load(fn) | |
| 12 | |
| 13 def save(self, fn): | |
| 14 self.model.save(fn) | |
| 15 | |
| 16 class SVM(Model): | |
| 17 '''wrapper for OpenCV SimpleVectorMachine algorithm''' | |
| 18 | |
| 19 def __init__(self, svm_type, kernel_type, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): | |
| 20 import cv2 | |
| 21 self.model = cv2.SVM() | |
| 22 self.params = dict(svm_type = svm_type, kernel_type = kernel_type, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p) | |
| 23 | |
| 24 def train(self, samples, responses): | |
| 25 self.model.train(samples, responses, params = self.params) | |
| 26 | |
| 27 def predict(self, sample): | |
| 28 return np.float32(self.model.predict(s)) | |
| 29 | |
| 7 | 30 |
| 8 class Centroid: | 31 class Centroid: |
| 9 'Wrapper around instances to add a counter' | 32 'Wrapper around instances to add a counter' |
| 10 | 33 |
| 11 def __init__(self, instance, nInstances = 1): | 34 def __init__(self, instance, nInstances = 1): |
