主要内容

群集使用高斯混合模型

本主题使用统计和机器学习工具箱™功能,提供与高斯混合模型(GMM)进行聚类介绍cluster以及显示在拟合GMM模型时指定可选参数的效果的示例fitgmdist

高斯混合如何模拟群集数据

高斯混合模型(GMMS)通常用于数据聚类。您可以使用GMMS执行难的聚类或者soft查询数据上的群集。

去表演难的clustering, the GMM assigns query data points to the multivariate normal components that maximize the component posterior probability, given the data. That is, given a fitted GMM,cluster将查询数据分配给产生最高后概率的组件。硬群集团将数据点分配到一个群集。有关如何将GMM拟合到数据的示例,群集使用拟合模型,并估算组件后续概率,请参阅使用硬群体群集高斯混合数据

Additionally, you can use a GMM to perform a more flexible clustering on data, referred to assoft(or模糊)聚类。软群集方法为每个群集分配给数据点的分数。分数的值表示数据点对群集的关联强度。与硬群集方法相反,软群集方法是灵活的,因为它们可以将数据点分配到多个群集。执行GMM群集时,分数是后验概率。有关GMM的软群的示例,请参阅使用软群集群集高斯混合数据

GMM聚类可以容纳具有不同尺寸和相关结构的群集。因此,在某些应用中,GMM聚类可以比诸如的方法更合适k- eans集群。与许多聚类方法一样,GMM群集要求您在拟合模型之前指定群集的数量。群集数指定GMM中的组件数。

For GMMs, follow these best practices:

  • Consider the component covariance structure. You can specify diagonal or full covariance matrices, and whether all components have the same covariance matrix.

  • Specify initial conditions. The Expectation-Maximization (EM) algorithm fits the GMM. As in thek- emeans聚类算法,EM对初始条件敏感,可能会收敛到本地最佳状态。您可以为参数指定自己的起始值,为数据点指定初始群集分配,也可以随机选择它们,或指定使用k-means ++算法

  • Implement regularization. For example, if you have more predictors than data points, then you can regularize for estimation stability.

使用不同的协方差选项和初始条件适合GMM

This example explores the effects of specifying different options for covariance structure and initial conditions when you perform GMM clustering.

加载fisher的虹膜数据集。考虑聚类萼片测量,并使用Sepal测量来可视化2-D中的数据。

