Exemplo n.º 1
0
def test_pca_variance_after_trim():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    # set number of active components
    model.trim_components(5)
    # kept variance must be smaller than total variance
    assert(model.variance() < model.original_variance())
    # kept variance ratio must be smaller than 1.0
    assert(model.variance_ratio() < 1.0)
    # noise variance must be bigger than 0.0
    assert(model.noise_variance() > 0.0)
    # noise variance ratio must also be bigger than 0.0
    assert(model.noise_variance_ratio() > 0.0)
    # inverse noise variance is computable
    assert(model.inverse_noise_variance() == 1 / model.noise_variance())
Exemplo n.º 2
0
def test_pca_variance_after_trim():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    # set number of active components
    model.trim_components(5)
    # kept variance must be smaller than total variance
    assert model.variance() < model.original_variance()
    # kept variance ratio must be smaller than 1.0
    assert model.variance_ratio() < 1.0
    # noise variance must be bigger than 0.0
    assert model.noise_variance() > 0.0
    # noise variance ratio must also be bigger than 0.0
    assert model.noise_variance_ratio() > 0.0
    # inverse noise variance is computable
    assert model.inverse_noise_variance() == 1 / model.noise_variance()
Exemplo n.º 3
0
def test_pca_vector_init_from_covariance():
    n_samples = 30
    n_features = 10
    centre_values = [True, False]
    for centre in centre_values:
        # generate samples matrix and mean vector
        samples = np.random.randn(n_samples, n_features)
        mean = np.mean(samples, axis=0)
        # compute covariance matrix
        if centre:
            X = samples - mean
            C = np.dot(X.T, X) / (n_samples - 1)
        else:
            C = np.dot(samples.T, samples) / (n_samples - 1)
        # create the 2 pca models
        pca1 = PCAVectorModel.init_from_covariance_matrix(C,
                                                          mean,
                                                          centred=centre,
                                                          n_samples=n_samples)
        pca2 = PCAVectorModel(samples, centre=centre, inplace=False)
        # compare them
        assert_array_almost_equal(pca1.mean(), pca2.mean())
        assert_array_almost_equal(pca1.component(0, with_mean=False),
                                  pca2.component(0, with_mean=False))
        assert_array_almost_equal(pca1.component(7), pca2.component(7))
        assert_array_almost_equal(pca1.components, pca2.components)
        assert_array_almost_equal(pca1.eigenvalues, pca2.eigenvalues)
        assert_array_almost_equal(pca1.eigenvalues_cumulative_ratio(),
                                  pca2.eigenvalues_cumulative_ratio())
        assert_array_almost_equal(pca1.eigenvalues_ratio(),
                                  pca2.eigenvalues_ratio())
        weights = np.random.randn(pca1.n_active_components - 4)
        assert_array_almost_equal(pca1.instance(weights),
                                  pca2.instance(weights))
        assert pca1.n_active_components == pca2.n_active_components
        assert pca1.n_components == pca2.n_components
        assert pca1.n_features == pca2.n_features
        assert pca1.n_samples == pca2.n_samples
        assert pca1.noise_variance() == pca2.noise_variance()
        assert pca1.noise_variance_ratio() == pca2.noise_variance_ratio()
        assert_allclose(pca1.variance(), pca2.variance())
        assert pca1.variance_ratio() == pca2.variance_ratio()
        assert_array_almost_equal(pca1.whitened_components(),
                                  pca2.whitened_components())
Exemplo n.º 4
0
def test_pca_vector_init_from_covariance():
    n_samples = 30
    n_features = 10
    centre_values = [True, False]
    for centre in centre_values:
        # generate samples matrix and mean vector
        samples = np.random.randn(n_samples, n_features)
        mean = np.mean(samples, axis=0)
        # compute covariance matrix
        if centre:
            X = samples - mean
            C = np.dot(X.T, X) / (n_samples - 1)
        else:
            C = np.dot(samples.T, samples) / (n_samples - 1)
        # create the 2 pca models
        pca1 = PCAVectorModel.init_from_covariance_matrix(C, mean, centred=centre,
                                                          n_samples=n_samples)
        pca2 = PCAVectorModel(samples, centre=centre, inplace=False)
        # compare them
        assert_array_almost_equal(pca1.mean(), pca2.mean())
        assert_array_almost_equal(pca1.component(0, with_mean=False),
                                  pca2.component(0, with_mean=False))
        assert_array_almost_equal(pca1.component(7), pca2.component(7))
        assert_array_almost_equal(pca1.components, pca2.components)
        assert_array_almost_equal(pca1.eigenvalues, pca2.eigenvalues)
        assert_array_almost_equal(pca1.eigenvalues_cumulative_ratio(),
                                  pca2.eigenvalues_cumulative_ratio())
        assert_array_almost_equal(pca1.eigenvalues_ratio(),
                                  pca2.eigenvalues_ratio())
        weights = np.random.randn(pca1.n_active_components - 4)
        assert_array_almost_equal(pca1.instance(weights),
                                  pca2.instance(weights))
        assert(pca1.n_active_components == pca2.n_active_components)
        assert(pca1.n_components == pca2.n_components)
        assert(pca1.n_features == pca2.n_features)
        assert(pca1.n_samples == pca2.n_samples)
        assert(pca1.noise_variance() == pca2.noise_variance())
        assert(pca1.noise_variance_ratio() == pca2.noise_variance_ratio())
        assert_allclose(pca1.variance(), pca2.variance())
        assert(pca1.variance_ratio() == pca2.variance_ratio())
        assert_array_almost_equal(pca1.whitened_components(),
                                  pca2.whitened_components())