How to plot a hyper plane in 3D for the SVM results?
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aaronne
el 22 de Abr. de 2013
Comentada: Joseph Olson
el 6 de En. de 2022
I just wondering how to plot a hyper plane of the SVM results.
For example, here we are using two features, we can plot the decision boundary in 2D. But if how can we plot a hyper plane in 3D if we use 3 features?
load fisheriris;
features = meas(1:100,:);
featureSelcted = features(1:100,1:2); % For example, featureSelcted = features(1:100,1:3) can not be plotted
groundTruthGroup = species(1:100);
svmStruct = svmtrain(featureSelcted, groundTruthGroup, ...
'Kernel_Function', 'rbf', 'boxconstraint', Inf, 'showplot', true, 'Method', 'QP');
svmClassified = svmclassify(svmStruct,featureSelcted,'showplot',true);
A similar solution in R can be found at
but a Matlab implementation would be handy. Thanks very much.
A.
0 comentarios
Respuesta aceptada
Más respuestas (1)
manan lalit
el 3 de Abr. de 2019
Just putting my answer here in case someone is curious about how to find the analytical equation of the 3-D linear plane separating data belonging to two classes with the fitcsvm function in MATLAB.
You can find the coefficients (
and
) using the two equations below. Quoting from "Support-Vector Networks" by Cortes and Vapnik, 1995, "... the vector
that determines the optimal hyperplane can be written as a linear combination of training vectors"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211872/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211873/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211874/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211875/image.png)
Here, l is the number of support vectors. In MATLAB version 2018b
can be extracted using code such as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211876/image.png)
SVMModel = fitcsvm(data, groups);
% "data" contains the (N * D) matrix.
% "groups" is a (N*1) matrix indicating the two groups (+1 and -1)
alpha = SVMModel.Alpha;
Similarly,
can be determined using the equation below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211877/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211878/image.png)
Once,
and
are available, then plotting such a plane can be done, in the following manner:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211879/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/211880/image.png)
xgrid=[0:200];
ygrid=[0:200];
[X, Y]=meshgrid(xgrid, ygrid);
% w0 and b0 were determined through the two equations mentioned above.
Z=(-b0-w0(1)*X-w0(2)*Y)/w0(3);
surf(X, Y, Z)
2 comentarios
Joy
el 9 de Abr. de 2019
I like this analytical solution . Could you please share your example code? I don't exactly get the deriving part.
Thanks!
Joseph Olson
el 6 de En. de 2022
w0 and b0 can be extracted with
w0 = SVMModel.Beta;
b0 = SVMModel.Bias;
Ver también
Categorías
Más información sobre Classification Trees en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!