KSquare Utilities
SvmWrapper.cpp File Reference

Provides an interface to the svm.cpp functions. More...

#include "FirstIncludes.h"
#include <algorithm>
#include <ctype.h>
#include "float.h"
#include <fstream>
#include <iostream>
#include <math.h>
#include <map>
#include <numeric>
#include <ostream>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#include "MemoryDebug.h"
#include "KKBaseTypes.h"
#include "OSservices.h"
#include "SvmWrapper.h"
#include "KKMLLTypes.h"
#include "svm.h"

Go to the source code of this file.

Macros

#define Malloc   (type,n) (type *)malloc((n)*sizeof(type))
 

Functions

void ComputeProb (kkint32 numClasses, const VectorFloat &probClassPairs, vector< double > &dist, double **crossClassProbTable, kkint32 *votes, double *probabilities, kkint32 knownClassNum)
 
void ComputeProbForVoting (kkint32 numClasses, float A, vector< double > &dist, double **crossClassProbTable, kkint32 *votes, double *probabilities, kkint32 knownClassNum, double confidence, double &compact)
 
template<class T >
kkint32 GetMaxIndex (vector< T > &vote, kkint32 voteLength, kkint32 &maxIndex2)
 
template<class T >
kkint32 GetMaxIndex (T *vote, kkint32 voteLength, kkint32 &maxIndex2)
 
bool GreaterThan (kkint32 leftVotes, double leftProb, kkint32 rightVotes, double rightProb)
 
void GreaterVotes (bool useProbability, kkint32 numClasses, kkint32 *votes, double *probabilities, kkint32 &pred1Idx, kkint32 &pred2Idx)
 
void NormalizeProbabilitiesWithAMinumum (kkint32 numClasses, double *probabilities, double minProbability)
 Will normailize probabilites such that the sum of all equal 1.0 and no one probability will be less than 'minProbability'. More...
 
void saveData (svm_problem ds, kkint32 begin, kkint32 end, std::string name)
 
void saveData (svm_problem ds, kkint32 begin, kkint32 end, string name)
 

Detailed Description

Provides an interface to the svm.cpp functions.

Definition in file SvmWrapper.cpp.

Macro Definition Documentation

#define Malloc   (type,n) (type *)malloc((n)*sizeof(type))

Definition at line 53 of file SvmWrapper.cpp.

Function Documentation

void ComputeProb ( kkint32  numClasses,
const VectorFloat probClassPairs,
vector< double > &  dist,
double **  crossClassProbTable,
kkint32 votes,
double *  probabilities,
kkint32  knownClassNum 
)

Definition at line 472 of file SvmWrapper.cpp.

480 {
481  kkint32 i;
482  for (i = 0; i < numClasses; i++)
483  votes[i] = 0;
484 
485  kkint32 distIdx = 0;
486  for (i = 0; i < (numClasses - 1); i++)
487  {
488  for (kkint32 j = i + 1; j < numClasses; j++)
489  {
490  if (dist[distIdx] > 0)
491  votes[i]++;
492  else
493  votes[j]++;
494 
495  double tempProb = (double)(1.0 / (1.0 + exp (-1.0 * probClassPairs[distIdx] * dist[distIdx])));
496  crossClassProbTable[i][j] = tempProb;
497  crossClassProbTable[j][i] = (1.0 - tempProb);
498  distIdx++;
499  }
500  }
501 
502  double totalProb = 0.0;
503  for (i = 0; i < numClasses; i++)
504  {
505  double probThisClass = 1.0;
506  for (kkint32 j = 0; j < numClasses; j++)
507  {
508  if (i != j)
509  probThisClass *= crossClassProbTable [i][j];
510  }
511 
512  probabilities[i] = probThisClass;
513  totalProb += probThisClass;
514  }
515 
516  if (totalProb == 0.0)
517  {
518  // I think this happens because we are using float pfor probTable and double for dist[]
519  // For now we will give each class an equal probability.
520  for (i = 0; i < numClasses; i++)
521  probabilities[i] = (1.0 / double(numClasses));
522  }
523  else
524  {
525  for (i = 0; i < numClasses; i++)
526  probabilities[i] = probabilities[i] / totalProb;
527  }
528 
529  if ((knownClassNum >= 0) && (knownClassNum < numClasses))
530  {
531  kkint32 maxIndex1 = -1;
532  kkint32 maxIndex2 = -1;
533  maxIndex1 = GetMaxIndex (probabilities, numClasses, maxIndex2);
534  }
535  //NormalizeProbabilitiesWithAMinumum (numClasses, probabilities, 0.002);
536 } /* ComputeProb */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 GetMaxIndex(vector< T > &vote, kkint32 voteLength, kkint32 &maxIndex2)
Definition: SvmWrapper.cpp:67
void ComputeProbForVoting ( kkint32  numClasses,
float  A,
vector< double > &  dist,
double **  crossClassProbTable,
kkint32 votes,
double *  probabilities,
kkint32  knownClassNum,
double  confidence,
double &  compact 
)
Parameters
numClassesNumber of Classes.
Aprobability parameter
distDistances for each binary classifier from decision boundary.
crossClassProbTableTwo dimensional array that is 'numClass' by 'numClass'; will receive the probabilities between classes.
votesArray 'numClasses' in length that will receive the number of Votes each class won.
probabilitiesArray 'numClasses' in length that will receive the computed Probability for Each Class
knownClassNumThe Class that we know the example to be.
confidenceUsed for calculating 'compact' probability must exceed this.
compact'knownClassNum' and 'confidence' need to be provided.

