当前位置: 首页>>代码示例>>Python>>正文


Python GaussianMixture.sample方法代码示例

本文整理汇总了Python中sklearn.mixture.gaussian_mixture.GaussianMixture.sample方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianMixture.sample方法的具体用法?Python GaussianMixture.sample怎么用?Python GaussianMixture.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.mixture.gaussian_mixture.GaussianMixture的用法示例。


在下文中一共展示了GaussianMixture.sample方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_sample

# 需要导入模块: from sklearn.mixture.gaussian_mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.gaussian_mixture.GaussianMixture import sample [as 别名]
def test_sample():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7, n_components=3)
    n_features, n_components = rand_data.n_features, rand_data.n_components

    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]

        gmm = GaussianMixture(n_components=n_components,
                              covariance_type=covar_type, random_state=rng)
        # To sample we need that GaussianMixture is fitted
        assert_raise_message(NotFittedError, "This GaussianMixture instance "
                             "is not fitted", gmm.sample, 0)
        gmm.fit(X)

        assert_raise_message(ValueError, "Invalid value for 'n_samples",
                             gmm.sample, 0)

        # Just to make sure the class samples correctly
        n_samples = 20000
        X_s, y_s = gmm.sample(n_samples)

        for k in range(n_components):
            if covar_type == 'full':
                assert_array_almost_equal(gmm.covariances_[k],
                                          np.cov(X_s[y_s == k].T), decimal=1)
            elif covar_type == 'tied':
                assert_array_almost_equal(gmm.covariances_,
                                          np.cov(X_s[y_s == k].T), decimal=1)
            elif covar_type == 'diag':
                assert_array_almost_equal(gmm.covariances_[k],
                                          np.diag(np.cov(X_s[y_s == k].T)),
                                          decimal=1)
            else:
                assert_array_almost_equal(
                    gmm.covariances_[k], np.var(X_s[y_s == k] - gmm.means_[k]),
                    decimal=1)

        means_s = np.array([np.mean(X_s[y_s == k], 0)
                           for k in range(n_components)])
        assert_array_almost_equal(gmm.means_, means_s, decimal=1)

        # Check shapes of sampled data, see
        # https://github.com/scikit-learn/scikit-learn/issues/7701
        assert_equal(X_s.shape, (n_samples, n_features))

        for sample_size in range(1, 100):
            X_s, _ = gmm.sample(sample_size)
            assert_equal(X_s.shape, (sample_size, n_features))
开发者ID:jerry-dumblauskas,项目名称:scikit-learn,代码行数:51,代码来源:test_gaussian_mixture.py

示例2: test_sample

# 需要导入模块: from sklearn.mixture.gaussian_mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.gaussian_mixture.GaussianMixture import sample [as 别名]
def test_sample():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7)
    n_features, n_components = rand_data.n_features, rand_data.n_components

    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]

        gmm = GaussianMixture(n_components=n_components,
                              covariance_type=covar_type, random_state=rng)
        # To sample we need that GaussianMixture is fitted
        assert_raise_message(NotFittedError, "This GaussianMixture instance "
                             "is not fitted", gmm.sample, 0)
        gmm.fit(X)

        assert_raise_message(ValueError, "Invalid value for 'n_samples",
                             gmm.sample, 0)

        # Just to make sure the class samples correctly
        X_s, y_s = gmm.sample(20000)
        for k in range(n_features):
            if covar_type == 'full':
                assert_array_almost_equal(gmm.covariances_[k],
                                          np.cov(X_s[y_s == k].T), decimal=1)
            elif covar_type == 'tied':
                assert_array_almost_equal(gmm.covariances_,
                                          np.cov(X_s[y_s == k].T), decimal=1)
            elif covar_type == 'diag':
                assert_array_almost_equal(gmm.covariances_[k],
                                          np.diag(np.cov(X_s[y_s == k].T)),
                                          decimal=1)
            else:
                assert_array_almost_equal(
                    gmm.covariances_[k], np.var(X_s[y_s == k] - gmm.means_[k]),
                    decimal=1)

        means_s = np.array([np.mean(X_s[y_s == k], 0)
                           for k in range(n_features)])
        assert_array_almost_equal(gmm.means_, means_s, decimal=1)
开发者ID:d-behi,项目名称:scikit-learn,代码行数:41,代码来源:test_gaussian_mixture.py


注:本文中的sklearn.mixture.gaussian_mixture.GaussianMixture.sample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。