使用matlab创建高斯混合模型及绘图

Matlab提供了根据几个独立的高斯模型创建Gaussian Mixture Model(GMM)的函数,即fitgmdist。关于该模型的具体使用方法以及绘制生成的GMM的图形的方法,如下代码所示:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot 3D Fitted Gaussian Mixture 
close;
clear;
clc;
mu1 = [1 2];
Sigma1 = [2 0; 0 0.5];
mu2 = [-3 -5];
Sigma2 = [1 0;0 1];
rng(1); % For reproducibility
X = [mvnrnd(mu1,Sigma1,1000);mvnrnd(mu2,Sigma2,1000)]; % 2000 x 2
GMModel = fitgmdist(X,2); %fit GMM distribution

figure;
y = [zeros(1000,1);ones(1000,1)];
hold on;
ezsurf (@(x1,x2)pdf(GMModel,[x1 x2]),get(gca,{'XLim','YLim'}));
title('{\bf 3-D Fitted Gaussian Mixture}');
hold off;

figure;
y = [zeros(1000,1);ones(1000,1)];
h = gscatter(X(:,1),X(:,2),y);
hold on;
ezcontour (@(x1,x2)pdf(GMModel,[x1 x2]),get(gca,{'XLim','YLim'}));
title('{\bf Fitted Gaussian Mixture Contours}');
legend(h,'Model 0','Model1')
hold off;

生成的图形,如下图所示:

使用matlab创建高斯混合模型及绘图_第1张图片



需要注意的是,在生成3D图形时,默认显示为俯视图,需要旋转图形,方可看出3D效果。


你可能感兴趣的:(matlab,GMM,matlab高斯混合模型,fitgmdist,ezsurf,ezcontour,matlab高斯绘图)