예제 #1
0
def test_kmcca_n_components():
    Xs = next(generate_mcca_test_data())
    n_features = sum([X.shape[1] for X in Xs])
    kmcca = KMCCA(n_components=n_features + 1)
    with pytest.raises(AttributeError):
        kmcca.n_components_
    with pytest.warns(None):
        kmcca.fit(Xs)
    assert kmcca.n_components_ == n_features
    for load in kmcca.dual_vars_:
        assert kmcca.n_components_ == load.shape[1]
예제 #2
0
def test_pgso():
    Xs = next(generate_mcca_test_data())
    N = Xs[0].shape[0]
    ranks = []
    corrs = []
    for tol in [0, 0.1, 0.5, 1]:
        kmcca = KMCCA(kernel='rbf', pgso=True, tol=tol, n_components=2)
        scores = kmcca.fit(Xs).transform(Xs)
        # kmcca2 = KMCCA(kernel='rbf', pgso=True, tol=tol)
        # scores2 = kmcca2.fit(Xs).transform(Xs)
        for v in range(len(Xs)):
            assert len(
                set([
                    kmcca.pgso_Ls_[v].shape[1], kmcca.pgso_norms_[v].shape[0],
                    kmcca.pgso_idxs_[v].shape[0], kmcca.pgso_Xs_[v].shape[0]
                ])) == 1
            R = kmcca._get_kernel(Xs[v], v) - \
                kmcca.pgso_Ls_[v] @ kmcca.pgso_Ls_[v].T
            R = R - kmcca.kernel_col_means_[v] - \
                kmcca.kernel_col_means_[v].T + kmcca.kernel_mat_means_[v]
            assert np.trace(R) / N <= tol
        ranks.append(kmcca.pgso_ranks_)
        corrs.append(kmcca.canon_corrs(scores))
        if tol == 0:
            assert np.all(ranks[-1] == [N for _ in Xs])
    assert np.all(np.diff(corrs, axis=0) <= 1e-10), corrs
    assert np.all(np.diff(corrs, axis=1) <= 1e-10), corrs
    assert np.all(np.diff(ranks, axis=0) <= 1e-10), ranks