
SVM超平面の3次元プロット
8 views (last 30 days)
Show older comments
予測子が3つある2クラスSVMにおいて,各予測子をX,Y,Z軸とする分離境界面プロットはどのようにすればいいでしょうか?
範例がありましたら併せてご教授いただければ幸いです.
0 Comments
Accepted Answer
Kenta
on 15 Feb 2020

こんにちは、下のURLなどを参考にしました。https://jp.mathworks.com/matlabcentral/answers/444031-how-to-draw-hyperplane-using-fitcsvm
上の赤の面が3D空間内の分離平面で、〇で囲まれている点がその付近の点(=サポートベクトル)です。
訓練データ内のx,y,z座標から、可視化する超平面の座標の範囲を決定し、それらを含む点すべてに対し、推論をし、平面を割り出します。下ではdという変数で、超平面を描くための下調べの点のインターバルを決め、それに沿って格子点のような点を作成するイメージです。
meshgrid関数で手間を少なくそれらの点の座標を取り出すことができます。より詳細な超平面を描くためには、そのステップ幅を小さくすればよいですが、きめ細かさと計算時間・必要なメモリはトレードオフの関係にあります。
サポートベクトルの座標もfitcsvmの出力変数に格納されています。
clear;clc;close all
load fisheriris
inds = ~strcmp(species,'setosa');
X = meas(inds,1:3);
y = species(inds);
SVMModel = fitcsvm(X,y,'KernelFunction','polynomial');
sv = SVMModel.SupportVectors;
figure
scatter3(X(:,1),X(:,2),X(:,3),10,categorical(y),'filled')
hold on
plot3(sv(:,1),sv(:,2),sv(:,3),'ko','MarkerSize',10)
hold on
d = 0.02;
[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(SVMModel,xGrid);
[faces,verts,colors] = isosurface(x1Grid,x2Grid,x3Grid, reshape(scores(:,2),size(x1Grid)), 0,x1Grid);
p=patch('Vertices', verts, 'Faces', faces, 'FaceColor','k','edgecolor', 'none', 'FaceAlpha', 0.5);
p.FaceColor = 'red';
% legend('versicolor','virginica','hyper-plane')
grid on
box on
2 Comments
Kenta
on 17 Feb 2020
解決したようでよかったです。今回は多項式カーネルの結果を示していますが、ガウシアンカーネルを試したり、ほかのパラメータ次第でいろいろとおもしろい分離境界が可視化できるかもしれません。いろいろと試してもらえると幸いです。
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!