MATLAB: Multivariate Gaussian Distribution Plotting of 4 Variable Data

gaussian distributioniris datasetmultivariate normal distribution

Hello I'm working on classification of Iris data set which has 4 variable: Setal length-width, Petal length-width. I have calculated the 4×4 covariance and mean values. How can I plot Gaussian distribution of this data set. I couldn't plot it via using Mvnpdf function in the link. I would be appreciated if you could help me. Thanks.

Best Answer

You are trying to visualize a 5D function. It is not easy to visualize such a function on a single graph. The easiest way is to make several graphs and show a higher dimensional slice of the function. See the following example. It uses the Setal length (SL) and Setal width (SW) on the x and y-axis. And uses the values of Petal length (PL) and Petal Width (PW) is used the higher dimensional variables to create several graphs.
rng(0);
mu = rand(1, 4);
sigma = rand(4);
sigma = sigma*sigma.'; % positive definite
sl = -3:0.1:3;
sw = -3:0.1:3;
pl = 0:1:2;
pw = 0:1:2;
[SL, SW, PL, PW] = ndgrid(sl, sw, pl, pw);
X = [SL(:) SW(:) PL(:) PW(:)];
Y = mvnpdf(X, mu, sigma);
Y = reshape(Y, size(SL));
figure;
tiledlayout('flow');
for i=1:numel(pl)
for j=1:numel(pw)
nexttile
surf(sl, sw, Y(:,:,i,j));
title(sprintf('PL=%.2f, PW=%.2f', pl(i), pw(j)));
shading interp
end
end