Reputation: 338
How do I visualise the output of discriminant analysis, such as linear discriminant analysis into 3D dimensions in MATLAB. The fitcdiscr function seems to be working with the 3 dimensional input, but I'm struggling to plot the "discrimination plane" with scatter3, which I'm using to plot the data.
I understand there are a few ways of plotting a plane, but what outputs of fitcdiscr do I need to pass to something like surf()?
I.e. if I have data like this:
clear
clc
close all
%----------------------------
x1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
x1 = x1 + r;
y1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
y1 = y1 + r;
z1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
z1 = z1 + r;
x1 = x1';
y1 = y1';
z1 = z1';
label1 = ones(length(y1),1);
Tclust1 = [label1,x1,y1,z1];
%----------------------------
x2 = 4:0.01:6;
r = -1 + (1+1)*rand(1,201);
x2 = x2 + r;
y2 = 10:0.01:12;
r = -1 + (1+1)*rand(1,201);
y2 = y2 + r;
z2 = 10:0.01:12;
r = -1 + (1+1)*rand(1,201);
z2 = z2 + r;
x2 = x2';
y2 = y2';
z2 = z2';
label2 = 2.*ones(length(y2),1);
Tclust2 = [label2,x2,y2,z2];
%----------------------------
x3 = 5:0.01:7;
r = -1 + (1+1)*rand(1,201);
x3 = x3 + r;
y3 = 11:0.01:13;
r = -1 + (1+1)*rand(1,201);
y3 = y3 + r;
z3 = 11:0.01:13;
r = -1 + (1+1)*rand(1,201);
z3 = z3 + r;
x3 = x3';
y3 = y3';
z3 = z3';
label3 = 3.*ones(length(y3),1);
Tclust3 = [label3,x3,y3,z3];
%----------------------------
T = [Tclust1;Tclust2;Tclust3];
data = T(:,2:4);
labels = T(:,1);
%do the discriminant analysis
MdlLinear = fitcdiscr(data,labels);
%plot data with boundaries -----------------------------------------------
figure()
scatter3(x1,y1,z1,'g')
hold on
scatter3(x2,y2,z2,'b')
scatter3(x3,y3,z3,'r')
% hold off
title('Full data set')
surf(?,?,?)
For reference, I want to do this but in 3 dimensions: Matlab fitcdiscr
Upvotes: 0
Views: 51
Reputation: 338
Having understood what I needed to plot, I found the following an extension of the example in the matlab example using "fimplicit3":
%group 1 and group 2
figure()
scatter3(x1,y1,z1,'g')
hold on
scatter3(x2,y2,z2,'b')
scatter3(x3,y3,z3,'r')
K12 = MdlLinear.Coeffs(1,2).Const;
L12 = MdlLinear.Coeffs(1,2).Linear;
f12 = @(x1,x2,x3) K12 + L12(1)*x1 + L12(2)*x2 + L12(3)*x3;
h2 = fimplicit3(f12,[xmin xmax ymin ymax zmin zmax]);
legend('Data 1','Data 2','Data 3','Boundary 1 -> 2','location','eastoutside')
Upvotes: 0
Reputation: 365
To visualise the plane of LInear Discriminant Analysis (LDA) in 3D, here's some follow through steps:
fitcdiscr
scatter3
surf
Heres how you can do it:
You have already done this part so nothing to worry about
MdlLinear = fitcdiscr(data, labels);
This next part is for extracting the coefficients, you can access them by using the coeffs
property of the model.
coeff = MdlLinear.Coeffs(1,2).Linear; % Coefficients between class 1 and 2
The plane above can be defined using the equation
a⋅x + b⋅y + c⋅z + d = 0
Where a
. b
, c
are the coefficients from the coeff
, and d
is the bias term shown in the const
property.
You can dow define a 3-dimensional grid and evaluate the plane equation as shown below:
[xGrid, yGrid] = meshgrid(min(data(:,1)):0.1:max(data(:,1)), min(data(:,2)):0.1:max(data(:,2)));
zGrid = -(coeff(1) * xGrid + coeff(2) * yGrid + MdlLinear.Coeffs(1,2).Const) / coeff(3);
After that all you need to do is just plot the data and the plane ill paste my MATLAB script to show.
figure;
hold on;
% Plot the original data points
scatter3(x1, y1, z1, 'g');
scatter3(x2, y2, z2, 'b');
scatter3(x3, y3, z3, 'r');
% Plot the discrimination plane
surf(xGrid, yGrid, zGrid, 'FaceAlpha', 0.5, 'EdgeColor', 'none');
title('LDA Discrimination Plane and Data');
xlabel('X');
ylabel('Y');
zlabel('Z');
legend({'Class 1', 'Class 2', 'Class 3', 'Discrimination Plane'});
hold off;
Notes
It should be noted that
surf
function does plot the plane in 3D spaceFaceAlpha
makes the plane semi-transparentcoeff(1, 2)
correspond to separation between class 1 and class 2. You can modify it to whatever you desire.Let me know if I have answered your question adequetly.
Upvotes: 0