def test_polynomial_count_sketch_dense_sparse(gamma, degree, coef0): """Check that PolynomialCountSketch results are the same for dense and sparse input. """ ps_dense = PolynomialCountSketch(n_components=500, gamma=gamma, degree=degree, coef0=coef0, random_state=42) Xt_dense = ps_dense.fit_transform(X) Yt_dense = ps_dense.transform(Y) ps_sparse = PolynomialCountSketch(n_components=500, gamma=gamma, degree=degree, coef0=coef0, random_state=42) Xt_sparse = ps_sparse.fit_transform(csr_matrix(X)) Yt_sparse = ps_sparse.transform(csr_matrix(Y)) assert_allclose(Xt_dense, Xt_sparse) assert_allclose(Yt_dense, Yt_sparse)
def test_polynomial_count_sketch(X, Y, gamma, degree, coef0): # test that PolynomialCountSketch approximates polynomial # kernel on random data # compute exact kernel kernel = polynomial_kernel(X, Y, gamma=gamma, degree=degree, coef0=coef0) # approximate kernel mapping ps_transform = PolynomialCountSketch(n_components=5000, gamma=gamma, coef0=coef0, degree=degree, random_state=42) X_trans = ps_transform.fit_transform(X) Y_trans = ps_transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) error = kernel - kernel_approx assert np.abs(np.mean(error)) <= 0.05 # close to unbiased np.abs(error, out=error) assert np.max(error) <= 0.1 # nothing too far off assert np.mean(error) <= 0.05 # mean is fairly close