Plot 3D hyperplane from fitcsvm results.

10 ビュー (過去 30 日間)
Michael McClelland
Michael McClelland 2018 年 6 月 27 日
回答済み: Michael McClelland 2018 年 6 月 27 日
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

採用された回答

Michael McClelland
Michael McClelland 2018 年 6 月 27 日
Disregard, I now understand the code and was able to update it to function for what I needed. Below is the updated function for any others looking for the same basic framework.
function [] = svm_3d_plot(mdl,X,group)
%Gather support vectors from ClassificationSVM struct
sv = mdl.SupportVectors;
%set step size for finer sampling
d =0.05;
%generate grid for predictions at finer sample rate
[x, y, z] = meshgrid(min(X(:,1)):d:max(X(:,1)),...
min(X(:,2)):d:max(X(:,2)), min(X(:,3)):d:max(X(:,3)));
xGrid = [x(:),y(:),z(:)];
%get scores, f
[ ~ , f] = predict(mdl,xGrid);
%reshape to same grid size as the input
f = reshape(f(:,2), size(x));
% Assume class labels are 1 and 0 and convert to logical
t = logical(group);
%plot data points, color by class label
figure
plot3(X(t, 1), X(t, 2), X(t, 3), 'b.');
hold on
plot3(X(~t, 1), X(~t, 2), X(~t, 3), 'r.');
hold on
% load unscaled support vectors for plotting
plot3(sv(:, 1), sv(:, 2), sv(:, 3), 'go');
%plot decision surface
[faces,verts,~] = isosurface(x, y, z, f, 0, x);
patch('Vertices', verts, 'Faces', faces, 'FaceColor','k','edgecolor',
'none', 'FaceAlpha', 0.2);
grid on
box on
view(3)
hold off
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel and Cloud についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by