8000 TST Speed-up test_polynomial_count_sketch (#23189) · scikit-learn/scikit-learn@84f8409 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84f8409

Browse files
jeremiedbblorentzenchrlesteve
authored
TST Speed-up test_polynomial_count_sketch (#23189)
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com> Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
1 parent 962c9b4 commit 84f8409

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

sklearn/tests/test_kernel_approximation.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from sklearn.utils._testing import assert_array_equal
88
from sklearn.utils._testing import assert_array_almost_equal
9+
from sklearn.utils._testing import assert_allclose
910

1011
from sklearn.metrics.pairwise import kernel_metrics
1112
from sklearn.kernel_approximation import RBFSampler
@@ -31,12 +32,10 @@ def test_polynomial_count_sketch_raises_if_degree_lower_than_one(degree):
3132
ps_transform.fit(X, Y)
3233

3334

34-
@pytest.mark.parametrize("X", [X, csr_matrix(X)])
35-
@pytest.mark.parametrize("Y", [Y, csr_matrix(Y)])
3635
@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):
4039
# test that PolynomialCountSketch approximates polynomial
4140
# kernel on random data
4241

@@ -45,7 +44,11 @@ def test_polynomial_count_sketch(X, Y, gamma, degree, coef0):
4544

4645 8000
# approximate kernel mapping
4746
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,
4952
)
5053
X_trans = ps_transform.fit_transform(X)
5154
Y_trans = ps_transform.transform(Y)
@@ -58,6 +61,29 @@ def test_polynomial_count_sketch(X, Y, gamma, degree, coef0):
5861
assert np.mean(error) <= 0.05 # mean is fairly close
5962

6063

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+
6187
def _linear_kernel(X, Y):
6288
return np.dot(X, Y.T)
6389

0 commit comments

Comments
 (0)
0