Matlab 用训练的高斯混合模型标记新数据
我不知道如何使用训练过的高斯混合模型(GMM)对一些新数据进行预测。例如,我从3个不同的类(集群)中提取了一些带标签的数据。对于每一类数据点,我拟合一个GMM(gm1、gm2和gm3)。假设我们知道每个类别的高斯混合数(例如,k1=2、k2=1和k3=3),或者可以使用Akaike信息准则(AIC)对其进行估计(优化)。那么,当我有了一些新的数据集时,我怎么知道它更可能属于类1、2还是3呢 一些Matlab脚本说明了我的意思:Matlab 用训练的高斯混合模型标记新数据,matlab,machine-learning,gaussian,supervised-learning,mixture-model,Matlab,Machine Learning,Gaussian,Supervised Learning,Mixture Model,我不知道如何使用训练过的高斯混合模型(GMM)对一些新数据进行预测。例如,我从3个不同的类(集群)中提取了一些带标签的数据。对于每一类数据点,我拟合一个GMM(gm1、gm2和gm3)。假设我们知道每个类别的高斯混合数(例如,k1=2、k2=1和k3=3),或者可以使用Akaike信息准则(AIC)对其进行估计(优化)。那么,当我有了一些新的数据集时,我怎么知道它更可能属于类1、2还是3呢 一些Matlab脚本说明了我的意思: clc; clf; clear all; close all; %
clc; clf; clear all; close all;
%% Create some artificial training data
% 1. Cluster 1 with two mixture of Gaussian (k1 = 2)
rng default; % For reproducibility
mu1 = [1 2];
sigma1 = [3 .2; .2 2];
mu2 = [-1 -2];
sigma2 = [2 0; 0 1];
X1 = [mvnrnd(mu1,sigma1,200); mvnrnd(mu2,sigma2,100)];
options1 = statset('Display', 'final');
k1 = 2;
gm1 = fitgmdist(X1, k1, 'Options', options1);
% 2. Cluster 2 with one mixture of Gaussian (k2 = 1)
mu3 = [6 4];
sigma3 = [3 .1; .1 4];
X2 = mvnrnd(mu3,sigma3,300);
options2 = statset('Display', 'final');
k2 = 1;
gm2 = fitgmdist(X2, k2, 'Options', options2);
% 3. Cluster 3 with three mixture of Gaussian (k3 = 3)
mu4 = [-5 -6];
sigma4 = [1 .1; .1 1];
mu5 = [-5 -10];
sigma5 = [6 .1; .1 1];
mu6 = [-2 -15];
sigma6 = [8 .1; .1 4];
X3 = [mvnrnd(mu4,sigma4,200); mvnrnd(mu5,sigma5,300); mvnrnd(mu6,sigma6,100)];
options3 = statset('Display', 'final');
k3 = 3;
gm3 = fitgmdist(X3, k3, 'Options', options3);
% Display
figure,
scatter(X1(:,1),X1(:,2),10,'ko'); hold on;
ezcontour(@(x,y)pdf(gm1, [x y]), [-12 12], [-12 12]);
scatter(X2(:,1),X2(:,2),10,'ko');
ezcontour(@(x,y)pdf(gm2, [x y]), [-12 12], [-12 12]);
scatter(X3(:,1),X3(:,2),10,'ko');
ezcontour(@(x,y)pdf(gm3, [x y]), [-12 12], [-12 12]); hold off;
我们可以得到这个数字:
然后我们得到了一些新的测试数据,例如:
%% Create some artificial testing data
mut1 = [6.1 3.8];
sigmat1 = [3.1 .1; .1 4.2];
mut2 = [5.8 4.5];
sigmat2 = [2.8 .1; .1 3.8];
Xt1 = [mvnrnd(mut1,sigmat1,500); mvnrnd(mut2,sigmat2,100)];
figure,
scatter(Xt1(:,1),Xt1(:,2),10,'ko');
xlim([-12 12]); ylim([-12 12]);
% number of instances in each data subset
n1 = size(X1,1);
n2 = size(X2,1);
n3 = size(X3,1);
% the entire dataset
X = [X1; X2; X3];
n = n1 + n2 + n3;
k = k1 + k2 + k3;
% initial parameters of the new GMM (combination of the previous three)
% (note PComponents is normalized according to proportion of data in each subset)
S = struct('mu',[gm1.mu; gm2.mu; gm3.mu], ...
'Sigma',cat(3, gm1.Sigma, gm2.Sigma, gm3.Sigma), ...
'PComponents',[gm1.PComponents*n1, gm2.PComponents*n2, gm3.PComponents*n3]./n);
% train the final model over all instances
opts = statset('MaxIter',1000, 'Display','final');
gmm = fitgmdist(X, k, 'Options',opts, 'Start',S);
% display GMM density function over training data
line(X(:,1), X(:,2), 'LineStyle','none', ...
'Marker','o', 'MarkerSize',1, 'Color','k')
hold on
ezcontour(@(x,y) pdf(gmm,[x y]), xlim(), ylim())
hold off
title(sprintf('GMM over %d training instances',n))
我故意让测试数据与Cluster 2数据相似。在我们使用GMM进行训练之后,我们能否以某种方式预测新测试数据的标签?对于每一类的预测,有可能得到一些概率,比如(p1=18%,p2=80%和p3=2%)。当我们得到p2=80%时,我们可以得到一个硬分类,新的测试数据被标记为集群2
p、 我找到了这篇文章,但对我来说似乎是理论性的。如果可以,请在回复中加入一些简单的Matlab脚本
非常感谢。A
编辑: 当Amro回答问题的解决方案时,我有更多的问题
% initial parameters of the new GMM (combination of the previous three)
% (note PComponents is normalized according to proportion of data in each subset)
S = struct('mu',[gm1.mu; gm2.mu; gm3.mu], ...
'Sigma',cat(3, gm1.Sigma, gm2.Sigma, gm3.Sigma), ...
'PComponents',[gm1.PComponents*n1, gm2.PComponents*n2, gm3.PComponents*n3]./n);
% train the final model over all instances
opts = statset('MaxIter',1000, 'Display','final');
gmm = fitgmdist(X, k, 'Options',opts, 'Start',S);
阿姆罗得到的是如下所示
这可能不适合我的数据,因为它将标记的cluster1和cluster2与cluster1的一部分分离。这就是我试图避免的
这里我给出的是一个人工数值例子;然而,在我的实际应用程序中,它处理图像分割问题(例如,cluster1是我的背景图像,cluster2是我想要分离的对象)。然后,我尝试以某种方式“强制”单独的GMM以适应不同的类。如果两个集群相距很远(例如,本例中的集群1和集群3),则使用Amro的方法组合所有数据,然后进行GMM拟合是没有问题的。然而,当我们对图像数据进行训练时,由于分辨率的限制(由于部分体积效应),背景和目标的分离永远不会完美;因此,我们很可能会遇到cluster1与cluster2重叠的情况,如图所示。我认为可能混合所有数据,然后进行拟合,这会给新数据的进一步预测带来一些问题,对吗
然而,经过一点思考,我现在想做的是:
% Combine the mixture of Gaussian and form a new gmdistribution
muAll = [gm1.mu; gm2.mu; gm3.mu];
sigmaAll = cat(3, gm1.Sigma, gm2.Sigma, gm3.Sigma);
gmAll = gmdistribution(muAll, sigmaAll);
pt1 = posterior(gmAll, Xt1);
你们觉得怎么样?还是等同于阿姆罗的方法?如果是,有没有办法强制我的GMM分离posterior
函数的原理也有疑问。从本质上讲,我想在GMM拟合的情况下估计测试数据的可能性。那为什么我们现在要计算后验概率呢?或者这只是一个命名问题(换句话说,“后验概率”=“可能性”)A.实际上,您已经培训了三个GMM模型,而不是一个,每个模型本身都是一种混合物。通常,您会创建一个包含多个组件的GMM,其中每个组件代表一个集群 因此,在您的案例中,我要做的是创建一个新的GMM模型,该模型在整个数据集(
X1
、X2
、和X3
)上进行训练,其分量数量等于三个GMM中所有分量的总和(即2+1+3=6
高斯混合)。该模型将使用单独训练的模型的参数进行初始化
下面是要说明的代码(我使用的变量与您在示例中创建的变量相同):
现在,我们已经在整个训练数据集上训练了一个GMM模型(使用
k=6
mixes),我们可以使用它来聚类新的数据实例:
cIdx = cluster(gmm, Xt1);
>> pr(cIdx==1,:)
ans =
0.9813 0.0001 0.0186 0.0000 0.0000 0.0000
0.6926 0.0000 0.3074 0.0000 0.0000 0.0000
0.5069 0.0000 0.4931 0.0000 0.0000 0.0000
0.6904 0.0018 0.3078 0.0000 0.0000 0.0000
0.6954 0.0000 0.3046 0.0000 0.0000 0.0000
<... output truncated ...>
0.5077 0.0000 0.4923 0.0000 0.0000 0.0000
0.6859 0.0001 0.3141 0.0000 0.0000 0.0000
0.8481 0.0000 0.1519 0.0000 0.0000 0.0000
这与手工计算组分的后验概率,以概率最大的组分为聚类指数相同:
pr = posterior(gmm, Xt1);
[~,cIdx] = max(pr,[],2);
正如预期的那样,几乎95%的测试数据被聚集为属于同一个组件:
>> tabulate(cIdx)
Value Count Percent
1 27 4.50%
2 0 0.00%
3 573 95.50%
以下是匹配的Guassian参数:
>> idx = 3;
>> gmm.mu(idx,:)
ans =
5.7779 4.1731
>> gmm.Sigma(:,:,idx)
ans =
2.9504 0.0801
0.0801 4.0907
这确实对应于上图右上侧的组件
类似地,如果您检查另一个组件idx=1
,它将是前一个组件左侧的组件,这解释了600个测试实例中的27个是如何“错误分类”的,如果您将。。。以下是GMM对这些情况的信心:
cIdx = cluster(gmm, Xt1);
>> pr(cIdx==1,:)
ans =
0.9813 0.0001 0.0186 0.0000 0.0000 0.0000
0.6926 0.0000 0.3074 0.0000 0.0000 0.0000
0.5069 0.0000 0.4931 0.0000 0.0000 0.0000
0.6904 0.0018 0.3078 0.0000 0.0000 0.0000
0.6954 0.0000 0.3046 0.0000 0.0000 0.0000
<... output truncated ...>
0.5077 0.0000 0.4923 0.0000 0.0000 0.0000
0.6859 0.0001 0.3141 0.0000 0.0000 0.0000
0.8481 0.0000 0.1519 0.0000 0.0000 0.0000
编辑: 我上面的示例旨在展示如何使用GMMs对数据进行聚类(无监督学习)。根据我现在的理解,您想要的是使用现有的训练模型(supervied learning)对数据进行分类。我想我被你使用的“集群”术语弄糊涂了:) 无论如何,现在应该很容易了;只需使用每个模型计算测试数据的类条件概率密度函数,并选择可能性最高的模型作为类标签(无需将模型组合为一个) 继续你的初始代码,那就是:
p = [pdf(gm1,Xt), pdf(gm2,Xt), pdf(gm3,Xt)]; % P(x|model_i)
[,cIdx] = max(p,[],2); % argmax_i P(x|model_i)
cIdx
是测试数据中每个实例的类预测(1、2或3)。您可能会发现我之前的答案也很有用:谢谢您的链接!另外,谢谢你的回复。请看我在原始帖子中的编辑,因为有些问题可以讨论。@SamoJerom:好的,现在我看到我们在考虑不同的任务(分类与聚类)。请参阅我的编辑以获取响应。。。如果你仔细阅读B,也可能会有所帮助