Plot 3D hyperplane from fitcsvm results.
Show older comments
Hello,
I am trying to figure out how to plot the resulting decision boundary from fitcsvm using 3 predictors. I was able to reproduce the sample code in 2-dimensions found here: https://www.mathworks.com/help/stats/support-vector-machines-for-binary-classification.html#bsr5b6n
I did also look at this answer: https://stackoverflow.com/questions/16146212/how-to-plot-a-hyper-plane-in-3d-for-the-svm-results/19969412#19969412
But it uses functions no longer supported, and I could not locate the equivalent parameters in the new structure.
However I am struggling to grasp how to extend this to three dimensions. Following the same construct but adding z-dimension did not seem to work with contour3.
Below is the sample code of what I am attempting to run. HOME, Hmax, and gap are the prdeictive metrics generated for each observation from separate code, they are all vectors of the same length.
X = [HOME, gap, Hmax];
mdl = fitcsvm([X,labels,'OptimizeHyperparameters','auto',...
'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',...
'expected-improvement-plus'));
d =0.05; % Step size of the grid
[x1Grid,x2Grid, x3Grid] = meshgrid(min(X(:,1)):d:max(X(:,1)),...
min(X(:,2)):d:max(X(:,2)), min(X(:,3)):d:max(X(:,3)));
xGrid = [x1Grid(:),x2Grid(:),x3Grid(:)];
[ ~ , scores] = predict(mdl,xGrid);
zGrid = reshape(scores(:,2), size(x1Grid));
colormap = zeros(size(dense,1),3);
colormap(labels==1,1) = 1;
colormap(labels~=1,3) = 1;
figure,
scatter3(HOME, gap, Hmax, 10, colormap)
xlabel 'HOME', ylabel 'Gap Percentage', zlabel 'Hmax'
hold on
plot3(X(mdl.IsSupportVector,1),...
X(mdl.IsSupportVector,2), X(mdl.IsSupportVector,3), 'ko','MarkerSize',10);
grid on
Accepted Answer
More Answers (0)
Categories
Find more on Parallel and Cloud in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!