Calls libsvm.svm_train() to create a model. Vector classes and features are mapped to integers.
(self)
| 2255 | sv = support_vectors |
| 2256 | |
| 2257 | def _train(self): |
| 2258 | """ Calls libsvm.svm_train() to create a model. |
| 2259 | Vector classes and features are mapped to integers. |
| 2260 | """ |
| 2261 | # Note: LIBLINEAR feature indices start from 1 (not 0). |
| 2262 | M = [v for type, v in self._vectors] # List of vectors. |
| 2263 | H1 = dict((w, i+1) for i, w in enumerate(self.features)) # Feature => integer hash. |
| 2264 | H2 = dict((w, i+1) for i, w in enumerate(self.classes)) # Class => integer hash. |
| 2265 | H3 = dict((i+1, w) for i, w in enumerate(self.classes)) # Class reversed hash. |
| 2266 | x = map(lambda v: dict(map(lambda k: (H1[k], v[k]), v)), M) # Hashed vectors. |
| 2267 | y = map(lambda (type, v): H2[type], self._vectors) # Hashed classes. |
| 2268 | # For linear SVC, use LIBLINEAR which is faster. |
| 2269 | # For kernel SVC, use LIBSVM. |
| 2270 | if self.extension == LIBLINEAR: |
| 2271 | f = self._svm.liblinearutil.train |
| 2272 | o = "-s 1 -c %s -p %s -q" % ( |
| 2273 | self._cost, # -c |
| 2274 | self._epsilon # -p |
| 2275 | ) |
| 2276 | else: |
| 2277 | f = self._svm.libsvmutil.svm_train |
| 2278 | o = "-s %s -t %s -d %s -g %s -r %s -c %s -p %s -n %s -m %s -h %s -b %s -q" % ( |
| 2279 | self._type, # -s |
| 2280 | self._kernel, # -t |
| 2281 | self._degree, # -d |
| 2282 | self._gamma, # -g |
| 2283 | self._coeff0, # -r |
| 2284 | self._cost, # -c |
| 2285 | self._epsilon, # -p |
| 2286 | self._nu, # -n |
| 2287 | self._cache, # -m |
| 2288 | int(self._shrinking), # -h |
| 2289 | 1, # -b |
| 2290 | ) |
| 2291 | # Cache the model and the feature hash. |
| 2292 | # SVM.train() will remove the cached model (since it needs to be retrained). |
| 2293 | self._model = (f(y, x, o), H1, H2, H3) |
| 2294 | |
| 2295 | def _classify(self, document, probability=False): |
| 2296 | """ Calls libsvm.svm_predict() with the cached model. |
no test coverage detected