Definition at line 287 of file SvmWrapper.cpp.

297 {
298  compact = 0.0;
299 
300  kkint32 i;
301  for (i = 0; i < numClasses; i++)
302  votes[i] = 0;
303 
304  kkint32 distIdx = 0;
305  for (i = 0; i < (numClasses - 1); i++)
306  {
307  for (kkint32 j = i + 1; j < numClasses; j++)
308  {
309  if (dist[distIdx] > 0)
310  votes[i]++;
311  else
312  votes[j]++;
313 
314  crossClassProbTable[i][j] = 0.0;
315  crossClassProbTable[j][i] = 0.0;
316 
317  distIdx++;
318  }
319  }
320 
321  int win1 = -1;
322  int win2 = -1;
323  int win3 = -1;
324  int win4 = -1;
325 
326  int win1Idx = 0;
327  int win2Idx = 0;
328  int win3Idx = 0;
329  int win4Idx = 0;
330 
331  for (i = 0; i < numClasses; ++i)
332  {
333  int zed = votes[i];
334  if (zed >= win4)
335  {
336  win4 = zed;
337  win4Idx = i;
338 
339  if (win4 >= win3)
340  {
341  win4 = win3;
342  win4Idx = win3Idx;
343  win3 = zed;
344  win3Idx = i;
345 
346  if (win3 >= win2)
347  {
348  win3 = win2;
349  win3Idx = win2Idx;
350  win2 = zed;
351  win2Idx = i;
352 
353  if (win2 >= win1)
354  {
355  win2 = win1;
356  win2Idx = win1Idx;
357  win1 = zed;
358  win1Idx = i;
359  }
360  }
361  }
362  }
363  }
364 
365  if (win1 == win2)
366  {
367  if (win1 == win3)
368  {
369  if (win1 == win4)
370  {
371  probabilities[win1Idx] = 0.25;
372  probabilities[win2Idx] = 0.25;
373  probabilities[win3Idx] = 0.25;
374  probabilities[win4Idx] = 0.25;
375  }
376  else
377  {
378  probabilities[win1Idx] = 0.95 / 3.0;
379  probabilities[win2Idx] = 0.95 / 3.0;
380  probabilities[win3Idx] = 0.95 / 3.0;
381  probabilities[win4Idx] = 0.05;
382  }
383  }
384  else
385  {
386  probabilities[win1Idx] = 0.85 / 2.0;
387  probabilities[win2Idx] = 0.85 / 2.0;
388  probabilities[win3Idx] = 0.10;
389  probabilities[win4Idx] = 0.05;
390  }
391  }
392 
393  else
394  {
395  probabilities[win1Idx] = 0.65;
396  if (win2 == win3)
397  {
398  if (win2 == win4)
399  {
400  probabilities[win2Idx] = 0.30 / 3.0;
401  probabilities[win3Idx] = 0.30 / 3.0;
402  probabilities[win4Idx] = 0.30 / 3.0;
403  }
404  else
405  {
406  probabilities[win2Idx] = 0.30 / 2.0;
407  probabilities[win3Idx] = 0.30 / 2.0;
408  probabilities[win4Idx] = 0.05;
409  }
410  }
411 
412  else
413  {
414  probabilities[win2Idx] = 0.20;
415  if (win3 == win4)
416  {
417  probabilities[win3Idx] = 0.15 / 2.0;
418  probabilities[win4Idx] = 0.15 / 2.0;
419  }
420  else
421  {
422  probabilities[win3Idx] = 0.10;
423  probabilities[win4Idx] = 0.05;
424  }
425  }
426  }
427 
428  //NormalizeProbabilitiesWithAMinumum (numClasses, probabilities, 0.001);
429 
430  for (i = 0; i < numClasses; ++i)
431  {
432  crossClassProbTable [win1Idx][i] = probabilities[win1Idx];
433  crossClassProbTable [i][win1Idx] = 1.0 - probabilities[win1Idx];
434  if (i != win1Idx)
435  {
436  crossClassProbTable [win2Idx][i] = probabilities[win2Idx];
437  crossClassProbTable [i][win2Idx] = 1.0 - probabilities[win2Idx];
438  if (i != win2Idx)
439  {
440  crossClassProbTable [win3Idx][i] = probabilities[win3Idx];
441  crossClassProbTable [i][win3Idx] = 1.0 - probabilities[win3Idx];
442  if (i != win3Idx)
443  {
444  crossClassProbTable [win4Idx][i] = probabilities[win4Idx];
445  crossClassProbTable [i][win4Idx] = 1.0 - probabilities[win4Idx];
446  }
447  }
448  }
449  }
450 
451  if ((knownClassNum >= 0) && (knownClassNum < numClasses))
452  {
453  kkint32 maxIndex1 = -1;
454  kkint32 maxIndex2 = -1;
455  maxIndex1 = GetMaxIndex (probabilities, numClasses, maxIndex2);
456 
457  if (probabilities[maxIndex1] >= confidence)
458  {
459  if ((probabilities[knownClassNum] < 1.0f) && (probabilities[knownClassNum] > 0.0f))
460  {
461  compact = -log ((double)probabilities[knownClassNum]);
462  }
463  }
464  }
465 } /* ComputeProbForVoting */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 GetMaxIndex(vector< T > &vote, kkint32 voteLength, kkint32 &maxIndex2)
Definition: SvmWrapper.cpp:67
template<class T >
kkint32 GetMaxIndex ( vector< T > &  vote,
kkint32  voteLength,
kkint32 maxIndex2 
)

