fitcsvm decision boundary equation

조회 수: 49 (최근 30일)
Shaybe
Shaybe 2019년 2월 17일
편집: Brendan Hamm 2019년 3월 11일
I have trained a linear SVM on 2D data and can't seem to get the line equation describing the decision boundary.
Here is some code that fails miserably.
rng(1)
n=5;
X=rand(n,2);
Y=logical([1 0 0 0 0]);
svmStruct=fitcsvm(X,Y);
gscatter(X(:,1),X(:,2),Y); hold on;
fplot(@(x)-(svmStruct.Beta(1)*x+svmStruct.Bias)/svmStruct.Beta(2))
Would appreciate help!
  댓글 수: 1
Brendan Hamm
Brendan Hamm 2019년 3월 11일
It seems to me the reason why this fails is you are trying to train a model with only 5 points. Stop and think, how do you make a support vector with only one data point for the True class? An infinite number of ways should be your answer.
One other issue you seem to have is in the orientation of your data. X is a 5-by-2 and Y is 1-by-5. So, you should transpose your response. No that this solves the above problem.
Y=logical([1 0 0 0 0])';
Otherwise what you have for the plotting seems fine.

댓글을 달려면 로그인하십시오.

답변 (1개)

Brendan Hamm
Brendan Hamm 2019년 3월 11일
편집: Brendan Hamm 2019년 3월 11일
Generate some data for the classes
rng('default')
n = 100;
X = [2 + 0.5*randn(n,2);...
3 + 0.6*randn(n,2)];
Y = [zeros(n,1);ones(n,1)];
gscatter(X(:,1),X(:,2),Y)
Fit a model
mdl = fitcsvm(X,Y);
Make a plot showing the decision boundary
Begin by making a grid of values
x = linspace(0,5);
y = linspace(0,5);
[XX,YY] = meshgrid(x,y);
Stack the values into an m-by-2 matrix
pred = [XX(:),YY(:)];
p = predict(mdl,pred);
Look at a scatter of the predictions
gscatter(pred(:,1),pred(:,2),p)
Define the boundary function, note that:
f = @(x) -(x*mdl.Beta(1) + mdl.Bias)/mdl.Beta(2);
y = f(x);
hold on
plot(x,y,'g--','LineWidth',2,'DisplayName','Boundary')
hold off
On the original data
gscatter(X(:,1),X(:,2),Y(:,1))
hold on
plot(x,y,'g--','LineWidth',2,'DisplayName','Boundary')
hold off
More generally for ECOC you might consider the code in this example:

카테고리

Help CenterFile Exchange에서 Support Vector Machine Classification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by