本文整理汇总了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)