Is it possible to get the equation of the separating line generated when using SVMTRAIN to train a classifier using a linear kernel function in Bioinformatics Toolbox 3.5 (R2010a) for a 2D set of data?

14 views (last 30 days)
I am using the SVMTRAIN function to train a classifier for a 2D set of data using a linear kernel function (the default). When the 'showplot' option is set to 'true', a plot is displayed showing the grouped data and separating line for the classifier. I would like a way to obtain the equation for this line in the form y=mx+b.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Nov 2011
The ability to obtain the equation of the classifying line drawn by SVMTRAIN is not available in Bioinformatics Toolbox.
To work around this issue for a 2D set of data, you can obtain the data points of the line object after it has been plotted, and calculate the y-intercept and slope of the line using these points.
% Generate training data (x1,y1 are positive classification, x2,y2 are
% negative classification)
x1 = randn(100,1);
y1 = randn(100,1);
x2 = randn(100,1) + 3;
y2 = randn(100,1) + 3;
trueDecision = [zeros(size(x1)) ; ones(size(x2))];
Training = [x1 y1 ; x2 y2];
Group = trueDecision;
% Train the classifier
svmOutput = svmtrain(Training, Group, 'ShowPlot', true);
% Get the handle to the classifying line
axesHandle = svmOutput.FigureHandles{1}; % Get the handle to the axes
childHandles=get(axesHandle,'children'); % Find the axes' children
hggroupHandle=childHandles(1); % Get HGGROUP object handle that contains the line
lineHandle=get(hggroupHandle, 'children'); % Get the line's handle
% Get the X and Y data points of the line
xvals=get(lineHandle, 'XData');
yvals=get(lineHandle, 'YData');
% Using 2 points on the line, calculate slope and y-intercept
m=(yvals(2)-yvals(1))/(xvals(2)-xvals(1))
b=yvals(1)-m*xvals(1)
% Plot the y=mx+b line to check it (blue dashed line)
hold on;
plot(xvals, m*xvals+b, '--', 'LineWidth', 3);

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2010a

Community Treasure Hunt

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

Start Hunting!