0
$\begingroup$

I am trying to code SVM from scratch using a small toy problem that involves five support vector values. In the code below, there are 5 support vectors arbitrary chosen and denoted by the variables s1,s2,s3,s4,s5. The support vectors are augmented with a third coordinate which is the bias = 1. y denotes the labels for the 3 support vectors. A is the 5 by3 design matrix that contains the values after evaluation:

A=  20    10     5
10    10     5
20    20     5
20    30     5
30    30     5

Thus the equation becomes y = wx + b where x is the input data. The equation for the hyperplane is w = sum_i a_i*s_i where the a_i's are the alpha parameter for i=1,2,3,4,5.

Confusion: While evaluating for alpha = y/A I get the error

Error using  / 
Matrix dimensions must agree.

However, if I take the transpose of A in the least squares solution: alpha = y/A' then there is no error.

I am not sure what is the correct way to get alpha and then how many data points should the decision boundary w have?

    % 5 support vector
s1 = [2 1 1];
s2 = [1 1 1];
s3 = [2 2 1];
s4 = [2 3 1];
s5 = [3 3 1];

s_x = [2 1 2 2 3 ];
s_y = [1 1 2 3 4];

y = [-1 -1 -1 +1 +1]
%plot(s11(:,1),s11(:,2),'o')
gscatter(s_x,s_y,y)


A = [ (s1.*s1)+ (s2.*s1)+ (s3.*s1) + (s4.*s1) + (s5.*s1);
      (s1.*s2)+ (s2.*s2)+ (s3.*s2) + (s4.*s2) + (s5.*s2);
      (s1.*s3)+ (s2.*s3)+ (s3.*s3) + (s4.*s3) + (s5.*s3);
      (s1.*s4)+ (s2.*s4)+ (s3.*s4) + (s4.*s4) + (s5.*s4);
      (s1.*s5)+ (s2.*s5)+ (s3.*s5) + (s4.*s5) + (s5.*s5);]
  
  alpha = y/A
     
    %Next Steps if everything works ok, no error  
  % w= sum a_i*s_i
  % y = wx +b
  w = [alpha1*s1+alpha2*s2+alpha3*s3 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.