Definition at line 67 of file SvmWrapper.cpp.

71 {
72  T max=vote[0];
73  kkint32 maxIndex=0;
74 
75  T max2 = 0;
76  maxIndex2 = -1;
77 
78  for (kkint32 i = 1; i < voteLength; i++)
79  {
80  if (vote[i]> max)
81  {
82  max2 = max;
83  maxIndex2 = maxIndex;
84  max = vote[i];
85  maxIndex = i;
86  }
87 
88  else if (maxIndex2 < 0)
89  {
90  max2 = vote[i];
91  maxIndex2 = i;
92  }
93 
94  else if (vote[i] > max2)
95  {
96  max2 = vote[i];
97  maxIndex2 = i;
98  }
99  }
100 
101  return maxIndex;
102 } /* GetMaxIndex */
__int32 kkint32
Definition: KKBaseTypes.h:88
#define max(X, Y)
Definition: UsfCasCor.h:248
template<class T >
kkint32 GetMaxIndex ( T *  vote,
kkint32  voteLength,
kkint32 maxIndex2 
)

Definition at line 108 of file SvmWrapper.cpp.

112 {
113  T max=vote[0];
114  kkint32 maxIndex=0;
115 
116  T max2 = 0;
117  maxIndex2 = -1;
118 
119  for (kkint32 i = 1; i < voteLength; i++)
120  {
121  if (vote[i]> max)
122  {
123  max2 = max;
124  maxIndex2 = maxIndex;
125  max = vote[i];
126  maxIndex = i;
127  }
128 
129  else if (maxIndex2 < 0)
130  {
131  max2 = vote[i];
132  maxIndex2 = i;
133  }
134 
135  else if (vote[i] > max2)
136  {
137  max2 = vote[i];
138  maxIndex2 = i;
139  }
140  }
141 
142  return maxIndex;
143 } /* GetMaxIndex */
__int32 kkint32
Definition: KKBaseTypes.h:88
#define max(X, Y)
Definition: UsfCasCor.h:248
bool GreaterThan ( kkint32  leftVotes,
double  leftProb,
kkint32  rightVotes,
double  rightProb 
)

Definition at line 151 of file SvmWrapper.cpp.

Referenced by GreaterVotes().

