高斯混合模型
sklearn.mixture
是一个能够学习高斯混合模型、抽样高斯模型和从数据中估计模型的包。同样,也提供了帮助决定正确组件数量的方法。
一个高斯混合模型是一个概率模型,它假设所有的数据点是从有限未知参数的高斯分布的混合生成的。可以将混合模型当作泛化的k均值聚类,以融合关于数据协方差和潜在高斯中心的信息。
高斯混合
GaussianMixture
对象实现了expection-maximization
算法来拟合高斯混合模型。它也能够得到多元模型的置信椭圆,计算贝叶斯信息准则来确定数据中聚集类别的数量。GaussianMixture.fit
方法从训练数据中学习一个高斯混合模型,GaussianMixture.predict
能够分配给每个样本最大可能属于的高斯分布。
GaussianMixture
提供了不同的选项来限制不同类别估计的方差,包括,spherical、diagonal、tied或full方差。
# coding: utf-8
# GMM covariances
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.mixture import GaussianMixture
from sklearn.model_selection import StratifiedKFold
colors = ['navy', 'turquoise', 'darkorange']
def make_ellipses(gmm, ax):
for n, color in enumerate(colors):
if gmm.covariance_type == 'full':
covariances = gmm.covariances_[n][:2, :2]
elif gmm.covariance_type == 'tied':
covariances = gmm.covariances_[:2, :2]
elif gmm.covariance_type == 'diag':
covariances = np.diag(gmm.covariances_[n][:2])
elif gmm.covariance_type == 'spherical':
covariances = np.eye(gmm.means_.shape[1]) * gmm.covariances_[n]
v, w = np.linalg.eigh(covariances)
u = w[0] / np.linalg.norm(w[0])
angle = np.arctan2(u[1], u[0])
angle = 180 * angle / np.pi
v = 2. * np.sqrt(2.) * np.sqrt(v)
ell = mpl.patches.Ellipse(gmm.means_[n, :2], v[0], v[1],
180+angle, color=color)
ell.set_clip_box(ax.bbox)
ell.set_alpha(0.5)
ax.add_artist(ell)
ax.set_aspect('equal', 'datalim')
iris = datasets.load_iris()
skf = StratifiedKFold(n_splits=4)
train_index, test_index = next(iter(skf.split(iris.data, iris.target)))
X_train = iris.data[train_index]
y_train = iris.target[train_index]
X_test = iris.data[test_index]
y_test = iris.target[test_index]
n_classes = len(np.unique(y_train))
estimators = {cov_type: GaussianMixture(n_components=n_classes, covariance_type=cov_type, max_iter=20, random_state=0)
for cov_type in ['spherical', 'diag', 'tied', 'full']}
n_estimators = len(estimators)
plt.figure(figsize=(3 * n_estimators // 2, 6))
plt.subplots_adjust(bottom=0.01, top=0.95, hspace=0.15, wspace=0.05, left=0.01, right=0.99)
for index, (name, estimator) in enumerate(estimators.items()):
estimator.means_init = np.array([X_train[y_train == i].mean(axis=0)
for i in range(n_classes)])
estimator.fit(X_train)
h = plt.subplot(2, n_estimators//2, index + 1)
make_ellipses(estimator, h)
for n, color in enumerate(colors):
data = iris.data[iris.target == n]
plt.scatter(data[:, 0], data[:, 1], s=0.8, color=color, label=iris.target_names[n])
for n, color in enumerate(colors):
data = X_test[y_test == n]
plt.scatter(data[:, 0], data[:, 1], marker='x', color=color)
y_train_pred = estimator.predict(X_train)
train_accuracy = np.mean(y_train_pred.ravel() == y_train.ravel()) * 100
plt.text(0.05, 0.9, 'Train accuracy: %.1f' % train_accuracy,
transform=h.transAxes)
y_test_pred = estimator.predict(X_test)
test_accuracy = np.mean(y_test_pred.ravel() == y_test.ravel()) * 100
plt.text(0.05, 0.8, 'Test accuracy: %.1f' % test_accuracy,
transform=h.transAxes)
plt.xticks(())
plt.yticks(())
plt.title(name)
plt.legend(scatterpoints=1, loc='lower right', prop=dict(size=12))
plt.show()
变分贝叶斯高斯混合
BayesianGaussianMixture
对象实现了一系列考虑不同推断算法的高斯混合模型。
估计算法:变分推断
变分推断(Variational Inference
)是最大期望的扩展,它最大化模型证据的下界,而不是数据似然。其背后的原理与最大期望方法相同。但是变分推断方法通过集成先验分布的信息添加正则项。这可以避免在最大期望中经常发生的奇异性,但会引入偏差到模型中。
BayesianGaussianMixture
类提供了两类权重的先验:使用Dirichlet
分布的有限混合模型和使用Dirichlet
过程的无限混合模型。
# coding: utf-8
# Concentration Prior Type Analysis of Variation Bayesian Gaussian Mixture
import numpy as np
from sklearn.mixture import BayesianGaussianMixture
random_state, n_components, n_features = 2, 3, 2
covars = np.array([[[.7, .0], [.0, .1]],
[[.5, .0], [.0, .1]],
[[.5, .0], [.0, .1]]])
samples = np.array([200, 500, 200])
means = np.array([[.0, -.70],
[.0, .0],
[.0, .70]])
estimators = [
("Finite mixture with a Dirichlet distribution\nprior and "
r"$\gamma_0=$", BayesianGaussianMixture(
weight_concentration_prior_type="dirichlet_distribution",
n_components=2 * n_components, reg_covar=0, init_params='random',
max_iter=1500, mean_precision_prior=.8,
random_state=random_state), [0.001, 1, 1000]),
("Infinite mixture with a Dirichlet process\n prior and" r"$\gamma_0=$",
BayesianGaussianMixture(
weight_concentration_prior_type="dirichlet_process",
n_components=2 * n_components, reg_covar=0, init_params='random',
max_iter=1500, mean_precision_prior=.8,
random_state=random_state), [1, 1000, 100000])]
rng = np.random.RandomState(random_state)
X = np.vstack([
rng.multivariate_normal(means[j], covars[j], samples[j])
for j in range(n_components)])
y = np.concatenate([np.full(samples[j], j, dtype=int)
for j in range(n_components)])
for (title, estimator, concentrations_prior) in estimators:
for k, concentration in enumerate(concentrations_prior):
estimator.weight_concentration_prior = concentration
estimator.fit(X)
print(estimator.weights_)