fitcsvm decision boundary equation
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Brendan Hamm
le 11 Mar 2019
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.
Réponses (1)
Brendan Hamm
le 11 Mar 2019
Modifié(e) : Brendan Hamm
le 11 Mar 2019
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:
0 commentaires
Voir également
Catégories
En savoir plus sur Discriminant Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!