Exemplo n.º 1
0
def test_anova_kernel(dist, degree, kernel):
    # compute exact kernel
    gram = anova(X, Y, degree)
    # approximate kernel mapping
    rk_transform = RandomKernel(n_components=1000, random_state=0,
                                kernel=kernel, degree=degree,
                                distribution=dist, p_sparse=0.5)

    X_trans = rk_transform.fit_transform(X)
    Y_trans = rk_transform.transform(Y)
    kernel_approx = np.dot(X_trans, Y_trans.T)
    error =  gram - kernel_approx
    assert np.abs(np.mean(error)) < 0.0001
    assert np.max(error) < 0.001  # nothing too far off
    assert np.mean(error) < 0.0005  # mean is fairly close

    # sparse input
    X_trans_sp = rk_transform.transform(X_sp)
    assert_allclose_dense_sparse(X_trans, X_trans_sp)

    # sparse output
    if dist == "sparse_rademacher":
        rk_transform.dense_output = False
        X_trans_sp = rk_transform.transform(X_sp)
        assert issparse(X_trans_sp)
        assert_allclose_dense_sparse(X_trans, X_trans_sp.toarray())
    else:
        rk_transform.dense_output = False
        X_trans_sp = rk_transform.transform(X_sp)
        assert not issparse(X_trans_sp)
        assert_allclose_dense_sparse(X_trans, X_trans_sp)
def test_anova_kernel_sparse_subset(degree):
    # compute exact kernel
    n_components = 2000 * 5
    n_sub_features = 25
    gram = anova(X_sp, Y_sp, degree, True)
    # approximate kernel mapping
    rk_transform = SubfeatureRandomKernel(n_components=n_components,
                                          random_state=rng,
                                          kernel='anova',
                                          degree=degree,
                                          distribution="rademacher",
                                          n_sub_features=n_sub_features)
    X_trans = rk_transform.fit_transform(X_sp)
    Y_trans = rk_transform.transform(Y_sp)
    assert_almost_equal(rk_transform.random_weights_.nnz,
                        n_components * n_sub_features)

    kernel_approx = safe_sparse_dot(X_trans, Y_trans.T, dense_output=True)
    error = gram - kernel_approx
    assert np.abs(np.mean(error)) < 0.001
    assert np.max(error) < 0.1  # nothing too far off
    assert np.mean(error) < 0.005  # mean is fairly close
    assert_almost_equal(n_sub_features * n_components,
                        rk_transform.random_weights_.nnz)
    assert_allclose_dense_sparse(
        n_sub_features * np.ones(n_components),
        np.array(abs(rk_transform.random_weights_).sum(axis=0))[0])
Exemplo n.º 3
0
def test_anova_kernel(degree):
    expected = np.zeros((X.shape[0], Y.shape[0]))
    for i in range(X.shape[0]):
        for j in range(Y.shape[0]):
            expected[i, j] = dumb_anova(X[i], Y[j], degree=degree)

    anova = pyrfm.anova(X, Y, degree)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova_fast(X, Y, degree)
    assert_array_almost_equal(expected, anova, decimal=4)
def test_anova_kernel(degree, kernel):
    # compute exact kernel
    gram = anova(X, Y, degree)
    # approximate kernel mapping
    rk_transform = SubfeatureRandomKernel(n_components=1000,
                                          random_state=rng,
                                          kernel=kernel,
                                          degree=degree,
                                          distribution="rademacher",
                                          n_sub_features=25)
    X_trans = rk_transform.fit_transform(X)
    Y_trans = rk_transform.transform(Y)
    kernel_approx = np.dot(X_trans, Y_trans.T)

    error = gram - kernel_approx
    assert np.abs(np.mean(error)) < 0.001
    assert np.max(error) < 0.01  # nothing too far off
    assert np.mean(error) < 0.005  # mean is fairly close
Exemplo n.º 5
0
def test_anova_kernel(degree):
    # compute exact kernel
    kernel = anova(X, Y, degree)
    # approximate kernel mapping
    rk_transform = SignedCirculantRandomKernel(n_components=1000,
                                               random_state=rng,
                                               kernel='anova',
                                               degree=degree)
    X_trans = rk_transform.fit_transform(X)
    Y_trans = rk_transform.transform(Y)
    kernel_approx = np.dot(X_trans, Y_trans.T)

    error = kernel - kernel_approx
    assert np.abs(np.mean(error)) < 0.0001
    assert np.max(error) < 0.001  # nothing too far off
    assert np.mean(error) < 0.0005  # mean is fairly close

    X_trans_sp = rk_transform.transform(X_sp)
    assert_allclose_dense_sparse(X_trans, X_trans_sp)
Exemplo n.º 6
0
def test_anova_kernel_sparse(degree):
    expected = np.zeros((X.shape[0], Y.shape[0]))
    for i in range(X.shape[0]):
        for j in range(Y.shape[0]):
            expected[i, j] = dumb_anova(X[i], Y[j], degree=degree)

    anova = pyrfm.anova(csr_matrix(X), Y, degree, True)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova(csr_matrix(X), Y, degree, False)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova(X, csr_matrix(Y), degree, True)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova(X, csr_matrix(Y), degree, False)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova(csr_matrix(X), csr_matrix(Y), degree, True)
    assert_array_almost_equal(expected, anova, decimal=4)

    anova = pyrfm.anova(csr_matrix(X), csr_matrix(Y), degree, False)
    print(type(anova))
    assert_array_almost_equal(expected, anova.toarray(), decimal=4)