156 {
157  if (leftVotes < rightVotes)
158  return false;
159 
160  else if (leftVotes > rightVotes)
161  return true;
162 
163  else if (leftProb < rightProb)
164  return false;
165 
166  else if (leftProb > rightProb)
167  return true;
168 
169  else
170  return false;
171 } /* GreaterThan */
void GreaterVotes ( bool  useProbability,
kkint32  numClasses,
kkint32 votes,
double *  probabilities,
kkint32 pred1Idx,
kkint32 pred2Idx 
)

Definition at line 175 of file SvmWrapper.cpp.

References GreaterThan().

Referenced by KKMLL::SvmPredictClass().

182 {
183  if (useProbability)
184  {
185  pred1Idx = GetMaxIndex (probabilities, numClasses, pred2Idx);
186  return;
187  }
188 
189  kkint32 max1Votes = votes[0];
190  double max1Prob = probabilities[0];
191  pred1Idx = 0;
192 
193  kkint32 max2Votes = -1;
194  double max2Prob = -1.0f;
195  pred2Idx = -1;
196 
197  for (kkint32 x = 1; x < numClasses; x++)
198  {
199  if (GreaterThan (votes[x], probabilities[x], max1Votes, max1Prob))
200  {
201  max2Votes = max1Votes;
202  max2Prob = max1Prob;
203  pred2Idx = pred1Idx;
204  max1Votes = votes[x];
205  max1Prob = probabilities[x];
206  pred1Idx = x;
207  }
208  else if ((pred2Idx < 0) || GreaterThan (votes[x], probabilities[x], max2Votes, max2Prob))
209  {
210  max2Votes = votes[x];
211  max2Prob = probabilities[x];
212  pred2Idx = x;
213  }
214  }
215 } /* GreaterVotes*/
bool GreaterThan(kkint32 leftVotes, double leftProb, kkint32 rightVotes, double rightProb)
Definition: SvmWrapper.cpp:151
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 GetMaxIndex(vector< T > &vote, kkint32 voteLength, kkint32 &maxIndex2)
Definition: SvmWrapper.cpp:67
void NormalizeProbabilitiesWithAMinumum ( kkint32  numClasses,
double *  probabilities,
double  minProbability 
)

Will normailize probabilites such that the sum of all equal 1.0 and no one probability will be less than 'minProbability'.

Parameters
[in]numClassesNumber of classes represented in array 'probabilities'.
[in,out]probabilitiesProbabilites that are to be adjusted.
[in]minProbabilitySmallest probablity that any one class can have assigned to it.

Definition at line 255 of file SvmWrapper.cpp.

259 {
260  double sumGreaterOrEqualMin = 0.0;
261  kkint32 numLessThanMin = 0;
262 
263  kkint32 x = 0;
264  for (x = 0; x < numClasses; ++x)
265  {
266  if (probabilities[x] < minProbability)
267  ++numLessThanMin;
268  else
269  sumGreaterOrEqualMin += probabilities[x];
270  }
271 
272  double probLessMinTotal = numLessThanMin * minProbability;
273  double probLeftToAllocate = 1.0 - probLessMinTotal;
274 
275  for (x = 0; x < numClasses; ++x)
276  {
277  if (probabilities[x] < minProbability)
278  probabilities[x] = minProbability;
279  else
280  probabilities[x] = (probabilities[x] / sumGreaterOrEqualMin) * probLeftToAllocate;
281  }
282 } /* NormalizeProbabilitiesWithAMinumum */
__int32 kkint32
Definition: KKBaseTypes.h:88
void saveData ( svm_problem  ds,
kkint32  begin,
kkint32  end,
std::string  name 
)
void saveData ( svm_problem  ds,
kkint32  begin,
kkint32  end,
string  name 
)

Definition at line 223 of file SvmWrapper.cpp.

References SVM233::svm_node::index, and SVM233::svm_problem::x.

228 {
229  ofstream out(name.c_str());
230  if(!out.good())
231  {
232  cout << " cannot open " << name << endl;
233  exit(-1);
234  }
235  for(kkint32 i=begin; i<end; i++)
236  {
237  svm_node* temp=ds.x[i];
238  while(temp->index!=-1)
239  {
240  out << temp->value << ",";
241  temp++;
242  }
243  out << ds.y[i] << endl;
244  }
245  out.close();
246 }
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
double value
Definition: svm.h:39
kkint16 index
Definition: svm.h:38
struct svm_node ** x
Definition: svm.h:52
double * y
Definition: svm.h:49