Exemple #1
0
# Split each dataset into a training set and test set (10% of dataset is training data)
train1 = data1[:int(nSamples / 10)]
train2 = data2[:int(nSamples / 10)]
test1 = data1[int(nSamples / 10):]
test2 = data2[int(nSamples / 10):]

n_components = 4

# Initialize a linear kCCA class
kcca_l = KCCA(ktype="linear", reg=0.001, n_components=n_components)

# Use the methods to find a kCCA mapping and transform the views of data
kcca_ft = kcca_l.fit_transform([train1, train2])
kcca_f = kcca_l.fit([train1, train2])
kcca_t = kcca_l.transform([train1, train2])


# Test that cancorrs_ is equal to n_components
def test_numCC_cancorrs_():
    assert len(kcca_ft.cancorrs_) == n_components


# Test that number of views is equal to number of ws_
def test_numCC_ws_():
    assert len(kcca_ft.weights_) == 2


# Test that number of views is equal to number of comps_
def test_numCC_comps_():
    assert len(kcca_ft.components_) == 2
Exemple #2
0
def test_no_weights():
    with pytest.raises(NameError):
        kcca_b = KCCA(ktype="linear", reg=0.001, n_components=1)
        kcca_b.transform([train1, train2])
# sinuisoidally related. The data has 100 samples and thus the fully decomposed
# kernel matrix would have dimensions (100, 100). Instead we implement ICD with
# a kernel matrix of rank 50 (mrank = 50).

np.random.seed(1)
Xsg = make_data('gaussian', 100)

crossviews_plot(Xsg, ax_ticks=False, ax_labels=True, equal_axes=True)

###############################################################################
# Full Decomposition
# ^^^^^^^^^^^^^^^^^^

kcca_g = KCCA(ktype="gaussian", n_components=2, reg=0.01)
kcca_g.fit(Xsg)
gausskcca = kcca_g.transform(Xsg)

crossviews_plot(gausskcca, ax_ticks=False, ax_labels=True, equal_axes=True)

(gr1, _) = stats.pearsonr(gausskcca[0][:, 0], gausskcca[1][:, 0])
(gr2, _) = stats.pearsonr(gausskcca[0][:, 1], gausskcca[1][:, 1])

print("Below are the canonical correlation of the two components:")
print(gr1, gr2)

###############################################################################
# ICD Decomposition
# ^^^^^^^^^^^^^^^^^

kcca_g_icd = KCCA(ktype="gaussian",
                  sigma=1.0,