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


Python GaussianMixture.covariances_init方法代码示例

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


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

示例1: test_check_covariances

# 需要导入模块: from sklearn.mixture.gaussian_mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.gaussian_mixture.GaussianMixture import covariances_init [as 别名]
def test_check_covariances():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components, n_features = rand_data.n_components, rand_data.n_features

    # Define the bad covariances for each covariance_type
    covariances_bad_shape = {
        'full': rng.rand(n_components + 1, n_features, n_features),
        'tied': rng.rand(n_features + 1, n_features + 1),
        'diag': rng.rand(n_components + 1, n_features),
        'spherical': rng.rand(n_components + 1)}

    # Define not positive-definite covariances
    covariances_not_pos = rng.rand(n_components, n_features, n_features)
    covariances_not_pos[0] = np.eye(n_features)
    covariances_not_pos[0, 0, 0] = -1.

    covariances_not_positive = {
        'full': covariances_not_pos,
        'tied': covariances_not_pos[0],
        'diag': -1. * np.ones((n_components, n_features)),
        'spherical': -1. * np.ones(n_components)}

    not_positive_errors = {
        'full': 'symmetric, positive-definite',
        'tied': 'symmetric, positive-definite',
        'diag': 'positive',
        'spherical': 'positive'}

    for cov_type in ['full', 'tied', 'diag', 'spherical']:
        X = rand_data.X[cov_type]
        g = GaussianMixture(n_components=n_components,
                            covariance_type=cov_type)

        # Check covariance with bad shapes
        g.covariances_init = covariances_bad_shape[cov_type]
        assert_raise_message(ValueError,
                             "The parameter '%s covariance' should have "
                             "the shape of" % cov_type,
                             g.fit, X)

        # Check not positive covariances
        g.covariances_init = covariances_not_positive[cov_type]
        assert_raise_message(ValueError,
                             "'%s covariance' should be %s"
                             % (cov_type, not_positive_errors[cov_type]),
                             g.fit, X)

        # Check the correct init of covariances_init
        g.covariances_init = rand_data.covariances[cov_type]
        g.fit(X)
        assert_array_equal(rand_data.covariances[cov_type], g.covariances_init)
开发者ID:123fengye741,项目名称:scikit-learn,代码行数:55,代码来源:test_gaussian_mixture.py


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