def test_bad_shapes():
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples, None, covariances)
    assert str(e.value) == "centers is of the incorrect shape"
    # Wrong Length
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples, [1], covariances)
    assert str(e.value) == \
        "The first dimensions of 2D centers and 3D covariances must be equal"
    # Inconsistent dimension
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples,
                              centers, [np.eye(2), np.eye(3)],
                              class_probs=class_probs)
    assert str(e.value) == "covariance matrix is of the incorrect shape"
    # Wrong uni dimensions
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples, [1, 0], [1, 0])
    assert str(e.value) == "covariance matrix is of the incorrect shape"
    # Wrong centerslti sizes
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples,
                              centers,
                              covariances,
                              class_probs=[0.3, 0.1, 0.6])
    assert str(e.value) == \
        "centers, covariances, and class_probs must be of equal length"
def test_bad_class_probs():
    with pytest.raises(ValueError) as e:
        make_gaussian_mixture(n_samples,
                              centers,
                              covariances,
                              class_probs=[0.3, 0.4])
    assert str(e.value) == "elements of `class_probs` must sum to 1"
def test_shuffle():
    np.random.seed(42)
    Xs_1, y_1 = make_gaussian_mixture(
        20,
        centers,
        covariances,
        class_probs=class_probs,
        transform='poly',
        random_state=42,
        shuffle=True,
        shuffle_random_state=42,
    )
    np.random.seed(30)
    Xs_2, y_2 = make_gaussian_mixture(
        20,
        centers,
        covariances,
        class_probs=class_probs,
        transform='poly',
        random_state=42,
        shuffle=True,
        shuffle_random_state=10,
    )
    for view1, view2 in zip(Xs_1, Xs_2):
        assert not np.allclose(view1, view2)
    assert not np.allclose(y_1, y_2)
def test_signal_noise_not_same_but_reproducible(transform):
    Xs_1, _ = make_gaussian_mixture(20,
                                    centers,
                                    covariances,
                                    class_probs=class_probs,
                                    random_state=42,
                                    transform=transform,
                                    noise=1)
    view1_noise, view2_noise = Xs_1[0], Xs_1[1]
    Xs_2, _ = make_gaussian_mixture(20,
                                    centers,
                                    covariances,
                                    class_probs=class_probs,
                                    random_state=42,
                                    transform=transform,
                                    noise=1)
    view1_noise2, view2_noise2 = Xs_2[0], Xs_2[1]
    # Noise is reproducible and signal is the same
    assert np.allclose(view1_noise, view1_noise2)
    assert np.allclose(view2_noise, view2_noise2)
    Xs_3, _ = make_gaussian_mixture(20,
                                    centers,
                                    covariances,
                                    class_probs=class_probs,
                                    random_state=42,
                                    transform=transform)
    view1_noise3, view2_noise3 = Xs_3[0], Xs_3[1]
    # Noise varies view1, but keeps view 2 unaffects (i.e. the latents)
    assert not np.allclose(view1_noise, view1_noise3)
    assert np.allclose(view2_noise, view2_noise3)
def test_random_state(noise):
    Xs_1, y_1 = make_gaussian_mixture(10,
                                      centers,
                                      covariances,
                                      class_probs=class_probs,
                                      transform='poly',
                                      random_state=42,
                                      noise=noise)
    Xs_2, y_2 = make_gaussian_mixture(10,
                                      centers,
                                      covariances,
                                      class_probs=class_probs,
                                      transform='poly',
                                      random_state=42,
                                      noise=noise)
    for view1, view2 in zip(Xs_1, Xs_2):
        assert np.allclose(view1, view2)
    assert np.allclose(y_1, y_2)
def test_noise_dims_not_same_but_reproducible():
    Xs_1, _ = make_gaussian_mixture(20,
                                    centers,
                                    covariances,
                                    class_probs=class_probs,
                                    random_state=42,
                                    transform="poly",
                                    noise_dims=2)
    view1_noise, view2_noise = Xs_1[0][:, -2:], Xs_1[1][:, -2:]
    assert not np.allclose(view1_noise, view2_noise)
    Xs_2, _ = make_gaussian_mixture(20,
                                    centers,
                                    covariances,
                                    class_probs=class_probs,
                                    random_state=42,
                                    transform="poly",
                                    noise_dims=2)
    view1_noise2, view2_noise2 = Xs_2[0][:, -2:], Xs_2[1][:, -2:]
    assert np.allclose(view1_noise, view1_noise2)
    assert np.allclose(view2_noise, view2_noise2)
