|
16 | 16 | from sklearn.preprocessing import LabelBinarizer |
17 | 17 | from sklearn.utils._array_api import get_namespace, indexing_dtype |
18 | 18 | from sklearn.svm import SVC |
| 19 | +from sklearn.base import clone |
| 20 | +from copy import deepcopy |
19 | 21 |
|
20 | | -from ..preprocessing import KernelNormalizer |
21 | | -from ..utils import check_krr_fit, pcovr_kernel |
| 22 | +from skmatter.preprocessing import KernelNormalizer |
| 23 | +from skmatter.utils import check_krr_fit, pcovr_kernel |
| 24 | + |
| 25 | +def check_cl_fit(classifier, X, y): |
| 26 | + r""" |
| 27 | + Checks that a (linear) classifier is fitted, and if not, |
| 28 | + fits it with the provided data |
| 29 | + :param regressor: sklearn-style classifier |
| 30 | + :type classifier: object |
| 31 | + :param X: feature matrix with which to fit the classifier |
| 32 | + if it is not already fitted |
| 33 | + :type X: array |
| 34 | + :param y: target values with which to fit the classifier |
| 35 | + if it is not already fitted |
| 36 | + :type y: array |
| 37 | + """ |
| 38 | + try: |
| 39 | + check_is_fitted(classifier) |
| 40 | + fitted_classifier = deepcopy(classifier) |
| 41 | + |
| 42 | + # Check compatibility with X |
| 43 | + fitted_classifier._validate_data(X, y, reset=False, multi_output=True) |
| 44 | + |
| 45 | + # Check compatibility with y |
| 46 | + |
| 47 | + # changed from if fitted_classifier.coef_.ndim != y.ndim: |
| 48 | + # dimension of classifier coefficients is always 2, hence we don't need to check |
| 49 | + # for match with Y |
| 50 | + if fitted_classifier.coef_.shape[1] != X.shape[1]: |
| 51 | + raise ValueError( |
| 52 | + "The classifier coefficients have a shape incompatible " |
| 53 | + "with the supplied feature space. " |
| 54 | + "The coefficients have shape %d and the features " |
| 55 | + "have shape %d" % (fitted_classifier.coef_.shape, X.shape) |
| 56 | + ) |
| 57 | + # LogisticRegression does not support multioutput, but RidgeClassifier does |
| 58 | + elif y.ndim == 2: |
| 59 | + if fitted_classifier.coef_.shape[0] != y.shape[1]: |
| 60 | + raise ValueError( |
| 61 | + "The classifier coefficients have a shape incompatible " |
| 62 | + "with the supplied target space. " |
| 63 | + "The coefficients have shape %r and the targets " |
| 64 | + "have shape %r" % (fitted_classifier.coef_.shape, y.shape) |
| 65 | + ) |
| 66 | + |
| 67 | + except NotFittedError: |
| 68 | + fitted_classifier = clone(classifier) |
| 69 | + fitted_classifier.fit(X, y) |
| 70 | + |
| 71 | + return fitted_classifier |
22 | 72 |
|
23 | 73 |
|
24 | 74 | class KernelPCovC(_BasePCA, LinearModel): |
@@ -432,7 +482,8 @@ def fit(self, X, y, W=None): |
432 | 482 |
|
433 | 483 | self._fit(K, Z, W) #gives us T, Pkt, self.pt__ |
434 | 484 |
|
435 | | - |
| 485 | + self.classifier_ = check_cl_fit(classifier, K @ self.pkt, y) #Ptz as weights |
| 486 | + |
436 | 487 | ''' |
437 | 488 | we now need Z = TPtz |
438 | 489 |
|
@@ -477,6 +528,7 @@ def decision_function(self, X=None, T=None): |
477 | 528 | return T @ self.ptz_ |
478 | 529 |
|
479 | 530 | #is there a reason why this predict function is different than the one in PCovc? |
| 531 | + #it can be the same |
480 | 532 | def predict(self, X=None, T=None): |
481 | 533 | """Predicts class values from X or T.""" |
482 | 534 |
|
|
0 commit comments