SVM超平面の3次元プロット

16 views (last 30 days)
anchovy potato
anchovy potato on 28 Jan 2020
Commented: Kenta on 17 Feb 2020
予測子が3つある2クラスSVMにおいて,各予測子をX,Y,Z軸とする分離境界面プロットはどのようにすればいいでしょうか?
範例がありましたら併せてご教授いただければ幸いです.

Accepted Answer

Kenta
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
anchovy potato
anchovy potato on 17 Feb 2020
この方法で図をプロットすることができました。
参考URLや細かい説明、本当にありがとうございます!
Kenta
Kenta on 17 Feb 2020
解決したようでよかったです。今回は多項式カーネルの結果を示していますが、ガウシアンカーネルを試したり、ほかのパラメータ次第でいろいろとおもしろい分離境界が可視化できるかもしれません。いろいろと試してもらえると幸いです。

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 入門 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!