0
votes

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.

1
What error does matlab throw? Can you copy it under your code please?BillBokeey
Hi i have added it..Sridhar Thiagarajan
What does visualizeBoundaryLinear look like? The error happens in that function, but visualizeBoundaryLinear is not part of the native MATLAB distribution.rayryeng
What i want to do is plot the linear decision boundary obtained by the SVM..why is the visualizeboundarylinear function throwing an error..isnt it supposed to help me do what i want?Sridhar Thiagarajan
Yeah it's supposed to do that, but the code is happening in that function... how can we help you if we don't see what the code is doing?rayryeng

1 Answers

0
votes

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.