高斯混合模型椭球#
绘制使用期望最大化(GaussianMixture
类)和变分推断(BayesianGaussianMixture
类模型,具有狄利克雷过程先验)获得的两个高斯混合的置信椭球。
两种模型都可以访问五个组件来拟合数据。请注意,期望最大化模型必然会使用所有五个组件,而变分推断模型实际上只会使用尽可能多的组件来实现良好的拟合。在这里,我们可以看到期望最大化模型任意地拆分了一些组件,因为它试图拟合过多的组件,而狄利克雷过程模型会自动调整其状态数。
由于我们处于低维空间中,因此此示例并未显示这一点,但狄利克雷过程模型的另一个优点是,即使每个集群的示例少于数据中的维度,它也可以有效地拟合完整的协方差矩阵,这归功于推理算法的正则化特性。
/home/circleci/project/sklearn/mixture/_base.py:270: ConvergenceWarning:
Best performing initialization did not converge. Try different init parameters, or increase max_iter, tol, or check for degenerate data.
import itertools
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from scipy import linalg
from sklearn import mixture
color_iter = itertools.cycle(["navy", "c", "cornflowerblue", "gold", "darkorange"])
def plot_results(X, Y_, means, covariances, index, title):
splot = plt.subplot(2, 1, 1 + index)
for i, (mean, covar, color) in enumerate(zip(means, covariances, color_iter)):
v, w = linalg.eigh(covar)
v = 2.0 * np.sqrt(2.0) * np.sqrt(v)
u = w[0] / linalg.norm(w[0])
# as the DP will not use every component it has access to
# unless it needs it, we shouldn't plot the redundant
# components.
if not np.any(Y_ == i):
continue
plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], 0.8, color=color)
# Plot an ellipse to show the Gaussian component
angle = np.arctan(u[1] / u[0])
angle = 180.0 * angle / np.pi # convert to degrees
ell = mpl.patches.Ellipse(mean, v[0], v[1], angle=180.0 + angle, color=color)
ell.set_clip_box(splot.bbox)
ell.set_alpha(0.5)
splot.add_artist(ell)
plt.xlim(-9.0, 5.0)
plt.ylim(-3.0, 6.0)
plt.xticks(())
plt.yticks(())
plt.title(title)
# Number of samples per component
n_samples = 500
# Generate random sample, two components
np.random.seed(0)
C = np.array([[0.0, -0.1], [1.7, 0.4]])
X = np.r_[
np.dot(np.random.randn(n_samples, 2), C),
0.7 * np.random.randn(n_samples, 2) + np.array([-6, 3]),
]
# Fit a Gaussian mixture with EM using five components
gmm = mixture.GaussianMixture(n_components=5, covariance_type="full").fit(X)
plot_results(X, gmm.predict(X), gmm.means_, gmm.covariances_, 0, "Gaussian Mixture")
# Fit a Dirichlet process Gaussian mixture using five components
dpgmm = mixture.BayesianGaussianMixture(n_components=5, covariance_type="full").fit(X)
plot_results(
X,
dpgmm.predict(X),
dpgmm.means_,
dpgmm.covariances_,
1,
"Bayesian Gaussian Mixture with a Dirichlet process prior",
)
plt.show()
脚本总运行时间:(0 分钟 0.288 秒)
相关示例