fitcsvm decision boundary equation
조회 수: 22 (최근 30일)
이전 댓글 표시
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
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
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207855/image.jpeg)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207856/image.jpeg)
Define the boundary function, note that:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207857/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207858/image.jpeg)
On the original data
gscatter(X(:,1),X(:,2),Y(:,1))
hold on
plot(x,y,'g--','LineWidth',2,'DisplayName','Boundary')
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207859/image.jpeg)
More generally for ECOC you might consider the code in this example:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Discriminant Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!