Skip to content

Commit f52e811

Browse files
committed
TST better coverage in kernel_approximation
1 parent 73a1229 commit f52e811

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

sklearn/tests/test_kernel_approximation.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from scipy.sparse import csr_matrix
33

44
from sklearn.utils.testing import assert_array_equal, assert_equal
5-
from sklearn.utils.testing import assert_array_almost_equal
5+
from sklearn.utils.testing import assert_array_almost_equal, assert_raises
66

77
from sklearn.kernel_approximation import RBFSampler
88
from sklearn.kernel_approximation import AdditiveChi2Sampler
@@ -35,6 +35,7 @@ def test_additive_chi2_sampler():
3535
transform = AdditiveChi2Sampler(sample_steps=3)
3636
X_trans = transform.fit_transform(X)
3737
Y_trans = transform.transform(Y)
38+
3839
kernel_approx = np.dot(X_trans, Y_trans.T)
3940

4041
assert_array_almost_equal(kernel, kernel_approx, 1)
@@ -45,6 +46,15 @@ def test_additive_chi2_sampler():
4546
assert_array_equal(X_trans, X_sp_trans.A)
4647
assert_array_equal(Y_trans, Y_sp_trans.A)
4748

49+
# test error is raised on negative input
50+
Y_neg = Y.copy()
51+
Y_neg[0, 0] = -1
52+
assert_raises(ValueError, transform.transform, Y_neg)
53+
54+
# test error on invalid sample_steps
55+
transform = AdditiveChi2Sampler(sample_steps=4)
56+
assert_raises(ValueError, transform.fit, X)
57+
4858

4959
def test_skewed_chi2_sampler():
5060
"""test that RBFSampler approximates kernel on random data"""
@@ -57,8 +67,8 @@ def test_skewed_chi2_sampler():
5767

5868
# we do it in log-space in the hope that it's more stable
5969
# this array is n_samples_x x n_samples_y big x n_features
60-
log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) +
61-
np.log(2.) - np.log(X_c + Y_c))
70+
log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) + np.log(2.) -
71+
np.log(X_c + Y_c))
6272
# reduce to n_samples_x x n_samples_y by summing over features in log-space
6373
kernel = np.exp(log_kernel.sum(axis=2))
6474

@@ -67,9 +77,15 @@ def test_skewed_chi2_sampler():
6777
random_state=42)
6878
X_trans = transform.fit_transform(X)
6979
Y_trans = transform.transform(Y)
80+
7081
kernel_approx = np.dot(X_trans, Y_trans.T)
7182
assert_array_almost_equal(kernel, kernel_approx, 1)
7283

84+
# test error is raised on negative input
85+
Y_neg = Y.copy()
86+
Y_neg[0, 0] = -1
87+
assert_raises(ValueError, transform.transform, Y_neg)
88+
7389

7490
def test_rbf_sampler():
7591
"""test that RBFSampler approximates kernel on random data"""

0 commit comments

Comments
 (0)