fitcsvm decision boundary equation

55 次查看(过去 30 天)
Shaybe
Shaybe 2019-2-17
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:

标签

Community Treasure Hunt

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

Start Hunting!

Translated by