Skip to content

Commit a1a316a

Browse files
committed
Christian's suggestions and decision_function tests
1 parent 5e619ae commit a1a316a

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

examples/pcovc/KPCovC_Comparison.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sklearn.model_selection import train_test_split
2323
from sklearn.linear_model import (
2424
LogisticRegressionCV,
25-
RidgeClassifier,
25+
RidgeClassifierCV,
2626
SGDClassifier,
2727
)
2828

@@ -199,10 +199,10 @@
199199
"kernel_params": {"kernel": "rbf", "gamma": 12},
200200
"title": "Logistic Regression",
201201
},
202-
RidgeClassifier(random_state=random_state): {
202+
RidgeClassifierCV(): {
203203
"kernel_params": {"kernel": "rbf", "gamma": 1},
204204
"title": "Ridge Classifier",
205-
"eps": 0.25,
205+
"eps": 0.40,
206206
},
207207
LinearSVC(random_state=random_state): {
208208
"kernel_params": {"kernel": "rbf", "gamma": 15},

tests/test_kernel_pcovc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ def test_Z_shape(self):
177177
self.assertTrue(Z.ndim == 2)
178178
self.assertTrue((Z.shape[0], Z.shape[1]) == (self.X.shape[0], n_classes))
179179

180+
def test_decision_function(self):
181+
"""Check that KPCovC's decision_function works when only T is
182+
provided and throws an error when appropriate.
183+
"""
184+
kpcovc = self.model(center=True)
185+
kpcovc.fit(self.X, self.Y)
186+
187+
with self.assertRaises(ValueError) as cm:
188+
_ = kpcovc.decision_function()
189+
self.assertEqual(
190+
str(cm.exception),
191+
"Either X or T must be supplied.",
192+
)
193+
194+
_ = kpcovc.decision_function(self.X)
195+
T = kpcovc.transform(self.X)
196+
_ = kpcovc.decision_function(T=T)
197+
180198
def test_no_centerer(self):
181199
"""Tests that when center=False, no centerer exists."""
182200
kpcovc = self.model(center=False)

tests/test_pcovc.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ def test_T_shape(self):
402402
self.assertTrue(check_X_y(self.X, T, multi_output=True))
403403
self.assertTrue(T.shape[-1] == n_components)
404404

405+
def test_Y_Shape(self):
406+
pcovc = self.model()
407+
Y = np.vstack(self.Y)
408+
pcovc.fit(self.X, Y)
409+
410+
self.assertEqual(pcovc.pxz_.shape[0], self.X.shape[1])
411+
self.assertEqual(pcovc.ptz_.shape[0], pcovc.n_components_)
412+
405413
def test_Z_shape(self):
406414
"""Check that PCovC returns an evidence matrix consistent with the
407415
number of samples and the number of classes.
@@ -428,20 +436,28 @@ def test_Z_shape(self):
428436
self.assertTrue(Z.ndim == 2)
429437
self.assertTrue((Z.shape[0], Z.shape[1]) == (self.X.shape[0], n_classes))
430438

439+
def test_decision_function(self):
440+
"""Check that PCovC's decision_function works when only T is
441+
provided and throws an error when appropriate.
442+
"""
443+
pcovc = self.model()
444+
pcovc.fit(self.X, self.Y)
445+
with self.assertRaises(ValueError) as cm:
446+
_ = pcovc.decision_function()
447+
self.assertEqual(
448+
str(cm.exception),
449+
"Either X or T must be supplied.",
450+
)
451+
452+
T = pcovc.transform(self.X)
453+
_ = pcovc.decision_function(T=T)
454+
431455
def test_default_ncomponents(self):
432456
pcovc = PCovC(mixing=0.5)
433457
pcovc.fit(self.X, self.Y)
434458

435459
self.assertEqual(pcovc.n_components_, min(self.X.shape))
436460

437-
def test_Y_Shape(self):
438-
pcovc = self.model()
439-
Y = np.vstack(self.Y)
440-
pcovc.fit(self.X, Y)
441-
442-
self.assertEqual(pcovc.pxz_.shape[0], self.X.shape[1])
443-
self.assertEqual(pcovc.ptz_.shape[0], pcovc.n_components_)
444-
445461
def test_prefit_classifier(self):
446462
classifier = LinearSVC()
447463
classifier.fit(self.X, self.Y)

0 commit comments

Comments
 (0)