Skip to content

Commit f3a372f

Browse files
author
Christian Jorgensen
committed
Changing default and adding warnings
1 parent d77d013 commit f3a372f

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/skmatter/decomposition/_kernel_pcovc.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
import numpy as np
23

34
from sklearn import clone
@@ -119,6 +120,14 @@ class KernelPCovC(LinearClassifierMixin, _BaseKPCov):
119120
and for matrix inversions.
120121
Must be of range [0.0, infinity).
121122
123+
z_mean_tol: float, default=1e-12
124+
Tolerance for the column means of Z.
125+
Must be of range [0.0, infinity).
126+
127+
z_var_tol: float, default=1.5
128+
Tolerance for the column variances of Z.
129+
Must be of range [0.0, infinity).
130+
122131
n_jobs : int, default=None
123132
The number of parallel jobs to run.
124133
:obj:`None` means 1 unless in a :obj:`joblib.parallel_backend` context.
@@ -203,7 +212,7 @@ def __init__(
203212
n_components=None,
204213
svd_solver="auto",
205214
classifier=None,
206-
scale_z=True,
215+
scale_z=False,
207216
kernel="linear",
208217
gamma=None,
209218
degree=3,
@@ -212,6 +221,8 @@ def __init__(
212221
center=False,
213222
fit_inverse_transform=False,
214223
tol=1e-12,
224+
z_mean_tol=1e-12,
225+
z_var_tol=1.5,
215226
n_jobs=None,
216227
iterated_power="auto",
217228
random_state=None,
@@ -234,6 +245,8 @@ def __init__(
234245
)
235246
self.classifier = classifier
236247
self.scale_z = scale_z
248+
self.z_mean_tol = z_mean_tol
249+
self.z_var_tol = z_var_tol
237250

238251
def fit(self, X, Y, W=None):
239252
r"""Fit the model with X and Y.
@@ -331,6 +344,23 @@ def fit(self, X, Y, W=None):
331344
if self.scale_z:
332345
Z = StandardFlexibleScaler().fit_transform(Z)
333346

347+
self.z_means_ = np.mean(Z, axis=0)
348+
self.z_vars_ = np.var(Z, axis=0)
349+
350+
if np.max(np.abs(self.z_means_)) > self.z_mean_tol:
351+
warnings.warn(
352+
"This class does not automatically center Z, and the column means "
353+
"of Z are greater than the supplied tolerance. We recommend scaling "
354+
"Z (and the weights) by setting `scale_z=True`."
355+
)
356+
357+
if np.max(self.z_vars_) > self.z_var_tol:
358+
warnings.warn(
359+
"This class does not automatically scale Z, and the column variances "
360+
"of Z are greater than the supplied tolerance. We recommend scaling "
361+
"Z (and the weights) by setting `scale_z=True`. "
362+
)
363+
334364
self._fit(K, Z, W)
335365

336366
self.ptk_ = self.pt__ @ K

src/skmatter/decomposition/_pcovc.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from skmatter.decomposition import _BasePCov
1818
from skmatter.utils import check_cl_fit
1919
from skmatter.preprocessing import StandardFlexibleScaler
20+
import warnings
2021

2122

2223
class PCovC(LinearClassifierMixin, _BasePCov):
@@ -124,7 +125,7 @@ class PCovC(LinearClassifierMixin, _BasePCov):
124125
If None, ``sklearn.linear_model.LogisticRegression()``
125126
is used as the classifier.
126127
127-
scale_z: bool, default=True
128+
scale_z: bool, default=False
128129
Whether to scale Z prior to eigendecomposition.
129130
130131
iterated_power : int or 'auto', default='auto'
@@ -147,6 +148,14 @@ class PCovC(LinearClassifierMixin, _BasePCov):
147148
Tolerance for singular values computed by svd_solver == 'arpack'.
148149
Must be of range [0.0, infinity).
149150
151+
z_mean_tol: float, default=1e-12
152+
Tolerance for the column means of Z.
153+
Must be of range [0.0, infinity).
154+
155+
z_var_tol: float, default=1.5
156+
Tolerance for the column variances of Z.
157+
Must be of range [0.0, infinity).
158+
150159
space: {'feature', 'sample', 'auto'}, default='auto'
151160
whether to compute the PCovC in ``sample`` or ``feature`` space.
152161
The default is equal to ``sample`` when :math:`{n_{samples} < n_{features}}`
@@ -212,9 +221,11 @@ def __init__(
212221
n_components=None,
213222
svd_solver="auto",
214223
tol=1e-12,
224+
z_mean_tol=1e-12,
225+
z_var_tol=1.5,
215226
space="auto",
216227
classifier=None,
217-
scale_z=True,
228+
scale_z=False,
218229
iterated_power="auto",
219230
random_state=None,
220231
whiten=False,
@@ -231,6 +242,8 @@ def __init__(
231242
)
232243
self.classifier = classifier
233244
self.scale_z = scale_z
245+
self.z_mean_tol = z_mean_tol
246+
self.z_var_tol = z_var_tol
234247

235248
def fit(self, X, Y, W=None):
236249
r"""Fit the model with X and Y.
@@ -312,6 +325,23 @@ def fit(self, X, Y, W=None):
312325
Z = z_scaler.transform(Z)
313326
W /= z_scaler.scale_.reshape(1, -1)
314327

328+
self.z_means_ = np.mean(Z, axis=0)
329+
self.z_vars_ = np.var(Z, axis=0)
330+
331+
if np.max(np.abs(self.z_means_)) > self.z_mean_tol:
332+
warnings.warn(
333+
"This class does not automatically center Z, and the column means "
334+
"of Z are greater than the supplied tolerance. We recommend scaling "
335+
"Z (and the weights) by setting `scale_z=True`."
336+
)
337+
338+
if np.max(self.z_vars_) > self.z_var_tol:
339+
warnings.warn(
340+
"This class does not automatically scale Z, and the column variances "
341+
"of Z are greater than the supplied tolerance. We recommend scaling "
342+
"Z (and the weights) by setting `scale_z=True`."
343+
)
344+
315345
if self.space_ == "feature":
316346
self._fit_feature_space(X, Y, Z)
317347
else:

0 commit comments

Comments
 (0)