def test_transforms(transform):
    Xs, y, latents = make_gaussian_mixture(n_samples,
                                           centers,
                                           covariances,
                                           class_probs=class_probs,
                                           return_latents=True,
                                           transform=transform,
                                           noise_dims=2)

    assert_equal(len(Xs), 2)
    assert_equal(Xs[0].shape, (n_samples, 4))
    assert_equal(Xs[1].shape, (n_samples, 4))
def test_formats(centers, covariances, class_probs):
    Xs, y, latents = make_gaussian_mixture(n_samples,
                                           centers,
                                           covariances,
                                           class_probs=class_probs,
                                           return_latents=True)
    assert_equal(n_samples, len(latents))
    assert_equal(len(covariances[0]), latents.shape[1])
    assert_equal(Xs[0], latents)

    if class_probs is not None:
        for i, p in enumerate(class_probs):
            assert_equal(int(p * n_samples), list(y).count(i))
示例#9
0
algorithm, one is interested in visualizing two views across dimensions.
One use is assessing correlation between corresponding dimensions of views.
Here, we use this function to display the relationship between two views
simulated from transformations of multi-variant gaussians.

"""

# License: MIT

from mvlearn.datasets import make_gaussian_mixture
from mvlearn.plotting import crossviews_plot
import numpy as np

n_samples = 100
centers = [[0, 1], [0, -1]]
covariances = [np.eye(2), np.eye(2)]
Xs, y = make_gaussian_mixture(n_samples,
                              centers,
                              covariances,
                              transform='poly',
                              noise_dims=2)

# Below, we see that the first two dimensions are related by a degree 2
# polynomial while the latter two dimensions are uncorrelated.

crossviews_plot(Xs,
                labels=y,
                title='View 1 vs. View 2 (Polynomial \
                    Transform + noise)',
                equal_axes=True)
示例#10
0
import matplotlib
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")

# GMM settings
n_samples = 200
centers = [[0, 0], [0, 0]]
covariances = 4*np.array([np.eye(2), np.eye(2)])
transforms = ['linear', 'poly', 'sin']

Xs_train_sets = []
Xs_test_sets = []
for transform in transforms:
    Xs_train, _ = make_gaussian_mixture(
        n_samples, centers, covariances, transform=transform, noise=0.25,
        noise_dims=2, random_state=41)
    Xs_test, _, latents = make_gaussian_mixture(
        n_samples, centers, covariances, transform=transform, noise=0.25,
        noise_dims=2, random_state=42, return_latents=True)

    Xs_train_sets.append(Xs_train)
    Xs_test_sets.append(Xs_test)


# Plotting parameters
labels = latents[:, 0]
cmap = matplotlib.colors.ListedColormap(
    sns.diverging_palette(240, 10, n=len(labels), center='light').as_hex())
cmap = 'coolwarm'
def test_bad_transform_type(transform):
    with pytest.raises(ValueError):
        make_gaussian_mixture(n_samples,
                              centers,
                              covariances,
                              transform=transform)
示例#12
0
import numpy as np
from mvlearn.datasets import make_gaussian_mixture
from mvlearn.plotting import crossviews_plot

# Latent variables are sampled from two multivariate Gaussians with equal
# prior probability. Then a polynomial transformation is applied and noise
# is added independently to both the transformed and untransformed latents.

n_samples = 100
centers = [[0, 1], [0, -1]]
covariances = [np.eye(2), np.eye(2)]
Xs, y, latent = make_gaussian_mixture(n_samples,
                                      centers,
                                      covariances,
                                      random_state=42,
                                      noise_dims=2,
                                      shuffle=True,
                                      shuffle_random_state=42,
                                      transform='poly',
                                      return_latents=True)

# The latent data is plotted against itself to reveal the underlying
# distribtution.

crossviews_plot([latent, latent],
                labels=y,
                title='Latent Variable',
                equal_axes=True)

# The noisy latent variable (view 1) is plotted against the transformed latent
# variable (view 2), an example of a dataset with two views.