0

So I want to determine the alpha, beta and gamma values of my SVM classifier, I trained my SVM classifier and determined the alpha and support vectors. My support vectors have 3 variables per row, below are just ten rows from my support vector:

0.0904235536887480  -0.269325475875919  -0.678528701392414
-0.321039098061280  -0.507618180664821  -1.42365662798284
-0.0737761304021185 -0.269641311369441  -0.647521877863172
0.00105779420640393 -0.311226557946309  -0.667506146498475
0.0913098589312967  -0.289462325547514  -0.391261050348894
0.00622693949845773 -0.166248587146820  -0.149546793127464
-0.292302915842567  -0.564676268888150  -1.60153093563523
0.112997393643248   -0.310512134534035  -0.725281274142312
-0.135361511770186  -0.456321702624641  -1.26973221898260
-0.173160731078767  -0.434439033384469  -1.22687774941370

and similarly below are just ten rows from my alpha:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

So what I basically doing is to determine the coefficients like so:

c = (-1)*[0.0904*x1 + (-0.2693)*x2 + (-0.6785)*x3] + (-1)*[(-0.321)*x1 + (-0.5076)*x2 + (-1.4236)*x3].....

and so on until the size of my alpha.

So I coded the following in Matlab

syms x1, syms x2, syms x3;

alpha = SVMStruct.Alpha;
svm_vec = SVMStruct.SupportVectors;

for i = 1:size(alpha,1)
    c(i) = alpha(i)*(svm_vec(i,1)*x1 + svm_vec(i,2)*x2 + svm_vec(i,3)*x3);
end

sum_it = sum(c);

But this created a very strange output:

(107845064549358722206080751595348329973204613074833920445585562521882937008164658045489239834546021458299139*x1)/50534761550197893278639420198779799540396107395587434771118149413836407509953624874438129483687080755200 + (95720990302914087945142311872326568914380675701489099929103269189530321664249312169660240242394455632803627*x2)/134178504805697854567421908803656709124500009291732154392279224305703564767807900528680550698065697177600 + (90626366614084720573448362168042659133754200934323766866906741825007634289583081638045482944881264585156183*x3)/125521827076297992982426946945356276277758073208394596044390242092432367040852552107475353878835652198400

is this the expected output? Why am I getting it as fractions?

Looking by the output, did I implement my equation correctly?

Suever
  • 64,497
  • 14
  • 82
  • 101
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73

1 Answers1

1

Your inputs to your calculation are symbolic variables, so any calculations using those variables are going to yield an exact value that is also a symbolic variable. If you want the approximate floating point representation of the result, you'll want to cast the sum as a double

double(sum_it)

In order to do this though, you'll need to specify actual values for x1, x2, and x3

double(subs(sum(c), [x1, x2, x3], [1, 2, 3]))

That being said, I think what you actually want is to solve the system of equations. You can use the \ operator to do this

variables = svm_vec \ alpha;
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thanks for the reply, I cannot use double as I do not know the value of x1,x2 and x3 and I am only interested in the coefficients of those variables. I do not know what ``\`` achieved, I read up on it and didn't exactly understand its purpose here. Having said that, is my implementation correct? If so, I can just manually divide these number with ``x1`` to get that coefficient, right? – StuckInPhDNoMore Feb 14 '17 at 16:26
  • 2
    @StuckInPhD: use [`vpa`](https://www.mathworks.com/help/symbolic/vpa.html). Not sure why you need symbolic or variable precision math for this application though. – horchler Feb 14 '17 at 16:51
  • @StuckInPhD Aren't you trying to solve for `x1`, `x2`, and `x3`? – Suever Feb 14 '17 at 17:02
  • @horchler vpa was what I was looking for Thanks. I am only interested in finding the coefficients of these variables. – StuckInPhDNoMore Feb 15 '17 at 12:27
  • @Suever No, I am only interested in finding out the coefficients of these variables, basically the vaues of alpha, beta and gamma, which are the scaling factors of these support vectors. – StuckInPhDNoMore Feb 15 '17 at 12:27