Skip to content

Commit 5e619ae

Browse files
committed
Docstring and other skmatter/decomposition suggestions
1 parent 4962500 commit 5e619ae

File tree

6 files changed

+69
-35
lines changed

6 files changed

+69
-35
lines changed

src/skmatter/decomposition/_kernel_pcovc.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323

2424
class KernelPCovC(LinearClassifierMixin, _BaseKPCov):
25-
r"""Kernel Principal Covariates Classification is a modification on the Principal
26-
Covariates Classification proposed in [Jorgensen2025]_. It determines a latent-space
27-
projection :math:`\mathbf{T}` which minimizes a combined loss in supervised and unsupervised
25+
r"""Kernel Principal Covariates Classification (KPCovC).
26+
27+
KPCovC is a modification on the PrincipalCovariates Classification
28+
proposed in [Jorgensen2025]_. It determines a latent-space projection
29+
:math:`\mathbf{T}` which minimizes a combined loss in supervised and unsupervised
2830
tasks in the reproducing kernel Hilbert space (RKHS).
2931
3032
This projection is determined by the eigendecomposition of a modified gram matrix
@@ -69,16 +71,22 @@ class KernelPCovC(LinearClassifierMixin, _BaseKPCov):
6971
run randomized SVD by the method of Halko et al.
7072
7173
classifier: `estimator object` or `precomputed`, default=None
72-
classifier for computing :math:`{\mathbf{Z}}`. The classifier should be one of
73-
`sklearn.linear_model.LogisticRegression`, `sklearn.linear_model.LogisticRegressionCV`,
74-
`sklearn.svm.LinearSVC`, `sklearn.discriminant_analysis.LinearDiscriminantAnalysis`,
75-
`sklearn.linear_model.RidgeClassifier`, `sklearn.linear_model.RidgeClassifierCV`,
76-
`sklearn.linear_model.SGDClassifier`, or `Perceptron`.
74+
classifier for computing :math:`{\mathbf{Z}}`. The classifier should be
75+
one of the following:
76+
77+
- ``sklearn.linear_model.LogisticRegression()``
78+
- ``sklearn.linear_model.LogisticRegressionCV()``
79+
- ``sklearn.svm.LinearSVC()``
80+
- ``sklearn.discriminant_analysis.LinearDiscriminantAnalysis()``
81+
- ``sklearn.linear_model.RidgeClassifier()``
82+
- ``sklearn.linear_model.RidgeClassifierCV()``
83+
- ``sklearn.linear_model.Perceptron()``
84+
7785
If a pre-fitted classifier is provided, it is used to compute :math:`{\mathbf{Z}}`.
7886
If None, ``sklearn.linear_model.LogisticRegression()``
7987
is used as the classifier.
8088
81-
kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, default='linear'
89+
kernel : {"linear", "poly", "rbf", "sigmoid", "precomputed"} or callable, default="linear"
8290
Kernel.
8391
8492
gamma : {'scale', 'auto'} or float, default=None
@@ -223,8 +231,9 @@ def __init__(
223231
self.classifier = classifier
224232

225233
def fit(self, X, Y, W=None):
226-
r"""Fit the model with X and Y. A computed kernel K is
227-
derived from X, and W is taken from the
234+
r"""Fit the model with X and Y.
235+
236+
A computed kernel K is derived from X, and W is taken from the
228237
coefficients of a linear classifier fit between K and Y to compute
229238
Z:
230239
@@ -265,7 +274,7 @@ def fit(self, X, Y, W=None):
265274

266275
super().fit(X)
267276

268-
K = super()._get_kernel(X)
277+
K = self._get_kernel(X)
269278

270279
if self.center:
271280
self.centerer_ = KernelNormalizer()

src/skmatter/decomposition/_kernel_pcovr.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111

1212
class KernelPCovR(_BaseKPCov):
13-
r"""Kernel Principal Covariates Regression, as described in [Helfrecht2020]_,
14-
determines a latent-space projection :math:`\mathbf{T}` which minimizes a combined
15-
loss in supervised and unsupervised tasks in the reproducing kernel Hilbert space
16-
(RKHS).
13+
r"""Kernel Principal Covariates Regression (KPCovR).
14+
15+
As described in [Helfrecht2020]_, KPCovR determines a latent-space projection
16+
:math:`\mathbf{T}` which minimizes a combined loss in supervised and unsupervised
17+
tasks in the reproducing kernel Hilbert space (RKHS).
1718
1819
This projection is determined by the eigendecomposition of a modified gram matrix
1920
:math:`\mathbf{\tilde{K}}`
@@ -243,7 +244,7 @@ def fit(self, X, Y, W=None):
243244

244245
super().fit(X)
245246

246-
K = super()._get_kernel(X)
247+
K = self._get_kernel(X)
247248

248249
if self.center:
249250
self.centerer_ = KernelNormalizer()
@@ -382,7 +383,9 @@ def inverse_transform(self, T):
382383

383384
def score(self, X, y):
384385
r"""Computes the (negative) loss values for KernelPCovR on the given predictor
385-
and response variables. The loss in :math:`\mathbf{K}`, as explained in
386+
and response variables.
387+
388+
The loss in :math:`\mathbf{K}`, as explained in
386389
[Helfrecht2020]_ does not correspond to a traditional Gram loss
387390
:math:`\mathbf{K} - \mathbf{TT}^T`. Indicating the kernel between set A and B as
388391
:math:`\mathbf{K}_{AB}`, the projection of set A as :math:`\mathbf{T}_A`, and

src/skmatter/decomposition/_kpcov.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222

2323
class _BaseKPCov(_BasePCA, LinearModel, metaclass=ABCMeta):
24+
"""Base class for KernelPCovR and KernelPCovC methods.
25+
26+
Warning: This class should not be used directly.
27+
Use derived classes instead.
28+
"""
29+
2430
@abstractmethod
2531
def __init__(
2632
self,
@@ -68,7 +74,6 @@ def _get_kernel(self, X, Y=None):
6874
X, Y, metric=self.kernel, filter_params=True, n_jobs=self.n_jobs, **params
6975
)
7076

71-
@abstractmethod
7277
def fit(self, X):
7378
"""Contains the common functionality for the KPCovR and KPCovC fit methods,
7479
but leaves the rest of the functionality to the subclass.

src/skmatter/decomposition/_pcov.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222

2323
class _BasePCov(_BasePCA, LinearModel, metaclass=ABCMeta):
24+
"""Base class for PCovR and PCovC methods.
25+
26+
Warning: This class should not be used directly.
27+
Use derived classes instead.
28+
"""
29+
2430
@abstractmethod
2531
def __init__(
2632
self,
@@ -42,7 +48,6 @@ def __init__(
4248
self.random_state = random_state
4349
self.whiten = whiten
4450

45-
@abstractmethod
4651
def fit(self, X):
4752
"""Contains the common functionality for the PCovR and PCovC fit methods,
4853
but leaves the rest of the functionality to the subclass.

src/skmatter/decomposition/_pcovc.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020

2121
class PCovC(LinearClassifierMixin, _BasePCov):
22-
r"""Principal Covariates Classification, as described in [Jorgensen2025]_,
23-
determines a latent-space projection :math:`\mathbf{T}`
24-
which minimizes a combined loss in supervised and unsupervised tasks.
22+
r"""Principal Covariates Classification (PCovC).
23+
24+
As described in [Jorgensen2025]_, PCovC determines a latent-space projection
25+
:math:`\mathbf{T}` which minimizes a combined loss in supervised and
26+
unsupervised tasks.
2527
2628
This projection is determined by the eigendecomposition of a modified gram
2729
matrix :math:`\mathbf{\tilde{K}}`
@@ -99,12 +101,19 @@ class PCovC(LinearClassifierMixin, _BasePCov):
99101
default=`sample` when :math:`{n_{samples} < n_{features}}` and
100102
`feature` when :math:`{n_{features} < n_{samples}}`
101103
102-
classifier: `estimator object` or `precomputed`, default=None
103-
classifier for computing :math:`{\mathbf{Z}}`. The classifier should be one of
104-
`sklearn.linear_model.LogisticRegression`, `sklearn.linear_model.LogisticRegressionCV`,
105-
`sklearn.svm.LinearSVC`, `sklearn.discriminant_analysis.LinearDiscriminantAnalysis`,
106-
`sklearn.linear_model.RidgeClassifier`, `sklearn.linear_model.RidgeClassifierCV`,
107-
`sklearn.linear_model.SGDClassifier`, or `Perceptron`. If a pre-fitted classifier
104+
classifier: `estimator object` or `precomputed`, default=None
105+
classifier for computing :math:`{\mathbf{Z}}`. The classifier should be
106+
one of the following:
107+
108+
- ``sklearn.linear_model.LogisticRegression()``
109+
- ``sklearn.linear_model.LogisticRegressionCV()``
110+
- ``sklearn.svm.LinearSVC()``
111+
- ``sklearn.discriminant_analysis.LinearDiscriminantAnalysis()``
112+
- ``sklearn.linear_model.RidgeClassifier()``
113+
- ``sklearn.linear_model.RidgeClassifierCV()``
114+
- ``sklearn.linear_model.Perceptron()``
115+
116+
If a pre-fitted classifier
108117
is provided, it is used to compute :math:`{\mathbf{Z}}`.
109118
Note that any pre-fitting of the classifier will be lost if `PCovC` is
110119
within a composite estimator that enforces cloning, e.g.,
@@ -218,9 +227,10 @@ def __init__(
218227
self.classifier = classifier
219228

220229
def fit(self, X, Y, W=None):
221-
r"""Fit the model with X and Y. Note that W is taken from the
222-
coefficients of a linear classifier fit between X and Y to compute
223-
Z:
230+
r"""Fit the model with X and Y.
231+
232+
Note that W is taken from the coefficients of a linear classifier fit
233+
between X and Y to compute Z:
224234
225235
.. math::
226236
\mathbf{Z} = \mathbf{X} \mathbf{W}

src/skmatter/decomposition/_pcovr.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010

1111
class PCovR(RegressorMixin, MultiOutputMixin, _BasePCov):
12-
r"""Principal Covariates Regression, as described in [deJong1992]_,
13-
determines a latent-space projection :math:`\mathbf{T}` which
14-
minimizes a combined loss in supervised and unsupervised tasks.
12+
r"""Principal Covariates Regression (PCovR).
13+
14+
As described in [deJong1992]_, PCovR determines a latent-space projection
15+
:math:`\mathbf{T}` which minimizes a combined loss in supervised and
16+
unsupervised tasks.
1517
1618
This projection is determined by the eigendecomposition of a modified gram
1719
matrix :math:`\mathbf{\tilde{K}}`

0 commit comments

Comments
 (0)