Return probability estimates for the test vectors X. Parameters ---------- X : array-like of shape (n_samples, n_features) Test data. Returns ------- P : ndarray of shape (n_samples, n_classes) or list of such arrays
(self, X)
| 337 | return y |
| 338 | |
| 339 | def predict_proba(self, X): |
| 340 | """ |
| 341 | Return probability estimates for the test vectors X. |
| 342 | |
| 343 | Parameters |
| 344 | ---------- |
| 345 | X : array-like of shape (n_samples, n_features) |
| 346 | Test data. |
| 347 | |
| 348 | Returns |
| 349 | ------- |
| 350 | P : ndarray of shape (n_samples, n_classes) or list of such arrays |
| 351 | Returns the probability of the sample for each class in |
| 352 | the model, where classes are ordered arithmetically, for each |
| 353 | output. |
| 354 | """ |
| 355 | check_is_fitted(self) |
| 356 | |
| 357 | # numpy random_state expects Python int and not long as size argument |
| 358 | # under Windows |
| 359 | n_samples = _num_samples(X) |
| 360 | rs = check_random_state(self.random_state) |
| 361 | |
| 362 | n_classes_ = self.n_classes_ |
| 363 | classes_ = self.classes_ |
| 364 | class_prior_ = self.class_prior_ |
| 365 | constant = self.constant |
| 366 | if self.n_outputs_ == 1: |
| 367 | # Get same type even for self.n_outputs_ == 1 |
| 368 | n_classes_ = [n_classes_] |
| 369 | classes_ = [classes_] |
| 370 | class_prior_ = [class_prior_] |
| 371 | constant = [constant] |
| 372 | |
| 373 | P = [] |
| 374 | for k in range(self.n_outputs_): |
| 375 | if self._strategy == "most_frequent": |
| 376 | ind = class_prior_[k].argmax() |
| 377 | out = np.zeros((n_samples, n_classes_[k]), dtype=np.float64) |
| 378 | out[:, ind] = 1.0 |
| 379 | elif self._strategy == "prior": |
| 380 | out = np.ones((n_samples, 1)) * class_prior_[k] |
| 381 | |
| 382 | elif self._strategy == "stratified": |
| 383 | out = rs.multinomial(1, class_prior_[k], size=n_samples) |
| 384 | out = out.astype(np.float64) |
| 385 | |
| 386 | elif self._strategy == "uniform": |
| 387 | out = np.ones((n_samples, n_classes_[k]), dtype=np.float64) |
| 388 | out /= n_classes_[k] |
| 389 | |
| 390 | elif self._strategy == "constant": |
| 391 | ind = np.where(classes_[k] == constant[k]) |
| 392 | out = np.zeros((n_samples, n_classes_[k]), dtype=np.float64) |
| 393 | out[:, ind] = 1.0 |
| 394 | |
| 395 | P.append(out) |
| 396 |