加载渔民;X = MEAS(:,1:2);[n,p] = size(x);绘图(x(:,1),x(:,2),'.',的标记Size',15); title('Fisher''s Iris Data Set');xlabel('Sepal length (cm)');ylabel('Sepal width (cm)');

图包含轴对象。具有标题Fisher的IRIS数据集的轴对象包含类型线的对象。

组件数量k在GMM中确定子步骤数或群集的数量。在该图中,难以确定是否有两个,三个或可能更多高斯组件是合适的。GMM的复杂性增加k增加。

指定不同的协方差结构选项

Each Gaussian component has a covariance matrix. Geometrically, the covariance structure determines the shape of a confidence ellipsoid drawn over a cluster. You can specify whether the covariance matrices for all components are diagonal or full, and whether all components have the same covariance matrix. Each combination of specifications determines the shape and orientation of the ellipsoids.

为EM算法指定三个GMM组件和1000个最大迭代。为了再现性,设置随机种子。

RNG(3);k = 3;%GMM组件的数量选项= statset('maxiter',1000);

Specify covariance structure options.

sigma = {'对角线','满的'};Covariance矩阵类型的%选项nsigma = numel(sigma);sharedcovariance = {true,false};% Indicator for identical or nonidentical covariance matricesSCtext = {'真的','错误的'};nSC = numel(SharedCovariance);

创建一个2-D网格,覆盖由极端测量的平面。稍后将使用这个网格,以吸引群集的信心椭圆体。

d = 500;%网格长度x1 = linspace(min(X(:,1))-2, max(X(:,1))+2, d); x2 = linspace(min(X(:,2))-2, max(X(:,2))+2, d); [x1grid,x2grid] = meshgrid(x1,x2); X0 = [x1grid(:) x2grid(:)];

Specify the following:

  • 对于协方差结构选项的所有组合,适合三个组件的GMM。

  • Use the fitted GMM to cluster the 2-D grid.

  • 获取指定每个置信区的99%概率阈值的分数。本说明书确定椭圆体的主要和次轴的长度。

  • 使用与其群集相似的颜色颜色每个椭圆体。

阈值= SQRT(Chi2inv(0.99,2));count = 1;为了我= 1:nsigma为了j = 1:nsc gmfit = fitgmdist(x,k,'CovarianceType',sigma {i},。。。'SharedCovariance',SharedCovariance{j},'选项',选项);%合适的GMMclusterx = cluster(gmfit,x);%群集索引mahaldist =玛哈尔(Gmfit,x0);% Distance from each grid point to each GMM component%在每个GMM组件上绘制椭圆体,并显示聚类结果。子图(2,2,计数);h1 = g箭偶(x(:,1),x(:,2),clusterx);抓住为了m = 1:k idx = mahaldist(:,m)<=阈值;颜色= H1(m).color * 0.75  -  0.5 *(H1(m).color  -  1);h2 = plot(x0(idx,1),x0(idx,2),'.','颜色',Color,的标记Size',1); uistack(h2,'bottom');结尾plot(gmfit.mu(:,1),gmfit.mu(:,2),'kx','行宽'2,的标记Size',10) title(sprintf('Sigma is %s\nSharedCovariance = %s',sigma {i},SCtext{j}),'字体大小',8)传奇(H1,{'1','2','3'}) 抓住离开count = count + 1;结尾结尾

图包含4个轴对象。Axes object 1 with title Sigma is diagonal SharedCovariance = true contains 7 objects of type line. These objects represent 1, 2, 3. Axes object 2 with title Sigma is diagonal SharedCovariance = false contains 7 objects of type line. These objects represent 1, 2, 3. Axes object 3 with title Sigma is full SharedCovariance = true contains 7 objects of type line. These objects represent 1, 2, 3. Axes object 4 with title Sigma is full SharedCovariance = false contains 7 objects of type line. These objects represent 1, 2, 3.

置信区的概率阈值确定主轴和次轴的长度,协方差类型确定轴的方向。注意以下关于协方差矩阵的选项:

  • Diagonal covariance matricesindicate that the predictors are uncorrelated. The major and minor axes of the ellipses are parallel or perpendicular to thexy轴。这个规范增加总number of parameters by p , the number of predictors, for each component, but is more parsimonious than the full covariance specification.

  • 完整的协方差矩阵允许与椭圆相对于椭圆的方向没有限制的相关预测器xy轴。每个组件增加参数总数 p ( p + 1 ) / 2 ,但捕获预测器之间的相关结构。该规范可能导致过度装备。

  • Shared covariance matrices表明所有组件都具有相同的协方差矩阵。所有椭圆均具有相同的尺寸,具有相同的方向。该规范比未共享的规范更加解散,因为参数总数增加了一个组件的协方差参数的数量增加。

  • 非共同的协方差矩阵表明每个组件都有自己的协方差矩阵。所有椭圆的大小和方向可能不同。本说明书增加了参数的数量k倍增组件的协方差参数的数量,但可以捕获组件之间的协方差差异。

该图还显示了这一点cluster并不总是保留集群顺序。如果你聚集了几个安装GMDistribution.models,clustercan assign different cluster labels for similar components.

指定不同的初始条件

适合GMM到数据的算法可以对初始条件敏感。为了说明这种灵敏度,适合四种不同的GMM,如下所示:

  1. For the first GMM, assign most data points to the first cluster.

  2. For the second GMM, randomly assign data points to clusters.

  3. 对于第三种GMM,将数据点的另一个随机分配给集群。

  4. For the fourth GMM, usek-means ++获取初始集群中心。

initialcond1 = [into(n-8,1);[2;2;2;2];[3;3;3;3];PROD GMM的百分比initialcond2 = randsample(1:k,n,true);第二种GMM的百分比initialCond3 = randsample(1:k,n,true);第三种GMM的百分比initialcond4 ='加';% For the fourth GMMcluster0 = {initialCond1;initialcond2;initialcond3;initialCond4};

对于所有实例,使用k= 3 components, unshared and full covariance matrices, the same initial mixture proportions, and the same initial covariance matrices. For stability when you try different sets of initial values, increase the number of EM algorithm iterations. Also, draw confidence ellipsoids over the clusters.

融合=南(4,1);为了j = 1:4 Gmfit = fitgmdist(x,k,'CovarianceType','满的',。。。'SharedCovariance',错误的,'Start',cluster0 {j},。。。'选项',选项);clusterx = cluster(gmfit,x);%群集索引mahaldist =玛哈尔(Gmfit,x0);% Distance from each grid point to each GMM component%在每个GMM组件上绘制椭圆体,并显示聚类结果。子图(2,2,j);h1 = g箭偶(x(:,1),x(:,2),clusterx);% Distance from each grid point to each GMM component抓住;nk = numel(唯一(clusterx));为了m = 1:nK idx = mahalDist(:,m)<=threshold; Color = h1(m).Color*0.75 + -0.5*(h1(m).Color - 1); h2 = plot(X0(idx,1),X0(idx,2),'.','颜色',Color,的标记Size',1); uistack(h2,'bottom');结尾plot(gmfit.mu(:,1),gmfit.mu(:,2),'kx','行宽'2,的标记Size',10) legend(h1,{'1','2','3'});抓住离开融合(j)= gmfit.converged;% Indicator for convergence结尾

图包含4个轴对象。轴对象1包含7个类型线的物体。这些对象代表1,2,3.轴对象2包含7个类型的7个对象。这些对象表示1,2,3.轴对象3包含7个类型线的物体。这些对象表示1,2,3.轴对象4包含7个类型的7个对象。这些对象代表1,2,3。

总和(融合)
ans = 4

所有算法都融合。数据点的每个起始群集分配都会导致不同的拟合集群分配。您可以为名称值对参数指定正整数Replicates, which runs the algorithm the specified number of times. Subsequently,fitgmdistchooses the fit that yields the largest likelihood.

When to Regularize

有时,在EM算法的迭代期间,拟合的协方差矩阵可能变成病变,这意味着可能性逃逸到无穷大。如果存在以下一个或多个条件,则会发生此问题:

  • You have more predictors than data points.

  • 您可以使用太多组件指定拟合。

  • Variables are highly correlated.

To overcome this problem, you can specify a small, positive number using the'正规化值'name-value pair argument.fitgmdistadds this number to the diagonal elements of all covariance matrices, which ensures that all matrices are positive definite. Regularizing can reduce the maximal likelihood value.

模型拟合统计数据

在大多数应用程序中,组件数量k并且适当的协方差结构σ是未知的。一种方法可以通过比较信息标准来调整GMM。两个流行的信息标准是Akaike信息标准(AIC)和贝叶斯信息标准(BIC)。

AIC和BIC都采用优化的负记录,然后用模型中的参数数(模型复杂性)惩罚它。但是,BIC惩罚了比AIC更严重的复杂性。因此,AIC倾向于选择更复杂的模型,可能会过度装备,并且BIC倾向于选择可能贴在底部的更简单的模型。良好的做法是在评估模型时看看两个标准。降低AIC或BIC值表示更好的拟合模型。另外,确保您的选择k并且协方差矩阵结构适合您的应用程序。fitgmdist存储适合的AIC和BICGMDistribution.model objects in the propertiesAIC.BIC.。您可以使用点表示法访问这些属性。有关如何选择如何选择相应参数的示例,请参阅调谐高斯混合模型

See Also

||

Related Topics