Calls libsvm.svm_predict() with the cached model. For CLASSIFICATION, returns the predicted class. For CLASSIFICATION with probability=True, returns a list of (weight, class)-tuples. For REGRESSION, returns a float.
(self, document, probability=False)
| 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. |
| 2297 | For CLASSIFICATION, returns the predicted class. |
| 2298 | For CLASSIFICATION with probability=True, returns a list of (weight, class)-tuples. |
| 2299 | For REGRESSION, returns a float. |
| 2300 | """ |
| 2301 | if self._model is None: |
| 2302 | return None |
| 2303 | M = self._model[0] |
| 2304 | H1 = self._model[1] |
| 2305 | H2 = self._model[2] |
| 2306 | H3 = self._model[3] |
| 2307 | n = len(H1) |
| 2308 | v = self._vector(document)[1] |
| 2309 | v = dict(map(lambda (i, k): (H1.get(k, n+i+1), v[k]), enumerate(v))) |
| 2310 | # For linear SVC, use LIBLINEAR which is 10x faster. |
| 2311 | # For kernel SVC, use LIBSVM. |
| 2312 | if self.extension == LIBLINEAR: |
| 2313 | f = self._svm.liblinearutil.predict |
| 2314 | o = "-b 0 -q" |
| 2315 | else: |
| 2316 | f = self._svm.libsvmutil.svm_predict |
| 2317 | o = "-b %s -q" % int(probability) |
| 2318 | p = f([0], [v], M, o) |
| 2319 | # Note: LIBLINEAR does not currently support probabilities for classification. |
| 2320 | if self._type == CLASSIFICATION and probability is True and self.extension == LIBLINEAR: |
| 2321 | return {} |
| 2322 | if self._type == CLASSIFICATION and probability is True: |
| 2323 | return defaultdict(float, ((H3[i], w) for i, w in enumerate(p[2][0]))) |
| 2324 | if self._type == CLASSIFICATION: |
| 2325 | return H3.get(int(p[0][0])) |
| 2326 | if self._type == REGRESSION: |
| 2327 | return p[0][0] |
| 2328 | if self._type == DETECTION: |
| 2329 | return p[0][0] > 0 # -1 = outlier => return False |
| 2330 | return p[0][0] |
| 2331 | |
| 2332 | def train(self, document, type=None): |
| 2333 | """ Trains the classifier with the given document of the given type (i.e., class). |