6
6
7
7
from sklearn .utils ._testing import assert_array_equal
8
8
from sklearn .utils ._testing import assert_array_almost_equal
9
+from sklearn .utils ._testing import assert_allclose
9
10
10
11
from sklearn .metrics .pairwise import kernel_metrics
11
12
from sklearn .kernel_approximation import RBFSampler
@@ -31,12 +32,10 @@ def test_polynomial_count_sketch_raises_if_degree_lower_than_one(degree):
31
32
ps_transform .fit (X , Y )
32
33
33
34
34
- @pytest .mark .parametrize ("X" , [X , csr_matrix (X )])
35
- @pytest .mark .parametrize ("Y" , [Y , csr_matrix (Y )])
36
35
@pytest .mark .parametrize ("gamma" , [0.1 , 1 , 2.5 ])
37
- @pytest .mark .parametrize ("degree" , [1 , 2 , 3 ])
38
- @pytest .mark .parametrize ("coef0" , [0 , 1 , 2.5 ])
39
- def test_polynomial_count_sketch (X , Y , gamma , degree , coef0 ):
36
+ @pytest .mark .parametrize ("degree, n_components " , [( 1 , 500 ), ( 2 , 500 ), ( 3 , 5000 ) ])
37
+ @pytest .mark .parametrize ("coef0" , [0 , 2.5 ])
38
+ def test_polynomial_count_sketch (gamma , degree , coef0 , n_components ):
40
39
# test that PolynomialCountSketch approximates polynomial
41
40
# kernel on random data
42
41
@@ -45,7 +44,11 @@ def test_polynomial_count_sketch(X, Y, gamma, degree, coef0):
45
44
46
45
8000
# approximate kernel mapping
47
46
ps_transform = PolynomialCountSketch (
48
- n_components = 5000 , gamma = gamma , coef0 = coef0 , degree = degree , random_state = 42
47
+ n_components = n_components ,
48
+ gamma = gamma ,
49
+ coef0 = coef0 ,
50
+ degree = degree ,
51
+ random_state = 42 ,
49
52
)
50
53
X_trans = ps_transform .fit_transform (X )
51
54
Y_trans = ps_transform .transform (Y )
@@ -58,6 +61,29 @@ def test_polynomial_count_sketch(X, Y, gamma, degree, coef0):
58
61
assert np .mean (error ) <= 0.05 # mean is fairly close
59
62
60
63
64
+ @pytest .mark .parametrize ("gamma" , [0.1 , 1.0 ])
65
+ @pytest .mark .parametrize ("degree" , [1 , 2 , 3 ])
66
+ @pytest .mark .parametrize ("coef0" , [0 , 2.5 ])
67
+ def test_polynomial_count_sketch_dense_sparse (gamma , degree , coef0 ):
68
+ """Check that PolynomialCountSketch results are the same for dense and sparse
69
+ input.
70
+ """
71
+ ps_dense = PolynomialCountSketch (
72
+ n_components = 500 , gamma = gamma , degree = degree , coef0 = coef0 , random_state = 42
73
+ )
74
+ Xt_dense = ps_dense .fit_transform (X )
75
+ Yt_dense = ps_dense .transform (Y )
76
+
77
+ ps_sparse = PolynomialCountSketch (
78
+ n_components = 500 , gamma = gamma , degree = degree , coef0 = coef0 , random_state = 42
79
+ )
80
+ Xt_sparse = ps_sparse .fit_transform (csr_matrix (X ))
81
+ Yt_sparse = ps_sparse .transform (csr_matrix (Y ))
82
+
83
+ assert_allclose (Xt_dense , Xt_sparse )
84
+ assert_allclose (Yt_dense , Yt_sparse )
85
+
86
+
61
87
def _linear_kernel (X , Y ):
62
88
return np .dot (X , Y .T )
63
89
0 commit comments