简体   繁体   中英

SVM MATLAB implementation Error plotting decision boundary

I am trying to do binary classification in MATLAB but the following code throws an error at the end.

   load('ex6data1.mat');

% Plot training data
plotData(X, y);



fprintf('Program paused. Press enter to continue.\n');
pause;

model=fitcsvm(X,y);
visualizeBoundaryLinear(X,y,model); //error shows up here i guess


fprintf('Paused');




Error window
   Error using subsref
No appropriate method, property, or field 'w' for class
'ClassificationSVM'.

Error in classreg.learning.internal.DisallowVectorOps/subsref (line
21)
                [varargout{1:nargout}] = builtin('subsref',this,s);

Error in visualizeBoundaryLinear (line 7)
w = model.w;

Error in Untitled2 (line 18)
visualizeBoundaryLinear(X,y,model);

Note:Y is 1 for positive example and -1 for negative.

If you look at the MATLAB documentation for fitcsvm you'll find that there is no property w , which is what's giving you your error.

You need to calculate the weights w yourself, since MATLAB is solving the dual form of the SVM. More details can be found here. Take a look at this reference if you want to learn more. You can use the following formula:

w = zeros( size(x(1,:)) );
for i=1:N
    w = w + alpha(i)*y(i)*x(i,:);
end

You can calculate the w vector using a combination of the alpha's, which are returned in your model, and your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM