Skip to content

Commit f807237

Browse files
committed
Minor changes to examples, formatting
1 parent 0c841dd commit f807237

File tree

8 files changed

+61
-50
lines changed

8 files changed

+61
-50
lines changed

examples/pcovc/PCovC-BreastCancerDataset.ipynb

Lines changed: 10 additions & 17 deletions
Large diffs are not rendered by default.

examples/pcovc/PCovC-IrisDataset.ipynb

Lines changed: 16 additions & 16 deletions
Large diffs are not rendered by default.

src/skmatter/decomposition/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@
2525
original PCovR method, proposed in [Helfrecht2020]_.
2626
"""
2727

28-
from ._pcov import _BasePCov
28+
from ._pcov import _BasePCov, pcovr_covariance, pcovr_kernel
2929

3030
from ._pcovr import PCovR
3131
from ._kernel_pcovr import KernelPCovR
3232

3333
from ._pcovc import PCovC
3434

35-
from ._pcov import pcovr_covariance, pcovr_kernel
36-
3735

3836
__all__ = [
37+
"_BasePCov",
3938
"pcovr_covariance",
4039
"pcovr_kernel",
4140
"PCovR",
4241
"KernelPCovR",
4342
"PCovC",
44-
"_BasePCov",
4543
]

src/skmatter/decomposition/_kernel_pcovr.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ class KernelPCovR(_BasePCA, LinearModel):
4040
----------
4141
mixing : float, default=0.5
4242
mixing parameter, as described in PCovR as :math:`{\alpha}`
43+
4344
n_components : int, float or str, default=None
4445
Number of components to keep.
4546
if n_components is not set all components are kept::
4647
4748
n_components == n_samples
49+
4850
svd_solver : {'auto', 'full', 'arpack', 'randomized'}, default='auto'
4951
If auto :
5052
The solver is selected by a default policy based on `X.shape` and
@@ -62,6 +64,7 @@ class KernelPCovR(_BasePCA, LinearModel):
6264
0 < n_components < min(X.shape)
6365
If randomized :
6466
run randomized SVD by the method of Halko et al.
67+
6568
regressor : {instance of `sklearn.kernel_ridge.KernelRidge`, `precomputed`, None}, default=None
6669
The regressor to use for computing
6770
the property predictions :math:`\hat{\mathbf{Y}}`.
@@ -72,36 +75,47 @@ class KernelPCovR(_BasePCA, LinearModel):
7275
7376
If `precomputed`, we assume that the `y` passed to the `fit` function
7477
is the regressed form of the targets :math:`{\mathbf{\hat{Y}}}`.
78+
7579
kernel : "linear" | "poly" | "rbf" | "sigmoid" | "cosine" | "precomputed"
7680
Kernel. Default="linear".
81+
7782
gamma : float, default=None
7883
Kernel coefficient for rbf, poly and sigmoid kernels. Ignored by other
7984
kernels.
85+
8086
degree : int, default=3
8187
Degree for poly kernels. Ignored by other kernels.
88+
8289
coef0 : float, default=1
8390
Independent term in poly and sigmoid kernels.
8491
Ignored by other kernels.
92+
8593
kernel_params : mapping of str to any, default=None
8694
Parameters (keyword arguments) and values for kernel passed as
8795
callable object. Ignored by other kernels.
96+
8897
center : bool, default=False
8998
Whether to center any computed kernels
99+
90100
fit_inverse_transform : bool, default=False
91101
Learn the inverse transform for non-precomputed kernels.
92102
(i.e. learn to find the pre-image of a point)
103+
93104
tol : float, default=1e-12
94105
Tolerance for singular values computed by svd_solver == 'arpack'
95106
and for matrix inversions.
96107
Must be of range [0.0, infinity).
108+
97109
n_jobs : int, default=None
98110
The number of parallel jobs to run.
99111
:obj:`None` means 1 unless in a :obj:`joblib.parallel_backend` context.
100112
``-1`` means using all processors.
113+
101114
iterated_power : int or 'auto', default='auto'
102115
Number of iterations for the power method computed by
103116
svd_solver == 'randomized'.
104117
Must be of range [0, infinity).
118+
105119
random_state : int, :class:`numpy.random.RandomState` instance or None, default=None
106120
Used when the 'arpack' or 'randomized' solvers are used. Pass an int
107121
for reproducible results across multiple function calls.
@@ -111,18 +125,23 @@ class KernelPCovR(_BasePCA, LinearModel):
111125
pt__: numpy.darray of size :math:`({n_{components}, n_{components}})`
112126
pseudo-inverse of the latent-space projection, which
113127
can be used to contruct projectors from latent-space
128+
114129
pkt_: numpy.ndarray of size :math:`({n_{samples}, n_{components}})`
115130
the projector, or weights, from the input kernel :math:`\mathbf{K}`
116131
to the latent-space projection :math:`\mathbf{T}`
132+
117133
pky_: numpy.ndarray of size :math:`({n_{samples}, n_{properties}})`
118134
the projector, or weights, from the input kernel :math:`\mathbf{K}`
119135
to the properties :math:`\mathbf{Y}`
136+
120137
pty_: numpy.ndarray of size :math:`({n_{components}, n_{properties}})`
121138
the projector, or weights, from the latent-space projection
122139
:math:`\mathbf{T}` to the properties :math:`\mathbf{Y}`
140+
123141
ptx_: numpy.ndarray of size :math:`({n_{components}, n_{features}})`
124142
the projector, or weights, from the latent-space projection
125143
:math:`\mathbf{T}` to the feature matrix :math:`\mathbf{X}`
144+
126145
X_fit_: numpy.ndarray of shape (n_samples, n_features)
127146
The data used to fit the model. This attribute is used to build kernels
128147
from new data.
@@ -133,12 +152,10 @@ class KernelPCovR(_BasePCA, LinearModel):
133152
>>> from skmatter.decomposition import KernelPCovR
134153
>>> from skmatter.preprocessing import StandardFlexibleScaler as SFS
135154
>>> from sklearn.kernel_ridge import KernelRidge
136-
>>>
137155
>>> X = np.array([[-1, 1, -3, 1], [1, -2, 1, 2], [-2, 0, -2, -2], [1, 0, 2, -1]])
138156
>>> X = SFS().fit_transform(X)
139157
>>> Y = np.array([[0, -5], [-1, 1], [1, -5], [-3, 2]])
140158
>>> Y = SFS(column_wise=True).fit_transform(Y)
141-
>>>
142159
>>> kpcovr = KernelPCovR(
143160
... mixing=0.1,
144161
... n_components=2,
@@ -248,6 +265,7 @@ def fit(self, X, Y, W=None):
248265
means and scaled. If features are related, the matrix should be scaled
249266
to have unit variance, otherwise :math:`\mathbf{X}` should be
250267
scaled so that each feature has a variance of 1 / n_features.
268+
251269
Y : numpy.ndarray, shape (n_samples, n_properties)
252270
Training data, where n_samples is the number of samples and
253271
n_properties is the number of properties
@@ -256,6 +274,7 @@ def fit(self, X, Y, W=None):
256274
means and scaled. If features are related, the matrix should be scaled
257275
to have unit variance, otherwise :math:`\mathbf{Y}` should be
258276
scaled so that each feature has a variance of 1 / n_features.
277+
259278
W : numpy.ndarray, shape (n_samples, n_properties)
260279
Regression weights, optional when regressor=`precomputed`. If not
261280
passed, it is assumed that `W = np.linalg.lstsq(K, Y, self.tol)[0]`
@@ -463,6 +482,7 @@ def score(self, X, y):
463482
----------
464483
X : numpy.ndarray
465484
independent (predictor) variable
485+
466486
Y : numpy.ndarray
467487
dependent (response) variable
468488

src/skmatter/decomposition/_pcov.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ def __init__(
3838
self.random_state = random_state
3939
self.whiten = whiten
4040

41-
# this contains the common functionality for the PCovR and PCovC fit methods,
42-
# but leaves the rest of the functionality to the subclass
4341
def _fit_utils(self, X):
42+
"""Contains the common functionality for the PCovR and PCovC fit methods,
43+
but leaves the rest of the functionality to the subclass.
44+
"""
4445
# saved for inverse transformations from the latent space,
4546
# should be zero in the case that the features have been properly centered
4647
self.mean_ = np.mean(X, axis=0)
@@ -152,6 +153,7 @@ def _fit_sample_space(self, X, Y, Yhat, W, compute_pty_=True):
152153

153154
self.pxt_ = P @ T
154155
self.ptx_ = T.T @ X
156+
155157
if compute_pty_:
156158
self.pty_ = T.T @ Y
157159

@@ -168,6 +170,7 @@ def inverse_transform(self, T):
168170

169171
def transform(self, X=None):
170172
check_is_fitted(self, ["pxt_", "mean_"])
173+
171174
return super().transform(X)
172175

173176
def _decompose_truncated(self, mat):

src/skmatter/decomposition/_pcovc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ def fit(self, X, Y, W=None):
291291

292292
# instead of using linear regression solution, refit with the
293293
# classifier and steal weights to get pxz and ptz
294-
295294
self.classifier_ = clone(classifier).fit(X @ self.pxt_, Y)
296295

297296
self.ptz_ = self.classifier_.coef_.T

src/skmatter/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
Y_feature_orthogonalizer,
99
Y_sample_orthogonalizer,
1010
)
11+
12+
from ._pcovc_utils import check_cl_fit
13+
1114
from ._pcovr_utils import (
1215
check_krr_fit,
1316
check_lr_fit,
1417
pcovr_covariance,
1518
pcovr_kernel,
1619
)
17-
18-
from ._pcovc_utils import check_cl_fit
19-
2020
from ._progress_bar import (
2121
get_progress_bar,
2222
no_progress_bar,

src/skmatter/utils/_pcovc_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def check_cl_fit(classifier, X, y):
3030
------
3131
ValueError
3232
If the fitted classifiers's coefficients have a shape incompatible with the
33-
number of classes or number of features.
33+
number of features in X or the number of classes in y.
3434
"""
3535
try:
3636
check_is_fitted(classifier)
@@ -39,10 +39,8 @@ def check_cl_fit(classifier, X, y):
3939
# Check compatibility with X
4040
validate_data(fitted_classifier, X, y, reset=False, multi_output=True)
4141

42-
# Check compatibility with y
43-
# dimension of classifier coefficients is always 2, hence we don't
44-
# need to check dimension for match with Y
45-
# We need to double check this...
42+
# Check compatibility with the number of features in X and the number of
43+
# classes in y
4644
n_classes = len(np.unique(y))
4745

4846
if n_classes == 2:

0 commit comments

Comments
 (0)