KSquare Utilities
SVMModel.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <stdio.h>
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 #include <math.h>
8 #include <vector>
9 #include <sstream>
10 #include <iomanip>
11 #include <set>
12 #include <vector>
13 #include "MemoryDebug.h"
14 using namespace std;
15 
16 
17 #include "GlobalGoalKeeper.h"
18 #include "KKBaseTypes.h"
19 #include "KKException.h"
20 #include "KKStr.h"
21 #include "OSservices.h"
22 #include "RunLog.h"
23 #include "XmlStream.h"
24 using namespace KKB;
25 
26 
27 #include "SVMModel.h"
28 #include "KKMLLTypes.h"
29 #include "BinaryClassParms.h"
30 #include "FeatureEncoder.h"
31 #include "FeatureNumList.h"
32 #include "FeatureVector.h"
33 #include "SvmWrapper.h"
34 using namespace KKMLL;
35 
36 
37 
38 template<class T>
39 void GetMaxIndex (T* vote,
40  kkint32 voteLength,
41  kkint32& maxIndex1,
42  kkint32& maxIndex2
43  )
44 {
45  T max1 = vote[0];
46  maxIndex1 = 0;
47 
48  T max2 = 0;
49  maxIndex2 = -1;
50 
51  for (kkint32 i = 1; i < voteLength; i++)
52  {
53  if (vote[i] > max1)
54  {
55  max2 = max1;
56  maxIndex2 = maxIndex1;
57  max1 = vote[i];
58  maxIndex1 = i;
59  }
60 
61  else if (maxIndex2 < 0)
62  {
63  max2 = vote[i];
64  maxIndex2 = i;
65  }
66 
67  else if (vote[i] > max2)
68  {
69  max2 = vote[i];
70  maxIndex2 = i;
71  }
72  }
73 
74  return;
75 } /* GetMaxIndex */
76 
77 
78 
79 
80 
81 bool SVMModel::GreaterThan (kkint32 leftVotes,
82  double leftProb,
83  kkint32 rightVotes,
84  double rightProb
85  )
86 {
87  if (leftVotes < rightVotes)
88  return false;
89 
90  else if (leftVotes > rightVotes)
91  return true;
92 
93  else if (leftProb < rightProb)
94  return false;
95 
96  else if (leftProb > rightProb)
97  return true;
98 
99  else
100  return false;
101 } /* GreaterThan */
102 
103 
104 
105 double AdjProb (double prob)
106 {
107  if (prob < 0.001)
108  prob = 0.001;
109  else if (prob > 0.999)
110  prob = 0.999;
111  return prob;
112 }
113 
114 
115 void SVMModel::GreaterVotes (bool useProbability,
116  kkint32 numClasses,
117  kkint32* votes,
118  kkint32& numOfWinners,
119  double* probabilities,
120  kkint32& pred1Idx,
121  kkint32& pred2Idx
122  )
123 {
124  if (useProbability)
125  {
126  GetMaxIndex (probabilities, numClasses, pred1Idx , pred2Idx);
127  numOfWinners = 1;
128  return;
129  }
130 
131  kkint32 max1Votes = votes[0];
132  double max1Prob = probabilities[0];
133  pred1Idx = 0;
134  numOfWinners = 1;
135 
136  kkint32 max2Votes = -1;
137  double max2Prob = -1.0f;
138  pred2Idx = -1;
139 
140  for (kkint32 x = 1; x < numClasses; x++)
141  {
142  if (votes[x] > max1Votes)
143  numOfWinners = 1;
144 
145  else if (votes[x] == max1Votes)
146  numOfWinners++;
147 
148  if (GreaterThan (votes[x], probabilities[x], max1Votes, max1Prob))
149  {
150  max2Votes = max1Votes;
151  max2Prob = max1Prob;
152  pred2Idx = pred1Idx;
153  max1Votes = votes[x];
154  max1Prob = probabilities[x];
155  pred1Idx = x;
156  }
157  else if ((pred2Idx < 0) || GreaterThan (votes[x], probabilities[x], max2Votes, max2Prob))
158  {
159  max2Votes = votes[x];
160  max2Prob = probabilities[x];
161  pred2Idx = x;
162  }
163  }
164 } /* GreaterVotes*/
165 
166 
167 
168 
170  assignments (),
171  binaryFeatureEncoders (NULL),
172  binaryParameters (NULL),
173  cardinality_table (),
174  cancelFlag (false),
175  classIdxTable (NULL),
176  crossClassProbTable (NULL),
177  crossClassProbTableSize (0),
178  featureEncoder (NULL),
179  fileDesc (NULL),
180  models (NULL),
181  numOfClasses (0),
182  numOfModels (0),
183  oneVsAllAssignment (),
184  oneVsAllClassAssignments (NULL),
185  predictXSpace (NULL),
186  predictXSpaceWorstCase (0),
187  probabilities (NULL),
188  rootFileName (),
189  selectedFeatures (NULL),
190  svmParam (NULL),
191  trainingTime (0.0),
192  type_table (),
193  validModel (true),
194  votes (NULL),
195  xSpaces (NULL),
196  xSpacesTotalAllocated (0)
197 {
198  svmParam = new SVMparam ();
199 }
200 
201 
202 
203 SVMModel::SVMModel (const SVMparam& _svmParam, // Create new model from
204  FeatureVectorList& _examples, // Training data.
205  ClassAssignments& _assignmnets,
206  FileDescPtr _fileDesc,
207  RunLog& _log
208  )
209 :
210  assignments (_assignmnets),
211  binaryFeatureEncoders (NULL),
212  binaryParameters (NULL),
213  cancelFlag (false),
214  cardinality_table (),
215  classIdxTable (NULL),
216  crossClassProbTable (NULL),
217  crossClassProbTableSize (0),
218  featureEncoder (NULL),
219  fileDesc (_fileDesc),
220  models (NULL),
221  numOfClasses (0),
222  numOfModels (0),
223  oneVsAllAssignment (),
224  oneVsAllClassAssignments (NULL),
225  predictXSpace (NULL),
226  predictXSpaceWorstCase (0),
227  probabilities (NULL),
228  rootFileName (),
229  selectedFeatures (NULL),
230  svmParam (new SVMparam (_svmParam)),
231  trainingTime (0.0),
232  type_table (),
233  validModel (true),
234  votes (NULL),
235  xSpaces (NULL),
236  xSpacesTotalAllocated (0)
237 
238 {
239  if (_examples.QueueSize () < 2)
240  {
241  _log.Level (-1) << endl
242  << "SVMModel **** ERROR **** NO EXAMPLES TO TRAIN WITH." << endl
243  << endl;
244  validModel = false;
245  return;
246  }
247 
248  SetSelectedFeatures (_svmParam.SelectedFeatures (), _log);
249 
250  type_table = fileDesc->CreateAttributeTypeTable ( );
251  cardinality_table = fileDesc->CreateCardinalityTable ( );
252 
253  struct svm_problem prob;
254 
255  numOfClasses = (kkint32)assignments.size ();
256 
257  _log.Level (20) << "SVMModel::SVMModel - Constructing From Training Data." << endl;
258 
259 
260  try
261  {
263  {
264  ConstructOneVsOneModel (&_examples, prob, _log);
265  }
266  else if (svmParam->MachineType () == SVM_MachineType::OneVsAll)
267  {
268  ConstructOneVsAllModel (&_examples, prob, _log);
269  }
270  else if (svmParam->MachineType () == SVM_MachineType::BinaryCombos)
271  {
272  ConstructBinaryCombosModel (&_examples, _log);
273  }
274  }
275  catch (const exception& e)
276  {
277  _log.Level (-1) << endl
278  << "SVMModel ***ERROR*** Exception occurred constructing model." << endl
279  << e.what () << endl
280  << endl;
281  validModel = false;
282  }
283  catch (...)
284  {
285  _log.Level (-1) << endl
286  << "SVMModel ***ERROR*** Exception occurred constructing model." << endl
287  << endl;
288  validModel = false;
289  }
290 
291  if (cancelFlag)
292  validModel = false;
293 
294  if (!validModel)
295  return;
296 
297  BuildClassIdxTable ();
298  BuildCrossClassProbTable ();
299 }
300 
301 
302 
304 {
305  kkint32 x;
306 
307  DeleteModels ();
308  DeleteXSpaces ();
309 
310  if (oneVsAllClassAssignments)
311  {
312  for (kkint32 x = 0; x < numOfModels; x++)
313  {
314  if (oneVsAllClassAssignments[x])
315  {
316  delete oneVsAllClassAssignments[x];
317  oneVsAllClassAssignments[x] = NULL;
318  }
319  }
320  delete oneVsAllClassAssignments;
321  oneVsAllClassAssignments = NULL;
322  }
323 
324  delete featureEncoder; featureEncoder = NULL;
325 
326  if (binaryParameters)
327  {
328  // We don't own the binaryParametres them self, only the
329  // array pointing to them, they are owned by svmParam.
330  // so we don't want to delete the contents.
331  delete[] binaryParameters;
332  binaryParameters = NULL;
333  }
334 
335  if (binaryFeatureEncoders)
336  {
337  for (x = 0; x < numOfModels; x++)
338  {
339  delete binaryFeatureEncoders[x];
340  binaryFeatureEncoders[x] = NULL;
341  }
342  delete[] binaryFeatureEncoders;
343  binaryFeatureEncoders = NULL;
344  }
345 
346  if (crossClassProbTable)
347  {
348  for (x = 0; x < crossClassProbTableSize; x++)
349  {
350  delete [] crossClassProbTable[x];
351  crossClassProbTable[x] = NULL;
352  }
353  delete[] crossClassProbTable;
354  crossClassProbTable = NULL;
355  }
356 
357 
358  delete [] classIdxTable; classIdxTable = NULL;
359  delete [] predictXSpace; predictXSpace = NULL;
360  delete [] probabilities; probabilities = NULL;
361  delete [] votes; votes = NULL;
362 
363  delete selectedFeatures; selectedFeatures = NULL;
364  delete svmParam; svmParam = NULL;
365 }
366 
367 
368 
369 void SVMModel::DeleteModels ()
370 {
371  if (models)
372  {
373  for (kkint32 x = 0; x < numOfModels; x++)
374  {
375  if (models[x] != NULL)
376  {
377  SvmDestroyModel (models[x]);
378  delete[] models[x];
379  models[x] = NULL;
380  }
381  }
382 
383  delete[] models;
384  models = NULL;
385  }
386 }
387 
388 
389 void SVMModel::DeleteXSpaces ()
390 {
391  if (xSpaces)
392  {
393  for (kkint32 x = 0; x < numOfModels; x++)
394  {
395  if (xSpaces[x] != NULL)
396  {
397  delete (xSpaces[x]);
398  xSpaces[x] = NULL;
399  }
400  }
401 
402  delete[] xSpaces;
403  xSpaces = NULL;
404  xSpacesTotalAllocated = 0;
405  }
406 }
407 
408 
409 
410 
411 
412 
413 void SVMModel::AllocateModels ()
414 {
415  models = new ModelPtr [numOfModels];
416  {
417  for (kkint32 x = 0; x < numOfModels; x++)
418  {
419  models[x] = new SvmModel233*[1];
420  models[x][0] = NULL;
421  }
422  }
423 }
424 
425 
426 void SVMModel::AllocateXSpaces ()
427 {
428  xSpaces = new XSpacePtr [numOfModels];
429  {
430  for (kkint32 x = 0; x < numOfModels; x++)
431  xSpaces[x] = NULL;
432  }
433 }
434 
435 
436 
438 {
439  kkint32 memoryConsumedEstimated = sizeof (SVMModel)
440  + assignments.MemoryConsumedEstimated ()
441  + sizeof (kkint16) * oneVsAllAssignment.size ()
442  + sizeof (kkint32) * cardinality_table.size ()
443  + rootFileName.MemoryConsumedEstimated ()
444  + svmParam->MemoryConsumedEstimated ()
445  + sizeof (AttributeType) * type_table.size ();
446 
447  if (binaryFeatureEncoders)
448  {
449  memoryConsumedEstimated += sizeof (FeatureEncoderPtr) * numOfModels;
450  for (kkint32 x = 0; x < numOfModels; x++)
451  {
452  if (binaryFeatureEncoders[x])
453  memoryConsumedEstimated += binaryFeatureEncoders[x]->MemoryConsumedEstimated ();
454  }
455  }
456 
457  if (binaryParameters)
458  memoryConsumedEstimated += numOfModels * sizeof (BinaryClassParmsPtr);
459 
460  if (classIdxTable)
461  memoryConsumedEstimated += numOfClasses * sizeof (MLClassPtr);
462 
463  if (crossClassProbTable)
464  memoryConsumedEstimated += crossClassProbTableSize * crossClassProbTableSize * sizeof (double);
465 
466  if (featureEncoder)
467  memoryConsumedEstimated += featureEncoder->MemoryConsumedEstimated ();
468 
469  // fileDesc We do not own 'filedesc'.
470 
471  if (models)
472  {
473  memoryConsumedEstimated += numOfModels * sizeof (ModelPtr);
474  for (kkint32 x = 0; x < numOfModels; ++x)
475  {
476  if (models[x])
477  if (models[x][0])
478  memoryConsumedEstimated += models[x][0]->MemoryConsumedEstimated ();
479  }
480  }
481 
482  if (oneVsAllClassAssignments)
483  {
484  memoryConsumedEstimated += numOfModels * sizeof (ClassAssignmentsPtr);
485  for (kkint32 x = 0; x < numOfModels; ++x)
486  {
487  if (oneVsAllClassAssignments[x])
488  memoryConsumedEstimated += oneVsAllClassAssignments[x]->MemoryConsumedEstimated ();
489  }
490  }
491 
492  if (predictXSpace)
493  memoryConsumedEstimated += sizeof (svm_node) * predictXSpaceWorstCase;
494 
495  if (probabilities)
496  memoryConsumedEstimated += sizeof (double) * numOfClasses;
497 
498  if (votes)
499  memoryConsumedEstimated += sizeof (kkint32) * numOfClasses;
500 
501  if (xSpaces)
502  memoryConsumedEstimated += xSpacesTotalAllocated * sizeof (svm_node) + numOfModels * sizeof (XSpacePtr);
503 
504  return memoryConsumedEstimated;
505 } /* MemoryConsumedEstimated */
506 
507 
508 
509 
510 
511 
512 
513 
514 
515 void SVMModel::CancelFlag (bool _cancelFlag)
516 {
517  cancelFlag = _cancelFlag;
518 }
519 
520 
521 
522 
523 void SVMModel::BuildClassIdxTable ()
524 {
525  delete[] classIdxTable;
526 
527  classIdxTable = new MLClassPtr[numOfClasses];
528  for (kkint32 classIdx = 0; classIdx < numOfClasses; classIdx++)
529  classIdxTable[classIdx] = assignments.GetMLClassByIndex (classIdx);
530 
531  probabilities = new double[numOfClasses + 2]; // I am add 2 as a desperate move to deal with some kind of memory corruption kak
532  votes = new kkint32[numOfClasses + 2]; //
533 
534  for (kkint32 x = 0; x < numOfClasses; x++)
535  {
536  probabilities[x] = 0.0;
537  votes[x] = 0;
538  }
539 
540 } /* BuildClassIdxTable */
541 
542 
543 
544 void SVMModel::BuildProblemOneVsAll (FeatureVectorList& examples,
545  struct svm_problem& prob,
546  XSpacePtr& xSpace,
547  const MLClassList& classesThisAssignment,
548  FeatureEncoderPtr featureEncoder,
549  MLClassList& allClasses,
550  ClassAssignmentsPtr& classAssignments,
551  RunLog& log
552  )
553 {
554  log.Level (20) << "SVMModel::BuildProblemOneVsAll" << endl;
555 
556  if (!selectedFeatures)
557  {
558  FeatureNumListPtr tempFeatures = new FeatureNumList (examples.FileDesc ());
559  SetSelectedFeatures (tempFeatures, log);
560  delete tempFeatures;
561  tempFeatures = NULL;
562  }
563 
564  kkint32 numOfFeaturesSelected = selectedFeatures->NumOfFeatures ();
565 
566  MLClassPtr mlClass = NULL;
567  delete classAssignments;
568  classAssignments = new ClassAssignments ();
569 
570  MLClassList::const_iterator idx;
571 
572  for (idx = allClasses.begin (); idx != allClasses.end (); idx++)
573  {
574  mlClass = *idx;
575  if (classesThisAssignment.PtrToIdx (mlClass) >= 0)
576  {
577  // We are looking at a class that is to be treated logically as the 'one' class in 'One-vs-All'
578  classAssignments->AddMLClass (mlClass, 0, log);
579  }
580  else
581  {
582  // We are looking at a class that is to be treated logically as one of the 'all' classes in 'One-vs-All'
583  classAssignments->AddMLClass (mlClass, 1, log);
584  }
585  }
586 
587  kkint32 totalxSpaceUsed;
588 
589  featureEncoder->EncodeIntoSparseMatrix (&examples,
590  *classAssignments,
591  xSpace,
592  totalxSpaceUsed,
593  prob,
594  log
595  );
596 
597  xSpacesTotalAllocated += totalxSpaceUsed;
598 
599  if (svmParam->Param ().gamma == 0)
600  svmParam->Gamma_Param (1.0 / numOfFeaturesSelected);
601 
602  return;
603 } /* BuildProblemOneVsAll */
604 
605 
606 
607 void SVMModel::BuildProblemBinaryCombos (FeatureVectorListPtr class1Examples,
608  FeatureVectorListPtr class2Examples,
609  BinaryClassParmsPtr& _twoClassParms,
610  FeatureEncoderPtr& _encoder,
611  struct svm_problem& prob,
612  XSpacePtr& xSpace,
613  MLClassPtr class1,
614  MLClassPtr class2,
615  RunLog& log
616  )
617 {
618  log.Level (10) << "SVMModel::BuildProblemBinaryCombos Class1[" << class1->Name () << "] Class2[" << class2->Name () << "]" << endl;
619 
620  kkint32 totalxSpaceUsed = 0;
621 
622  ClassAssignments binaryAssignments;
623  binaryAssignments.AddMLClass (class1, 0, log);
624  binaryAssignments.AddMLClass (class2, 1, log);
625 
626  _twoClassParms = svmParam->GetParamtersToUseFor2ClassCombo (class1, class2);
627 
628  FeatureVectorListPtr twoClassExamples
629  = new FeatureVectorList (fileDesc,
630  false
631  );
632  twoClassExamples->AddQueue (*class1Examples);
633  twoClassExamples->AddQueue (*class2Examples);
634 
635  FeatureNumListConstPtr selFeatures = svmParam->GetFeatureNums (fileDesc, class1, class2);
636 
637  _encoder = new FeatureEncoder (fileDesc,
638  class1,
639  class2,
640  *selFeatures,
641  svmParam->EncodingMethod (),
642  svmParam->C_Param ()
643  );
644 
645  _encoder->EncodeIntoSparseMatrix (twoClassExamples,
646  binaryAssignments,
647  xSpace,
648  totalxSpaceUsed,
649  prob,
650  log
651  );
652 
653  xSpacesTotalAllocated += totalxSpaceUsed;
654 
655  if (_twoClassParms->Param ().gamma == 0)
656  {
657  _twoClassParms->Gamma (1.0f / (float)_encoder->CodedNumOfFeatures ());
658  cout << endl << endl
659  << "Gamma was set to ZERO" << endl
660  << endl;
661  }
662 
663  delete twoClassExamples;
664  twoClassExamples = NULL;
665 } /* BuildProblemBinaryCombos */
666 
667 
668 
669 
670 
671 void SVMModel::BuildCrossClassProbTable ()
672 {
673  kkint32 x, y;
674 
675  if (crossClassProbTable)
676  {
677  for (x = 0; x < crossClassProbTableSize; x++)
678  delete crossClassProbTable[x];
679  delete[] crossClassProbTable;
680  crossClassProbTable = NULL;
681  }
682 
683  crossClassProbTable = NULL;
684 
685  if (numOfClasses > 0)
686  {
687  crossClassProbTableSize = numOfClasses;
688  crossClassProbTable = new double*[crossClassProbTableSize + 2]; // I am adding 2 to deal with a memory corruption problem. kak
689  for (x = 0; x < crossClassProbTableSize; x++)
690  {
691  crossClassProbTable[x] = new double[crossClassProbTableSize + 2];
692  for (y = 0; y < crossClassProbTableSize; y++)
693  crossClassProbTable[x][y] = 0.0;
694  }
695  }
696 } /* BuildCrossClassProbTable */
697 
698 
699 
700 
702 {
703  return svmParam->GetFeatureNums ();
704 }
705 
706 
707 
708 FeatureNumListConstPtr SVMModel::GetFeatureNums (FileDescPtr fileDesc) const
709 {
710  return svmParam->GetFeatureNums (fileDesc);
711 }
712 
713 
714 
716  MLClassPtr class1,
717  MLClassPtr class2
718  ) const
719 {
720  return svmParam->GetFeatureNums (fileDesc, class1, class2);
721 } /* GetFeatureNums */
722 
723 
724 
725 
726 
727 
728 double SVMModel::DistanceFromDecisionBoundary (FeatureVectorPtr example,
729  MLClassPtr class1,
730  MLClassPtr class2
731  )
732 {
734  {
735  cerr << endl << "SVMModel::DistanceFromDecisionBoundary ***ERROR*** This method only works with BinaryCombos." << endl << endl;
736  return 0.0;
737  }
738 
739  kkint32 modelIDX = 0;
740  bool revClassOrder = false;
741  FeatureEncoderPtr encoder = NULL;
742 
743  while (modelIDX < numOfModels)
744  {
745  encoder = binaryFeatureEncoders[modelIDX];
746  if ((encoder->Class1 () == class1) && (encoder->Class2 () == class2))
747  {
748  revClassOrder = false;
749  break;
750  }
751  else if ((encoder->Class1 () == class2) && (encoder->Class2 () == class1))
752  {
753  revClassOrder = true;
754  break;
755  }
756 
757  encoder = NULL;
758  modelIDX++;
759  }
760 
761  if (encoder == NULL)
762  {
763  return 0.0;
764  }
765 
766  kkint32 xSpaceUsed;
767  encoder->EncodeAExample (example, predictXSpace, xSpaceUsed);
768 
769  double distance = 0.0;
770 
771  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, -1);
772 
773  if (revClassOrder)
774  distance = 0.0 - distance;
775 
776  return distance;
777 } /* DistanceFromDecisionBoundary */
778 
779 
780 
781 void SVMModel::InializeProbClassPairs ()
782 {
783  if (svmParam->ProbClassPairs ().size () < 1)
784  {
785  svmParam->ProbClassPairsInitialize (assignments);
786  }
787 } /* InializeProbClassPairs */
788 
789 
790 
791 
792 void SVMModel::Predict (FeatureVectorPtr example,
793  MLClassPtr knownClass,
794  MLClassPtr& predClass1,
795  MLClassPtr& predClass2,
796  kkint32& predClass1Votes,
797  kkint32& predClass2Votes,
798  double& probOfKnownClass,
799  double& predClass1Prob,
800  double& predClass2Prob,
801  kkint32& numOfWinners,
802  bool& knownClassOneOfTheWinners,
803  double& breakTie
804  )
805 {
806  InializeProbClassPairs ();
807 
808  breakTie = 0.0f; // experiments.
809 
810  knownClassOneOfTheWinners = false;
811 
812  predClass1 = NULL;
813  predClass2 = NULL;
814 
815  predClass1Votes = -1;
816  predClass2Votes = -1;
817 
818  probOfKnownClass = 0.0;
819  predClass1Prob = 0.0;
820  predClass2Prob = -1.0f;
821 
822  numOfWinners = 0;
823 
825  {
826  EncodeExample (example, predictXSpace);
827  PredictOneVsAll (predictXSpace, // used to be xSpace,
828  knownClass,
829  predClass1,
830  predClass2,
831  probOfKnownClass,
832  predClass1Prob,
833  predClass2Prob,
834  numOfWinners,
835  knownClassOneOfTheWinners,
836  breakTie
837  );
838  predClass1Votes = 1;
839  predClass2Votes = 1;
840  // free (xSpace); // We are now allocating only once.
841  }
842 
843  else if (svmParam->MachineType () == SVM_MachineType::OneVsOne)
844  {
845  EncodeExample (example, predictXSpace);
846 
847  kkint32 knownClassNum = -1;
848  kkint32 prediction = -1;
849  kkint32 prediction2 = -1;
850 
851  if (knownClass)
852  knownClassNum = assignments.GetNumForClass (knownClass);
853 
854  vector<kkint32> winners;
855 
856  SvmPredictClass (*svmParam,
857  models[0],
858  predictXSpace,
859  votes,
860  probabilities,
861  knownClassNum,
862  prediction,
863  prediction2,
864  predClass1Votes,
865  predClass2Votes,
866  predClass1Prob,
867  predClass2Prob,
868  probOfKnownClass,
869  winners,
870  crossClassProbTable,
871  breakTie
872  );
873 
874  numOfWinners = (kkint32)winners.size ();
875 
876  for (kkint32 idx = 0; idx < (kkint32)winners.size (); idx++)
877  {
878  if (winners[idx] == knownClassNum)
879  {
880  knownClassOneOfTheWinners = true;
881  break;
882  }
883  }
884 
885  predClass1 = assignments.GetMLClass (prediction);
886  predClass2 = assignments.GetMLClass (prediction2);
887  //free (xSpace);
888  }
889 
890 
891  else if (svmParam->MachineType () == SVM_MachineType::BinaryCombos)
892  {
893  PredictByBinaryCombos (example,
894  knownClass,
895  predClass1,
896  predClass2,
897  predClass1Votes,
898  predClass2Votes,
899  probOfKnownClass,
900  predClass1Prob,
901  predClass2Prob,
902  breakTie,
903  numOfWinners,
904  knownClassOneOfTheWinners
905  );
906  }
907 
908  else
909  {
910  KKStr errMsg = "***ERROR*** SVMModel::Predict Invalid Machine Type Specified.";
911  cerr << endl << errMsg << endl << endl;
912  throw KKException (errMsg);
913  }
914 
915  return;
916 } /* Predict */
917 
918 
919 
920 
921 void SVMModel::PredictRaw (FeatureVectorPtr example,
922  MLClassPtr& predClass,
923  double& dist
924  )
925 {
926  InializeProbClassPairs ();
927 
928  dist = 0.0;
929  double label = 0.0;
930  EncodeExample (example, predictXSpace);
931  SvmPredictRaw (models[0], predictXSpace, label, dist);
932  predClass = assignments.GetMLClass ((kkint16)label);
933  return;
934 } /* PredictRaw */
935 
936 
937 
938 
939 void SVMModel::PredictOneVsAll (XSpacePtr xSpace,
940  MLClassPtr knownClass,
941  MLClassPtr& predClass1,
942  MLClassPtr& predClass2,
943  double& probOfKnownClass,
944  double& predClass1Prob,
945  double& predClass2Prob,
946  kkint32& numOfWinners,
947  bool& knownClassOneOfTheWinners,
948  double& breakTie
949  )
950 {
951  InializeProbClassPairs ();
952 
953  predClass1 = NULL;
954  predClass2 = NULL;
955  knownClassOneOfTheWinners = false;
956 
957  probOfKnownClass = 0.0;
958  predClass1Prob = 0.0;
959 
960  vector<kkint32> winningClasses;
961 
962  double* probabilities = new double [numOfModels + 2]; // I am adding 2 as a desperate measure to deal with a memory corruption problem kak
963 
964  double largestLosingProbability = FLT_MIN;
965  kkint32 largestLosingProbabilityIDX = -1;
966 
967  double secondLargestLosingProbability = FLT_MIN;
968  kkint32 secondLargestLosingProbabilityIDX = -1;
969 
970  double largestWinningProbability = FLT_MIN;
971  kkint32 largestWinningProbabilityIDX = -1;
972 
973  double secondLargestWinningProbability = FLT_MIN;
974  kkint32 secondLargestWinningProbabilityIDX = -1;
975 
976  kkint32 knownAssignmentIDX = -1;
977 
978  kkuint32 assignmentIDX;
979 
980  for (assignmentIDX = 0; assignmentIDX < oneVsAllAssignment.size (); ++assignmentIDX)
981  {
982  kkint16 assignmentNum = oneVsAllAssignment[assignmentIDX];
983 
984  kkint32 knownClassNum = -1;
985  kkint32 predClassNum1 = -1;
986  kkint32 predClassNum2 = -1;
987 
988  MLClassPtr classWeAreLookingAt = assignments.GetMLClassByIndex (assignmentNum);
989 
990  if (knownClass)
991  {
992  if (knownClass == classWeAreLookingAt)
993  {
994  knownAssignmentIDX = assignmentIDX;
995  knownClassNum = 0;
996  }
997  else
998  {
999  knownClassNum = 1;
1000  }
1001  }
1002 
1003  double predictedClassProbability = 0.0f;
1004  double predictedClassProbability2 = 0.0f;
1005  double knownClassProbabilioty = 0.0f;
1006 
1007  vector<kkint32> winners;
1008 
1009  double* tempProbabilities = new double[numOfClasses + 2]; // I am adding 2 as a desperate measure to deal with a memory corruption problem kak
1010  kkint32* tempVotes = new kkint32[numOfClasses + 2];
1011 
1012  kkint32 predClass1Votes = -1;
1013  kkint32 predClass2Votes = -1;
1014 
1015  SvmPredictClass (*svmParam,
1016  models[assignmentIDX],
1017  xSpace,
1018  tempVotes,
1019  tempProbabilities,
1020  predClass1Votes,
1021  predClass2Votes,
1022  knownClassNum,
1023  predClassNum1,
1024  predClassNum2,
1025  predictedClassProbability,
1026  predictedClassProbability2,
1027  knownClassProbabilioty,
1028  winners,
1029  crossClassProbTable,
1030  breakTie
1031  );
1032 
1033  delete[] tempVotes;
1034  tempVotes = NULL;
1035  delete[] tempProbabilities;
1036  tempProbabilities = NULL;
1037 
1038  if (predClassNum1 == 0)
1039  {
1040  winningClasses.push_back (assignmentIDX);
1041  probabilities[assignmentIDX] = predictedClassProbability;
1042  if (predictedClassProbability > largestWinningProbability)
1043  {
1044  secondLargestWinningProbability = largestWinningProbability;
1045  secondLargestWinningProbabilityIDX = largestWinningProbabilityIDX;
1046 
1047  largestWinningProbability = predictedClassProbability;
1048  largestWinningProbabilityIDX = assignmentIDX;
1049  }
1050  else if (predictedClassProbability > secondLargestWinningProbability)
1051  {
1052  secondLargestWinningProbability = predictedClassProbability;
1053  secondLargestWinningProbabilityIDX = assignmentIDX;
1054  }
1055  }
1056  else
1057  {
1058  probabilities[assignmentIDX] = 1.0 - predictedClassProbability;
1059  if (probabilities[assignmentIDX] > largestLosingProbability)
1060  {
1061  secondLargestLosingProbabilityIDX = largestLosingProbabilityIDX;
1062  secondLargestLosingProbability = largestLosingProbability;
1063 
1064  largestLosingProbabilityIDX = assignmentIDX;
1065  largestLosingProbability = probabilities[assignmentIDX];
1066  }
1067  else if (probabilities[assignmentIDX] > secondLargestLosingProbability)
1068  {
1069  secondLargestLosingProbabilityIDX = assignmentIDX;
1070  secondLargestLosingProbability = probabilities[assignmentIDX];
1071  }
1072  }
1073  }
1074 
1075  numOfWinners = (kkint32)winningClasses.size ();
1076 
1077  kkint32 assignmentIDXthatWon = -1;
1078  kkint32 assignmentIDXsecond = -1;
1079 
1080  if (winningClasses.size () <= 0)
1081  {
1082  // There were no winners, lets just use the 1 that had the highest probability.
1083 
1084  assignmentIDXthatWon = largestLosingProbabilityIDX;
1085  assignmentIDXsecond = secondLargestLosingProbabilityIDX;
1086 
1087  predClass1 = assignments.GetMLClassByIndex (oneVsAllAssignment[assignmentIDXthatWon]);
1088 
1089  knownClassOneOfTheWinners = false;
1090  }
1091 
1092  else if (winningClasses.size () == 1)
1093  {
1094  assignmentIDXthatWon = winningClasses[0];
1095  knownClassOneOfTheWinners = (assignmentIDXthatWon == knownAssignmentIDX);
1096  assignmentIDXsecond = largestLosingProbabilityIDX;
1097  }
1098 
1099  else
1100  {
1101  // We had more than one Winner
1102  assignmentIDXthatWon = largestWinningProbabilityIDX;
1103  assignmentIDXsecond = secondLargestLosingProbabilityIDX;
1104 
1105  for (kkint32 idx = 0; idx < (kkint32)winningClasses.size (); idx++)
1106  {
1107  if (winningClasses[idx] == knownAssignmentIDX)
1108  {
1109  knownClassOneOfTheWinners = true;
1110  break;
1111  }
1112  }
1113  }
1114 
1115  predClass1 = assignments.GetMLClassByIndex (oneVsAllAssignment[assignmentIDXthatWon]);
1116  predClass2 = assignments.GetMLClassByIndex (oneVsAllAssignment[assignmentIDXsecond]);
1117 
1118  predClass1Prob = (probabilities [oneVsAllAssignment[assignmentIDXthatWon]]);
1119  probOfKnownClass = (probabilities [oneVsAllAssignment[knownAssignmentIDX]]);
1120 
1121  delete[] probabilities;
1122  probabilities = NULL;
1123 
1124  return;
1125 } /* PredictOneVsAll */
1126 
1127 
1128 
1129 
1130 
1131 MLClassPtr SVMModel::Predict (FeatureVectorPtr example)
1132 {
1133  double breakTie = -1.0f;
1134  bool knownClassOneOfTheWinners = false;
1135  kkint32 numOfWinners = -1;
1136  MLClassPtr pred1 = NULL;
1137  MLClassPtr pred2 = NULL;
1138  double predClass1Prob = -1.0;
1139  double predClass2Prob = -1.0;
1140  double probOfKnownClass = -1.0;
1141 
1142  kkint32 predClass1Votes = -1;
1143  kkint32 predClass2Votes = -1;
1144 
1145  InializeProbClassPairs ();
1146 
1147  Predict (example,
1148  NULL,
1149  pred1,
1150  pred2,
1151  predClass1Votes,
1152  predClass2Votes,
1153  probOfKnownClass,
1154  predClass1Prob,
1155  predClass2Prob,
1156  numOfWinners,
1157  knownClassOneOfTheWinners,
1158  breakTie
1159  );
1160 
1161  return pred1;
1162 } /* Predict */
1163 
1164 
1165 
1166 
1167 
1168 void SVMModel::PredictByBinaryCombos (FeatureVectorPtr example,
1169  MLClassPtr knownClass,
1170  MLClassPtr& predClass1,
1171  MLClassPtr& predClass2,
1172  kkint32& predClass1Votes,
1173  kkint32& predClass2Votes,
1174  double& probOfKnownClass,
1175  double& predClass1Prob,
1176  double& predClass2Prob,
1177  double& breakTie,
1178  kkint32& numOfWinners,
1179  bool& knownClassOneOfTheWinners
1180  )
1181 {
1182  kkint32 classIDX;
1183 
1184  predClass1 = NULL;
1185  predClass2 = NULL;
1186  probOfKnownClass = -1.0f;
1187  predClass1Prob = -1.0f;
1188  predClass2Prob = -1.0f;
1189 
1190  predClass1Prob = 0.0;
1191  predClass2Prob = 0.0;
1192 
1193  knownClassOneOfTheWinners = false;
1194 
1195  double probability = -1.0;
1196 
1197  kkint32 knownClassIDX = numOfClasses - 1;
1198  kkint32 predClass1IDX = -1;
1199  kkint32 predClass2IDX = -1;
1200 
1201  kkint32 modelIDX = 0;
1202 
1203  for (kkint32 x = 0; x < numOfClasses; x++)
1204  {
1205  votes[x] = 0;
1206  probabilities[x] = 1.0f;
1207  }
1208 
1209  for (kkint32 class1IDX = 0; class1IDX < (numOfClasses - 1); class1IDX++)
1210  {
1211  MLClassPtr class1 = classIdxTable [class1IDX];
1212 
1213  if (class1 == knownClass)
1214  knownClassIDX = class1IDX;
1215 
1216  for (kkint32 class2IDX = (class1IDX + 1); class2IDX < numOfClasses; class2IDX++)
1217  {
1218  MLClassPtr class2 = classIdxTable [class2IDX];
1219  BinaryClassParmsPtr thisComboPrameters = binaryParameters[modelIDX];
1220 
1221  kkint32 xSpaceUsed;
1222 
1223  if (binaryFeatureEncoders[modelIDX] == NULL)
1224  {
1225  KKStr errMsg;
1226  errMsg << "SVMModel::PredictByBinaryCombos ***ERROR*** No feature encoder for model[" << modelIDX << "]";
1227  cerr << endl << errMsg << endl << endl;
1228  throw KKException (errMsg);
1229  }
1230 
1231  binaryFeatureEncoders[modelIDX]->EncodeAExample (example, predictXSpace, xSpaceUsed);
1232 
1233  double distance = 0.0;
1234  double margin = 0.0;
1235 
1236  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, -1);
1237  probability = (1.0 / (1.0 + exp (-1.0 * (thisComboPrameters->Param ().A) * distance)));
1238  probability = AdjProb (probability); // KAK 2011-06-10
1239 
1240  if (probability > 0.5)
1241  {
1242  votes[class1IDX]++;
1243  probabilities[class1IDX] *= probability;
1244  probabilities[class2IDX] *= (1.0f - probability);
1245  }
1246  else
1247  {
1248  votes[class2IDX]++;
1249  probabilities[class2IDX] *= (1.0 - probability);
1250  probabilities[class1IDX] *= probability;
1251  }
1252 
1253  modelIDX++;
1254  }
1255  }
1256 
1257  double totProbability = 0.0;
1258  for (classIDX = 0; classIDX < numOfClasses; classIDX++)
1259  totProbability += probabilities[classIDX];
1260 
1261  if (totProbability <= 0.0)
1262  totProbability = 1.0;
1263 
1264  for (classIDX = 0; classIDX < numOfClasses; classIDX++)
1265  probabilities[classIDX] = probabilities[classIDX] / totProbability;
1266 
1267  GreaterVotes (svmParam->SelectionMethod () == SVM_SelectionMethod::Probability,
1268  numOfClasses,
1269  votes,
1270  numOfWinners,
1271  probabilities,
1272  predClass1IDX,
1273  predClass2IDX
1274  );
1275 
1276  if (predClass1IDX >= 0)
1277  {
1278  predClass1 = classIdxTable [predClass1IDX];
1279  predClass1Votes = votes [predClass1IDX];
1280  predClass1Prob = probabilities [predClass1IDX];
1281  }
1282 
1283  if (predClass2IDX >= 0)
1284  {
1285  predClass2 = classIdxTable [predClass2IDX];
1286  predClass2Votes = votes [predClass2IDX];
1287  predClass2Prob = probabilities [predClass2IDX];
1288  }
1289 
1290  if (knownClassIDX >= 0)
1291  probOfKnownClass = probabilities[knownClassIDX];
1292 
1293 
1294  breakTie = fabs (predClass1Prob - predClass2Prob);
1295  knownClassOneOfTheWinners = (predClass1IDX == knownClassIDX);
1296 
1297  return;
1298 } /* PredictByBinaryCombos */
1299 
1300 
1301 
1302 
1303 
1305 {
1306  kkint32 numOfSupportVectors = 0;
1307 
1308  if (models == NULL)
1309  return 0;
1310 
1312  {
1313  vector<KKStr> svNames = SupportVectorNames ();
1314  numOfSupportVectors = (kkint32)svNames.size ();
1315  }
1316  else
1317  {
1318  for (kkint32 x = 0; x < numOfModels; x++)
1319  numOfSupportVectors += models[x][0]->l;
1320  }
1321 
1322  return numOfSupportVectors;
1323 } /* NumOfSupportVectors */
1324 
1325 
1326 
1327 
1329  kkint32& totalNumSVs
1330  )
1331 {
1332  numSVs = 0;
1333  totalNumSVs = 0;
1334  if (models == NULL)
1335  return;
1336 
1338  {
1339  numSVs = NumOfSupportVectors ();
1340  for (kkint32 modelIDX = 0; modelIDX < numOfModels; modelIDX++)
1341  {
1342  totalNumSVs += models[modelIDX][0]->l;
1343  }
1344  }
1345  else
1346  {
1347  svm_GetSupportVectorStatistics (models[0][0], numSVs, totalNumSVs);
1348  }
1349 
1350 } /* SupportVectorStatistics */
1351 
1352 
1353 
1354 void SVMModel::ProbabilitiesByClass (FeatureVectorPtr example,
1355  const MLClassList& _mlClasses,
1356  kkint32* _votes,
1357  double* _probabilities,
1358  RunLog& _log
1359  )
1360 {
1361  InializeProbClassPairs ();
1362 
1364  {
1365  PredictProbabilitiesByBinaryCombos (example, _mlClasses, _votes, _probabilities, _log);
1366  return;
1367  }
1368 
1369  kkint32 predClass1Votes = -1;
1370  kkint32 predClass2Votes = -1;
1371  double predClass1Prob = 0.0;
1372  double predClass2Prob = 0.0;
1373  double probOfKnownClass = 0.0;
1374  double smallestDistOfPredClass = 0.0;
1375 
1376  double breakTie = 0.0f;
1377 
1378  MLClassPtr predictedClass = NULL;
1379  XSpacePtr xSpace = NULL;
1380  kkint32 x, y;
1381 
1382  xSpace = featureEncoder->EncodeAExample (example);
1383 
1384  kkint32 knownClassNum = 0;
1385  kkint32 prediction = 0;
1386  kkint32 prediction2 = 0;
1387 
1388  for (x = 0; x < numOfClasses; x++)
1389  {
1390  probabilities [x] = 0.0;
1391  _probabilities[x] = 0.0;
1392  _votes [x] = 0;
1393  }
1394 
1395  vector<kkint32> winners;
1396 
1397  SvmPredictClass (*svmParam,
1398  models[0],
1399  xSpace,
1400  votes,
1401  probabilities,
1402  knownClassNum,
1403  prediction,
1404  prediction2,
1405  predClass1Votes,
1406  predClass2Votes,
1407  predClass1Prob,
1408  predClass2Prob,
1409  probOfKnownClass,
1410  winners,
1411  crossClassProbTable,
1412  breakTie
1413  );
1414 
1415  // We have to do it this way, so that we pass back the probabilities in the
1416  // same order that the user indicated with mlClasses being passed in.
1417  for (x = 0; x < numOfClasses; x++)
1418  {
1419  predictedClass = assignments.GetMLClass (x);
1420  if (predictedClass)
1421  {
1422  y = _mlClasses.PtrToIdx (predictedClass);
1423  if ((y < 0) || (y >= numOfClasses))
1424  {
1425  _log.Level (-1) << endl
1426  << "SVMModel::ProbabilitiesByClass ***ERROR***" << endl
1427  << " Invalid classIdx[" << y << "] Specified." << endl
1428  << " ClassName [" << predictedClass->Name () << "]" << endl
1429  << endl;
1430  }
1431  else
1432  {
1433  _votes [y] = votes [x];
1434  _probabilities[y] = probabilities[x];
1435  }
1436  }
1437  }
1438 
1439  delete xSpace;
1440  xSpace = NULL;
1441 
1442  return;
1443 } /* ProbabilitiesByClass */
1444 
1445 
1446 
1447 
1448 
1449 
1450 void SVMModel::PredictProbabilitiesByBinaryCombos (FeatureVectorPtr example,
1451  const MLClassList& _mlClasses,
1452  kkint32* _votes,
1453  double* _probabilities,
1454  RunLog& _log
1455  )
1456 {
1457  InializeProbClassPairs ();
1458 
1459  kkint32 classIDX;
1460 
1461  double probability = -1.0;
1462 
1463  kkint32 modelIDX = 0;
1464 
1465  for (kkint32 x = 0; x < numOfClasses; x++)
1466  {
1467  probabilities[x] = 1.0;
1468  votes[x] = 0;
1469  }
1470 
1471 
1472  if (numOfClasses != crossClassProbTableSize)
1473  {
1474  _log.Level (-1) << endl << endl
1475  << "SVMModel::PredictProbabilitiesByBinaryCombos ***ERROR***" << endl
1476  << " numfClasses != crossClassProbTableSize" << endl
1477  << endl;
1478  return;
1479  }
1480 
1481 
1482  for (kkint32 class1IDX = 0; class1IDX < (numOfClasses - 1); class1IDX++)
1483  {
1484 
1485  for (kkint32 class2IDX = (class1IDX + 1); class2IDX < numOfClasses; class2IDX++)
1486  {
1487  BinaryClassParmsPtr thisComboPrameters = binaryParameters[modelIDX];
1488 
1489  if (binaryFeatureEncoders[modelIDX] == NULL)
1490  {
1491  KKStr errMsg;
1492  errMsg << "SVMModel::PredictProbabilitiesByBinaryCombos ***ERROR*** No feature encoder for model[" << modelIDX << "]";
1493  _log.Level (-1) << endl << endl << errMsg << endl << endl;
1494  throw KKException (errMsg);
1495  }
1496 
1497  kkint32 xSpaceUsed;
1498  binaryFeatureEncoders[modelIDX]->EncodeAExample (example, predictXSpace, xSpaceUsed);
1499 
1500  double distance = 0.0;
1501 
1502  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, -1);
1503  probability = (1.0 / (1.0 + exp (-1.0 * (thisComboPrameters->Param ().A) * distance)));
1504  probability = AdjProb (probability); // KAK 2011-06-10
1505 
1506  crossClassProbTable[class1IDX][class2IDX] = probability;
1507  crossClassProbTable[class2IDX][class1IDX] = 1.0 - probability;
1508 
1509  if (probability > 0.5f)
1510  {
1511  probabilities[class1IDX] *= probability;
1512  probabilities[class2IDX] *= (1.0f - probability);
1513  votes[class1IDX]++;
1514  }
1515  else
1516  {
1517  probabilities[class2IDX] *= (1.0f - probability);
1518  probabilities[class1IDX] *= probability;
1519  votes[class2IDX]++;
1520  }
1521 
1522  modelIDX++;
1523  }
1524  }
1525 
1526  double totProbability = 0.0;
1527 
1528  for (classIDX = 0; classIDX < numOfClasses; classIDX++)
1529  {
1530  totProbability += probabilities[classIDX];
1531  }
1532 
1533  if (totProbability == 0.0f)
1534  totProbability = 1.0f;
1535 
1536  {
1537  kkint32 callersIdx = 0;
1538  MLClassList::const_iterator idx;
1539  for (idx = _mlClasses.begin (); idx != _mlClasses.end (); idx++)
1540  {
1541  kkint32 ourIdx = assignments.GetNumForClass (*idx);
1542  if ((ourIdx < 0) || (ourIdx >= numOfClasses))
1543  {
1544  // For what ever reason the MLClass instance in '_mlClasses' provided by caller
1545  // is not one of the classes that this model was built for.
1546  _log.Level (-1) << endl
1547  << "SVMModel::PredictProbabilitiesByBinaryCombos ***WARNING*** MLClass[" << (*idx)->Name () << "] is not one of the classes in SVMModel." << endl
1548  << endl;
1549  _votes [callersIdx] = 0;
1550  _probabilities[callersIdx] = 0.0;
1551  }
1552  else
1553  {
1554  _votes [callersIdx] = votes [ourIdx];
1555  _probabilities[callersIdx] = probabilities[ourIdx] / totProbability;
1556  }
1557  callersIdx++;
1558  }
1559  }
1560 
1561  return;
1562 } /* PredictByBinaryCombos */
1563 
1564 
1565 
1566 namespace KKMLL
1567 {
1569  {
1570  return l.probability > r.probability;
1571  }
1572 }
1573 
1574 
1575 
1576 vector<KKStr> SVMModel::SupportVectorNames (MLClassPtr c1,
1577  MLClassPtr c2
1578  ) const
1579 {
1580  vector<KKStr> results;
1581  if (svmParam->MachineType () != SVM_MachineType::BinaryCombos)
1582  return results;
1583 
1584  // Locate the binary parms in question.
1585  kkint32 modelIDX = 0;
1586  BinaryClassParmsPtr parms = NULL;
1587  for (modelIDX = 0; modelIDX < numOfModels; modelIDX++)
1588  {
1589  parms = binaryParameters[modelIDX];
1590  if ((parms->Class1 () == c1) && (parms->Class2 () == c2))
1591  break;
1592 
1593  else if ((parms->Class2 () == c1) && (parms->Class1 () == c2))
1594  break;
1595  }
1596 
1597  if (modelIDX >= numOfModels)
1598  {
1599  // Binary Combo not found.
1600  cerr << endl
1601  << "SVMModel::SupportVectorNames ***ERROR*** Class1[" << c1->Name () << "] Class2[" << c2->Name () << "] not part of model." << endl
1602  << endl;
1603  return results;
1604  }
1605 
1606  kkint32 numSVs = models[modelIDX][0]->l;
1607  kkint32 svIDX = 0;
1608  for (svIDX = 0; svIDX < numSVs; svIDX++)
1609  {
1610  KKStr svName = models[modelIDX][0]->SupportVectorName (svIDX);
1611  results.push_back (svName);
1612  }
1613  return results;
1614 } /* SupportVectorNames */
1615 
1616 
1617 
1619 {
1620  vector<KKStr> results;
1621  if (svmParam->MachineType () != SVM_MachineType::BinaryCombos)
1622  return results;
1623 
1624  map<KKStr,KKStr> names;
1625  map<KKStr,KKStr>::iterator svnIDX;
1626 
1627  // Locate the binary parms in question.
1628  kkint32 modelIDX = 0;
1629  BinaryClassParmsPtr parms = NULL;
1630  for (modelIDX = 0; modelIDX < numOfModels; modelIDX++)
1631  {
1632  parms = binaryParameters[modelIDX];
1633 
1634  kkint32 numSVs = models[modelIDX][0]->l;
1635  kkint32 svIDX = 0;
1636  for (svIDX = 0; svIDX < numSVs; svIDX++)
1637  {
1638  KKStr svName = models[modelIDX][0]->SupportVectorName (svIDX);
1639  svnIDX = names.find (svName);
1640  if (svnIDX == names.end ())
1641  {
1642  names.insert (pair<KKStr,KKStr>(svName, svName));
1643  results.push_back (svName);
1644  }
1645  }
1646  }
1647 
1648  return results;
1649 } /* SupportVectorNames */
1650 
1651 
1652 
1655  MLClassPtr c1,
1656  MLClassPtr c2
1657  )
1658 {
1659  vector<ProbNamePair> results;
1660  if (svmParam->MachineType () != SVM_MachineType::BinaryCombos)
1661  return results;
1662 
1663  // Locate the binary parms in question.
1664  bool c1RevFlag = false;
1665  kkint32 modelIDX = 0;
1666 
1667  BinaryClassParmsPtr parms = NULL;
1668 
1669  for (modelIDX = 0; modelIDX < numOfModels; modelIDX++)
1670  {
1671  parms = binaryParameters[modelIDX];
1672  if ((parms->Class1 () == c1) && (parms->Class2 () == c2))
1673  {
1674  c1RevFlag = false;
1675  break;
1676  }
1677  else if ((parms->Class2 () == c1) && (parms->Class1 () == c2))
1678  {
1679  c1RevFlag = true;
1680  break;
1681  }
1682  }
1683 
1684  if (modelIDX >= numOfModels)
1685  {
1686  // Binary Combo not found.
1687  cerr << endl
1688  << "SVMModel::FindWorstSupportVectors ***ERROR*** Class1[" << c1->Name () << "] Class2[" << c2->Name () << "] not part of model." << endl
1689  << endl;
1690  return results;
1691  }
1692 
1693  if (binaryFeatureEncoders[modelIDX] == NULL)
1694  {
1695  KKStr errMsg;
1696  errMsg << "SVMModel::FindWorstSupportVectors ***ERROR*** No feature encoder for model[" << modelIDX << "]";
1697  cerr << endl << errMsg << endl << endl;
1698  throw KKException (errMsg);
1699  }
1700 
1701  kkint32 xSpaceUsed;
1702  binaryFeatureEncoders[modelIDX]->EncodeAExample (example, predictXSpace, xSpaceUsed);
1703 
1704  kkint32 svIDX = 0;
1705  //kkint32 numSVs = models[modelIDX][0]->l;
1706  kkint32 numSVs = models[modelIDX][0]->l;
1707 
1708  double origProbabilityC1 = 0.0;
1709  double probabilityC1 = 0.0;
1710  double distance = 0.0;
1711 
1712  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, -1);
1713  origProbabilityC1 = ((1.0 / (1.0 + exp (-1.0 * (parms->Param ().A) * distance))));
1714  origProbabilityC1 = AdjProb (origProbabilityC1); // KAK 2011-06-10
1715 
1716  if (c1RevFlag)
1717  origProbabilityC1 = 1.0f - origProbabilityC1;
1718 
1719  vector<ProbNamePair> candidates;
1720 
1721  for (svIDX = 0; svIDX < numSVs; svIDX++)
1722  {
1723  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, svIDX);
1724  probabilityC1 = ((1.0 / (1.0 + exp (-1.0 * (parms->Param ().A) * distance))));
1725  probabilityC1 = AdjProb (probabilityC1); // KAK 2011-06-10
1726  if (c1RevFlag)
1727  probabilityC1 = 1.0f - probabilityC1;
1728 
1729  double deltaProb = probabilityC1 - origProbabilityC1;
1730  KKStr svName = models[modelIDX][0]->SupportVectorName (svIDX);
1731  candidates.push_back (ProbNamePair (svName, deltaProb));
1732  }
1733 
1734  sort (candidates.begin (), candidates.end (), PairCompareOperator);
1735 
1736  kkint32 zed = 0;
1737  for (zed = 0; (zed < (kkint32)candidates.size ()) && (zed < numToFind); zed++)
1738  results.push_back (candidates[zed]);
1739 
1740  return results;
1741 } /* FindWorstSupportVectors */
1742 
1743 
1744 
1747  MLClassPtr c1,
1748  MLClassPtr c2
1749  )
1750 {
1751  vector<ProbNamePair> results;
1752  if (svmParam->MachineType () != SVM_MachineType::BinaryCombos)
1753  return results;
1754 
1755  // Locate the binary parms in question.
1756  bool c1RevFlag = false;
1757  kkint32 modelIDX = 0;
1758 
1759  BinaryClassParmsPtr parms = NULL;
1760 
1761  for (modelIDX = 0; modelIDX < numOfModels; modelIDX++)
1762  {
1763  parms = binaryParameters[modelIDX];
1764  if ((parms->Class1 () == c1) && (parms->Class2 () == c2))
1765  {
1766  c1RevFlag = false;
1767  break;
1768  }
1769  else if ((parms->Class2 () == c1) && (parms->Class1 () == c2))
1770  {
1771  c1RevFlag = true;
1772  break;
1773  }
1774  }
1775 
1776  if (modelIDX >= numOfModels)
1777  {
1778  // Binary Combo not found.
1779  cerr << endl
1780  << "SVMModel::FindWorstSupportVectors ***ERROR*** Class1[" << c1->Name () << "] Class2[" << c2->Name () << "] not part of model." << endl
1781  << endl;
1782  return results;
1783  }
1784 
1785 
1786  if (binaryFeatureEncoders[modelIDX] == NULL)
1787  {
1788  KKStr errMsg;
1789  errMsg << "SVMModel::FindWorstSupportVectors2 ***ERROR*** No feature encoder for model[" << modelIDX << "]";
1790  cerr << endl << errMsg << endl << endl;
1791  throw KKException (errMsg);
1792  }
1793 
1794  kkint32 xSpaceUsed;
1795  binaryFeatureEncoders[modelIDX]->EncodeAExample (example, predictXSpace, xSpaceUsed);
1796 
1797  kkint32 svIDX = 0;
1798  //kkint32 numSVs = models[modelIDX][0]->l;
1799  kkint32 numSVs = models[modelIDX][0]->l;
1800 
1801  double origProbabilityC1 = 0.0;
1802  double probabilityC1 = 0.0;
1803  double distance = 0.0;
1804 
1805  svm_predictTwoClasses (models[modelIDX][0], predictXSpace, distance, -1);
1806  origProbabilityC1 = ((1.0 / (1.0 + exp (-1.0 * (parms->Param ().A) * distance))));
1807  origProbabilityC1 = AdjProb (origProbabilityC1);
1808  if (c1RevFlag)
1809  origProbabilityC1 = 1.0 - origProbabilityC1;
1810 
1811  vector<ProbNamePair> candidates;
1812 
1813  {
1814  svm_problem* subSetProb = svm_BuildProbFromTwoClassModel (models[modelIDX][0], -1);
1815  svm_parameter parameters = models[modelIDX][0]->param;
1816  parameters.nr_class = 2;
1817 
1818  SvmModel233* subSetModel = svm_train (subSetProb, &parameters);
1819 
1820  svm_predictTwoClasses (subSetModel, predictXSpace, distance, -1);
1821  probabilityC1 = ((1.0 / (1.0 + exp (-1.0 * (parms->Param ().A) * distance))));
1822  probabilityC1 = AdjProb (probabilityC1);
1823  if (c1RevFlag)
1824  probabilityC1 = 1.0f - probabilityC1;
1825  kkint32 zed = 100;
1826  }
1827 
1828  for (svIDX = 0; svIDX < numSVs; svIDX++)
1829  {
1830  svm_problem* subSetProb = svm_BuildProbFromTwoClassModel (models[modelIDX][0], svIDX);
1831  svm_parameter parameters = models[modelIDX][0]->param;
1832  parameters.nr_class = 2;
1833 
1834  SvmModel233* subSetModel = svm_train (subSetProb, &parameters);
1835 
1836  svm_predictTwoClasses (subSetModel, predictXSpace, distance, -1);
1837  probabilityC1 = ((1.0 / (1.0 + exp (-1.0 * (parms->Param ().A) * distance))));
1838  probabilityC1 = AdjProb (probabilityC1);
1839  if (c1RevFlag)
1840  probabilityC1 = 1.0f - probabilityC1;
1841 
1842  double deltaProb = probabilityC1 - origProbabilityC1;
1843 
1844  KKStr svName = models[modelIDX][0]->SupportVectorName (svIDX);
1845  candidates.push_back (ProbNamePair (svName, deltaProb));
1846 
1847  delete subSetModel; subSetModel = NULL;
1848  delete subSetProb; subSetProb = NULL;
1849  }
1850 
1851  sort (candidates.begin (), candidates.end (), PairCompareOperator);
1852 
1853  kkint32 zed = 0;
1854  for (zed = 0; (zed < (kkint32)candidates.size ()) && (zed < numToFind); zed++)
1855  results.push_back (candidates[zed]);
1856 
1857  return results;
1858 } /* FindWorstSupportVectors2 */
1859 
1860 
1861 
1862 void SVMModel::CalculatePredictXSpaceNeeded (RunLog& log)
1863 {
1864  if (!selectedFeatures)
1865  {
1866  KKStr errMsg = "SVMModel::CalculatePredictXSpaceNeeded ***ERROR*** numOfFeaturesSelected is NOT defined.";
1867  log.Level (-1) << endl << errMsg << endl
1868  << endl;
1869  throw KKException (errMsg);
1870  }
1871 
1872  kkint32 z;
1873  kkint32 numFeaturesAfterEncoding = 0;
1874  kkint32 numOfFeaturesSelected = selectedFeatures->NumOfFeatures ( );
1875 
1876  switch (svmParam->EncodingMethod())
1877  {
1879  for (z = 0; z < numOfFeaturesSelected; z++)
1880  {
1881  if ((type_table[(*selectedFeatures)[z]] == AttributeType::Nominal) ||
1882  (type_table[(*selectedFeatures)[z]] == AttributeType::Symbolic)
1883  )
1884  numFeaturesAfterEncoding += cardinality_table[(*selectedFeatures)[z]];
1885  else
1886  numFeaturesAfterEncoding ++;
1887  }
1888  break;
1889 
1892  default:
1893  //numFeaturesAfterEncoding = fileDesc->NumOfFields ( );
1894  numFeaturesAfterEncoding = selectedFeatures->NumOfFeatures ();
1895  break;
1896  }
1897 
1898  numFeaturesAfterEncoding++; // extra node for -1 index
1899 
1900  predictXSpaceWorstCase = numFeaturesAfterEncoding + 10;
1901 
1902  // We need to make sure that 'predictXSpace' is bigger than the worst possible case.
1903  // When doing the Binary Combo case we will assume that this can be all EncodedFeatures.
1904  // Since I am really only worried about the BinaryCombo case with Plankton data where there
1905  // is not encoded fields the number of attributes in the FileDesc should surface.
1906  if (predictXSpaceWorstCase < (fileDesc->NumOfFields () + 10))
1907  predictXSpaceWorstCase = fileDesc->NumOfFields () + 10;
1908 
1909 
1910  delete predictXSpace;
1911  predictXSpace = new svm_node[predictXSpaceWorstCase];
1912 } /* CalculatePredictXSpaceNeeded */
1913 
1914 
1915 
1916 
1917 kkint32 SVMModel::EncodeExample (FeatureVectorPtr example,
1918  svm_node* row
1919  )
1920 {
1921  if (!featureEncoder)
1922  {
1923  featureEncoder = new FeatureEncoder (fileDesc,
1924  NULL, /**< class1, set to NULL because we are dealing with all classes */
1925  NULL, /**< class2, "" "" "" "" */
1926  *selectedFeatures,
1927  svmParam->EncodingMethod (),
1928  svmParam->C_Param ()
1929  );
1930  }
1931 
1932  kkint32 xSpaceNodesNeeded = 0;
1933  featureEncoder->EncodeAExample (example, row, xSpaceNodesNeeded);
1934  return xSpaceNodesNeeded;
1935 } /* EncodeExample */
1936 
1937 
1938 
1939 void SVMModel::ConstructOneVsOneModel (FeatureVectorListPtr examples,
1940  svm_problem& prob,
1941  RunLog& log
1942  )
1943 {
1944  //**** Start of new compression replacement code.
1945  kkint32 totalxSpaceUsed = 0;
1946 
1947  numOfModels = 1;
1948  models = new ModelPtr [numOfModels];
1949  xSpaces = new XSpacePtr [numOfModels];
1950 
1951  {
1952  kkint32 x;
1953  for (x = 0; x < numOfModels; x++)
1954  {
1955  models[x] = NULL;
1956  xSpaces[x] = NULL;
1957  }
1958  }
1959 
1960  delete featureEncoder;
1961  featureEncoder = new FeatureEncoder (fileDesc,
1962  NULL, // class1, set to NULL because we are dealing with all classes
1963  NULL, // class2, "" "" "" ""
1964  *selectedFeatures,
1965  svmParam->EncodingMethod (),
1966  svmParam->C_Param ()
1967  );
1968 
1969  featureEncoder->EncodeIntoSparseMatrix (examples,
1970  assignments,
1971  xSpaces[0],
1972  totalxSpaceUsed,
1973  prob,
1974  log
1975  );
1976  xSpacesTotalAllocated += totalxSpaceUsed;
1977 
1978  //**** End of new compression replacement code.
1979  // train the model using the svm_problem built above
1980  double startTrainingTime = osGetSystemTimeUsed ();
1981  models[0] = SvmTrainModel (svmParam->Param (), prob);
1982  double endTrainingTime = osGetSystemTimeUsed ();
1983  trainingTime = endTrainingTime - startTrainingTime;
1984 
1985  // free the memory for the svm_problem
1986  delete[] prob.index; prob.index = NULL;
1987  free (prob.y); prob.y = NULL;
1988  if (xSpaces[0] != NULL)
1989  {
1990  // We can only free prob.x if xSpacs[0] was allocated otherwise we need
1991  // prob.x to located xSpace that will need to be deleted later.
1992  free (prob.x); prob.x = NULL;
1993  }
1994  delete[] prob.W; prob.W = NULL;
1995 } /* ConstructOneVsOneModel */
1996 
1997 
1998 
1999 
2000 void SVMModel::ConstructOneVsAllModel (FeatureVectorListPtr examples,
2001  svm_problem& prob,
2002  RunLog& log
2003  )
2004 {
2005  MLClassListPtr allClasses = examples->ExtractListOfClasses ();
2006 
2007  VectorShort assignmentNums = assignments.GetUniqueListOfAssignments ();
2008  numOfModels = (kkint32)assignmentNums.size ();
2009 
2010  models = new ModelPtr [numOfModels];
2011  xSpaces = new XSpacePtr [numOfModels];
2012  {
2013  kkint32 x;
2014  for (x = 0; x < numOfModels; x++)
2015  {
2016  models[x] = NULL;
2017  xSpaces[x] = NULL;
2018  }
2019  }
2020 
2021  featureEncoder = new FeatureEncoder (fileDesc,
2022  NULL, // class1, set to NULL because we are dealing with all classes
2023  NULL, // class2, "" "" "" ""
2024  *selectedFeatures,
2025  svmParam->EncodingMethod (),
2026  svmParam->C_Param ()
2027  );
2028 
2029  kkint32 modelIDX = 0;
2030  kkint32 assignmentIDX;
2031 
2032  trainingTime = 0;
2033  oneVsAllAssignment.erase (oneVsAllAssignment.begin (), oneVsAllAssignment.end ());
2034 
2035  {
2036  kkint32 modelIDX;
2037  delete oneVsAllClassAssignments;
2038  oneVsAllClassAssignments = new ClassAssignmentsPtr[numOfModels];
2039  for (modelIDX = 0; modelIDX < numOfModels; modelIDX++)
2040  oneVsAllClassAssignments[modelIDX] = NULL;
2041  }
2042 
2043  // build the models
2044  for (assignmentIDX = 0; assignmentIDX < numOfModels; assignmentIDX++)
2045  {
2046  kkint16 assignmentNum = assignmentNums[assignmentIDX];
2047  oneVsAllAssignment.push_back (assignmentNum);
2048 
2049  MLClassList classesThisAssignment = assignments.GetMLClasses (assignmentNum);
2050 
2051  BuildProblemOneVsAll (*examples,
2052  prob,
2053  xSpaces[modelIDX],
2054  classesThisAssignment,
2055  featureEncoder,
2056  *allClasses,
2057  oneVsAllClassAssignments [modelIDX],
2058  log
2059  );
2060 
2061  // train the model using the svm_problem built above
2062 
2063  double trainTimeStart = osGetSystemTimeUsed ();
2064  models[modelIDX] = SvmTrainModel (svmParam->Param (), prob);
2065  double trainTimeEnd = osGetSystemTimeUsed ();
2066  trainingTime += (trainTimeEnd - trainTimeStart);
2067 
2068  // free the memory for the svm_problem
2069  delete [] prob.index; prob.index = NULL;
2070  free (prob.y); prob.y = NULL;
2071  free (prob.x); prob.x = NULL;
2072  delete[] prob.W;
2073 
2074  modelIDX++;
2075  }
2076 
2077  delete allClasses;
2078 } /* ConstructOneVsAllModel */
2079 
2080 
2081 
2082 void SVMModel::ConstructBinaryCombosModel (FeatureVectorListPtr examples,
2083  RunLog& log
2084  )
2085 {
2086  log.Level (10) << "SVMModel::ConstructBinaryCombosModel" << endl;
2087 
2088  kkint32 maxXSpaceNeededPerExample = 0;
2089 
2090  numOfModels = (numOfClasses * (numOfClasses - 1)) / 2;
2091 
2092  models = new ModelPtr [numOfModels];
2093  xSpaces = new XSpacePtr [numOfModels];
2094  binaryParameters = new BinaryClassParmsPtr [numOfModels];
2095  binaryFeatureEncoders = new FeatureEncoderPtr [numOfModels];
2096 
2097  kkint32 modelIDX = 0;
2098  kkint32 class1IDX;
2099  kkint32 class2IDX;
2100  kkint32 x;
2101 
2102  for (x = 0; x < numOfModels; x++)
2103  {
2104  models [x] = NULL;
2105  binaryParameters [x] = NULL;
2106  binaryFeatureEncoders [x] = NULL;
2107  xSpaces [x] = NULL;
2108  }
2109 
2110  FeatureVectorListPtr srcExamples = examples;
2111  FeatureVectorListPtr compressedExamples = NULL;
2112 
2113  FeatureVectorListPtr* examplesByClass = BreakDownExamplesByClass (srcExamples);
2114 
2115 
2116  // NOTE: compression is performed in the BuildProblemBinaryCombos() function since
2117  // we can do the compression on just the example for those two classes - KNS
2118 
2119  // build a model for each possible 2-class combination
2120  for (class1IDX = 0; (class1IDX < (numOfClasses - 1)) && (!cancelFlag); class1IDX++)
2121  {
2122  MLClassPtr class1 = assignments.GetMLClassByIndex (class1IDX);
2123 
2124  for (class2IDX = class1IDX + 1; (class2IDX < numOfClasses) && (!cancelFlag); class2IDX++)
2125  {
2126  MLClassPtr class2 = assignments.GetMLClassByIndex (class2IDX);
2127 
2128  log.Level (20) << "ConstructBinaryCombosModel Class1[" << class1->Name () << "] Class2[" << class2->Name () << "]" << endl;
2129 
2130  struct svm_problem prob;
2131  //memset (&prob, 0, sizeof(svm_problem)); kak
2132 
2133  binaryParameters [modelIDX] = NULL;
2134  binaryFeatureEncoders [modelIDX] = NULL;
2135  xSpaces [modelIDX] = NULL;
2136 
2137  // build the svm_problem for the current combination
2138  BuildProblemBinaryCombos (examplesByClass[class1IDX],
2139  examplesByClass[class2IDX],
2140  binaryParameters [modelIDX],
2141  binaryFeatureEncoders [modelIDX],
2142  prob,
2143  xSpaces [modelIDX],
2144  class1,
2145  class2,
2146  log
2147  );
2148 
2149  maxXSpaceNeededPerExample = Max (maxXSpaceNeededPerExample, binaryFeatureEncoders [modelIDX]->XSpaceNeededPerExample ());
2150 
2151  // train the model
2152  double startTrainingTime = osGetSystemTimeUsed ();
2153  models[modelIDX] = SvmTrainModel (binaryParameters[modelIDX]->Param (), prob);
2154  double endTrainingTime = osGetSystemTimeUsed ();
2155  trainingTime += (endTrainingTime - startTrainingTime);
2156 
2157 
2158  /*
2159  {
2160  {
2161  ofstream xxx ("c:\\Temp\\testModel.txt");
2162  SvmSaveModel (xxx, models[modelIDX]);
2163  xxx.close ();
2164  }
2165 
2166  {
2167  FILE* xxx = fopen ("c:\\Temp\\testModel.txt", "r");
2168  SvmModel233 ** m = SvmLoadModel (xxx);
2169  fclose (xxx);
2170  }
2171  }
2172  */
2173 
2174  // log.Level (10) << "Support Vectors[" << models[modelIDX][0]->l << "]" << endl;
2175 
2176  //osWaitForEnter ();
2177 
2178  // free the memory for the svm_problem
2179  delete [] prob.index; prob.index = NULL;
2180  free (prob.y); prob.y = NULL;
2181  free (prob.x); prob.x = NULL;
2182  delete[] prob.W;
2183 
2184  modelIDX++;
2185  }
2186  }
2187 
2188  {
2189  kkint32 x;
2190  for (x = 0; x < (kkint32)assignments.size (); x++)
2191  delete examplesByClass[x];
2192  delete[] examplesByClass;
2193  examplesByClass = NULL;
2194  }
2195 
2196  predictXSpaceWorstCase = maxXSpaceNeededPerExample + 10;
2197 
2198  // We need to make sure that 'predictXSpace' is bigger than the worst possible case.
2199  // When doing the Binary Combo case we will assume that this can be all EncodedFeatures.
2200  // Since I am really only worried about the BinaryCombo case with Plankton data where there
2201  // is not encoded fields the number of attributes in the FileDesc should suffice.
2202  if (predictXSpaceWorstCase < (fileDesc->NumOfFields () + 10))
2203  predictXSpaceWorstCase = fileDesc->NumOfFields () + 10;
2204 
2205  delete predictXSpace; predictXSpace = NULL;
2206  predictXSpace = new svm_node[predictXSpaceWorstCase];
2207 
2208  delete compressedExamples;
2209 
2210  log.Level (10) << "SVMModel::ConstructBinaryCombosModel Done." << endl;
2211 } /* ConstructBinaryCombosModel */
2212 
2213 
2214 
2215 FeatureVectorListPtr* SVMModel::BreakDownExamplesByClass (FeatureVectorListPtr examples)
2216 {
2217  kkint32 x;
2218 
2219  FeatureVectorListPtr* examplesByClass = new FeatureVectorListPtr[numOfClasses];
2220  for (x = 0; x < numOfClasses; x++)
2221  examplesByClass[x] = new FeatureVectorList (fileDesc, false);
2222 
2223  MLClassPtr lastMLClass = NULL;
2224  kkint32 classIdx = 0;
2225 
2226  FeatureVectorList::iterator idx;
2227 
2228  for (idx = examples->begin (); idx != examples->end (); idx++)
2229  {
2230  FeatureVectorPtr example = *idx;
2231 
2232  if (lastMLClass != example->MLClass ())
2233  {
2234  lastMLClass = example->MLClass ();
2235  classIdx = assignments.GetNumForClass (lastMLClass);
2236  }
2237 
2238  examplesByClass[classIdx]->PushOnBack (example);
2239  }
2240 
2241  return examplesByClass;
2242 } /* BreakDownExamplesByClass */
2243 
2244 
2245 
2247 {
2249  return true;
2250  else
2251  return false;
2252 } /* NormalizeNominalAttributes */
2253 
2254 
2255 
2256 void SVMModel::SetSelectedFeatures (FeatureNumListConst& _selectedFeatures,
2257  RunLog& _log
2258  )
2259 {
2260  delete selectedFeatures;
2261  selectedFeatures = new FeatureNumList (_selectedFeatures);
2262  CalculatePredictXSpaceNeeded (_log);
2263 } /* SetSelectedFeatures */
2264 
2265 
2266 
2267 void SVMModel::SetSelectedFeatures (FeatureNumListConstPtr _selectedFeatures,
2268  RunLog& _log
2269  )
2270 {
2271  delete selectedFeatures;
2272  selectedFeatures = new FeatureNumList (*_selectedFeatures);
2273  CalculatePredictXSpaceNeeded (_log);
2274 } /* SetSelectedFeatures */
2275 
2276 
2277 
2278 
2280  double** crossProbTable, // two dimension matrix that needs to be classes.QueueSize () squared.
2281  RunLog& log
2282  )
2283 {
2284  if (classes.QueueSize () != crossClassProbTableSize)
2285  {
2286  // There Class List does not have the same number of entries as our 'CrossProbTable'
2287  log.Level (-1) << endl
2288  << "SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
2289  << " classes.QueueSize ()[" << classes.QueueSize () << "] != crossClassProbTableSize[" << crossClassProbTableSize << "]" << endl
2290  << endl;
2291  return;
2292  }
2293 
2294  kkint32* indexTable = new kkint32[classes.QueueSize ()];
2295  kkint32 x, y;
2296  for (x = 0; x < classes.QueueSize (); x++)
2297  {
2298  for (y = 0; y < classes.QueueSize (); y++)
2299  crossProbTable[x][y] = 0.0;
2300 
2301  indexTable[x] = assignments.GetNumForClass (classes.IdxToPtr (x));
2302  if (indexTable[x] < 0)
2303  {
2304  log.Level (-1) << endl << endl
2305  << "SVMModel::RetrieveCrossProbTable ***WARNING***" << endl
2306  << endl
2307  << " Class Index[" << x << "] Name[" << classes[x].Name () << "]" << endl
2308  << " will populate this index with zeros." << endl
2309  << endl;
2310  }
2311  }
2312 
2313  if (classes.QueueSize () != crossClassProbTableSize)
2314  {
2315  log.Level (-1) << endl
2316  << "SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
2317  << " 'classes.QueueSize () != crossClassProbTableSize'" << endl
2318  << endl;
2319  return;
2320  }
2321 
2322 
2323  // x,y = 'Callers' Class Indexes..
2324  // xIdx, yIdx = 'SVMNodel' Class Indexed.
2325  for (x = 0; x < classes.QueueSize (); x++)
2326  {
2327  kkint32 xIdx = indexTable[x];
2328  if (xIdx >= 0)
2329  {
2330  for (y = 0; y < classes.QueueSize (); y++)
2331  {
2332  kkint32 yIdx = indexTable[y];
2333  if (yIdx >= 0)
2334  {
2335  if ((x != xIdx) || (y != yIdx))
2336  {
2337  //kak I just added this check to see when this situation actually occurs.
2338  kkint32 zed = 111;
2339  }
2340  crossProbTable[x][y] = this->crossClassProbTable[xIdx][yIdx];
2341  }
2342  }
2343  }
2344  }
2345 
2346  delete[] indexTable; indexTable = NULL;
2347  return;
2348 } /* RetrieveCrossProbTable */
2349 
2350 
2351 
2352 
2353 
2354 void SVMModel::WriteXML (const KKStr& varName,
2355  ostream& o
2356  ) const
2357 {
2358  XmlTag startTag ("SVMModel", XmlTag::TagTypes::tagStart);
2359  if (!varName.Empty ())
2360  startTag.AddAtribute ("VarName", varName);
2361  startTag.WriteXML (o);
2362  o << endl;
2363 
2364  if (fileDesc)
2365  fileDesc->WriteXML ("FileDesc", o);
2366 
2367  {
2369 
2370  headerFields->Add ("RootFileName", rootFileName);
2371  headerFields->Add ("Time", osGetLocalDateTime ());
2372  headerFields->Add ("NumOfModels", numOfModels);
2373  if (selectedFeatures)
2374  headerFields->Add ("SelectedFeatures", selectedFeatures->ToString ());
2375 
2376  headerFields->Add ("TrainingTime", trainingTime);
2377  headerFields->Add ("Assignments", assignments.ToString ());
2378  headerFields->WriteXML ("HeaderFields", o);
2379  delete headerFields;
2380  headerFields = NULL;
2381  }
2382  svmParam->WriteXML ("svmParam", o);
2383 
2385  {
2386  // "<OneVsOne>"
2387  rootFileName.WriteXML ("RootFileName", o);
2388  if (models && (models[0]) && models[0][0])
2389  models[0][0]->WriteXML ("OneVsOneModel", o);
2390  }
2391 
2392  else if (svmParam->MachineType () == SVM_MachineType::OneVsAll)
2393  {
2394  // Not Supported
2395  }
2396 
2397  else if (svmParam->MachineType () == SVM_MachineType::BinaryCombos)
2398  {
2399  kkint32 modelsIDX = 0;
2400 
2401  for (modelsIDX = 0; modelsIDX < numOfModels; modelsIDX++)
2402  {
2403  BinaryClassParmsPtr binClassParms = binaryParameters[modelsIDX];
2404  KKStr binaryClassNames (256);
2405  binaryClassNames << modelsIDX << "\t" << binClassParms->Class1Name () << "\t" << binClassParms->Class2Name ();
2406  binaryClassNames.WriteXML ("BinaryCombo", o);
2407  KKStr binaryComboModelName = "BinaryComboModel_" + StrFormatInt (modelsIDX, "000");
2408  models[modelsIDX][0]->WriteXML (binaryComboModelName, o);
2409  }
2410  }
2411 
2412  XmlTag endTag ("SVMModel", XmlTag::TagTypes::tagEnd);
2413  endTag.WriteXML (o);
2414  o << endl;
2415 } /* WriteXML */
2416 
2417 
2418 
2419 
2420 
2422  XmlTagConstPtr tag,
2423  VolConstBool& cancelFlag,
2424  RunLog& log
2425  )
2426 {
2427  kkint32 numModeLoaded = 0;
2428  KKStr lastBinaryClass1Name = "";
2429  KKStr lastBinaryClass2Name = "";
2430  kkint32 lastModelIdx = -1;
2431 
2432  DeleteModels ();
2433  DeleteXSpaces ();
2434 
2435  delete binaryParameters; binaryParameters = NULL;
2436  delete models; models = NULL;
2437  delete binaryFeatureEncoders; binaryFeatureEncoders = NULL;
2438 
2439  numOfClasses = 0;
2440  numOfModels = 0;
2441 
2442  DateTime timeSaved;
2443 
2444  bool errorsFound = false;
2445  XmlTokenPtr t = NULL;
2446  while (!errorsFound)
2447  {
2448  delete t;
2449  t = s.GetNextToken (cancelFlag, log);
2450  if ((!t) || cancelFlag)
2451  break;
2452 
2454  {
2455  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
2456  if (!e)
2457  continue;
2458 
2459  const KKStr& varName = e->VarName ();
2460 
2461  if (varName.EqualIgnoreCase ("FileDesc") && (typeid (*e) == typeid (XmlElementFileDesc)))
2462  {
2463  fileDesc = dynamic_cast<XmlElementFileDescPtr> (e)->Value ();
2464  }
2465 
2466  else if (varName.EqualIgnoreCase ("HeaderFields") && (typeid (*e) == typeid (XmlElementKeyValuePairs)))
2467  {
2469  if (!hf)
2470  continue;
2471 
2472  for (auto idx: *hf->Value ())
2473  {
2474  if (idx.first.EqualIgnoreCase ("RootFileName"))
2475  rootFileName = idx.second;
2476 
2477  else if (idx.first.EqualIgnoreCase ("Time"))
2478  timeSaved = DateTime (idx.second);
2479 
2480  else if (idx.first.EqualIgnoreCase ("NumOfModels"))
2481  numOfModels = idx.second.ToInt32 ();
2482 
2483  else if (idx.first.EqualIgnoreCase ("SelectedFeatures"))
2484  {
2485  delete selectedFeatures;
2486  bool validFeatures = false;
2487  selectedFeatures = new FeatureNumList (idx.second, validFeatures);
2488  }
2489 
2490  else if (idx.first.EqualIgnoreCase ("TrainingTime"))
2491  trainingTime = idx.second.ToDouble ();
2492 
2493  else if (idx.first.EqualIgnoreCase ("Assignments") || idx.first.EqualIgnoreCase ("ClassAssignments"))
2494  assignments.ParseToString (idx.second, log);
2495 
2496  else
2497  {
2498  log.Level (-1) << endl
2499  << "SVMModel::ReadXM ***ERROR*** Unrecognized Header Field: " << idx.first << endl
2500  << endl;
2501  errorsFound = true;
2502  }
2503  }
2504 
2505  if (numOfModels < 1)
2506  {
2507  log.Level (-1) << endl
2508  << "SVMModel::ReadXM ***ERROR*** numOfModels: " << numOfModels << " Is invalid." << endl
2509  << endl;
2510  errorsFound = true;
2511  }
2512  else
2513  {
2514  DeleteModels ();
2515  AllocateModels ();
2516  binaryParameters = new BinaryClassParmsPtr [numOfModels];
2517  binaryFeatureEncoders = new FeatureEncoderPtr [numOfModels];
2518 
2519  for (kkint32 x = 0; x < numOfModels; x++)
2520  {
2521  binaryParameters [x] = NULL;
2522  binaryFeatureEncoders [x] = NULL;
2523  }
2524  }
2525  }
2526 
2527  else if (varName.EqualIgnoreCase ("SvmParam") && (typeid (*e) == typeid (XmlElementSVMparam)))
2528  {
2529  XmlElementSVMparamPtr xmlSvmParam = dynamic_cast<XmlElementSVMparamPtr> (e);
2530  if (xmlSvmParam)
2531  {
2532  delete svmParam;
2533  svmParam = xmlSvmParam->TakeOwnership ();
2534  }
2535  }
2536 
2537  else if (varName.EqualIgnoreCase ("RootFileName"))
2538  {
2539  rootFileName = e->ToKKStr ();
2540  }
2541 
2542  else if (varName.EqualIgnoreCase ("OneVsOneModel") && (typeid (*e) == typeid (XmlElementSvmModel233)))
2543  {
2544  XmlElementSvmModel233Ptr xmlElementModel = dynamic_cast<XmlElementSvmModel233Ptr> (e);
2545  SvmModel233* m = xmlElementModel->Value ();
2546  if (m)
2547  {
2548  if (!m->valid)
2549  {
2550  log.Level (-1) << endl
2551  << "SVMModel::ReadXM ***ERROR*** 'OneVsOneModel' is invalid." << endl
2552  << endl;
2553  }
2554  else
2555  {
2556  if (!models)
2557  {
2558  log.Level (-1) << endl
2559  << "SVMModel::ReadXM ***ERROR*** 'OneVsOneModel' models was not defined/allocated." << endl
2560  << endl;
2561  }
2562  else
2563  {
2564  models[0][0] = xmlElementModel->TakeOwnership ();
2565  ++numModeLoaded;
2566  }
2567  }
2568  }
2569  }
2570 
2571  else if (varName.EqualIgnoreCase ("BinaryCombo") && (typeid (*e) == typeid (XmlElementKKStr)))
2572  {
2573  KKStr s = *(dynamic_cast<XmlElementKKStrPtr> (e)->Value ());
2574  lastModelIdx = s.ExtractTokenInt ("\t");
2575  lastBinaryClass1Name = s.ExtractToken2 ("\t");
2576  lastBinaryClass2Name = s.ExtractToken2 ("\t");
2577  }
2578 
2579  else if (varName.StartsWith ("BinaryComboModel_") && (typeid (*e) == typeid (XmlElementSvmModel233)))
2580  {
2581  XmlElementSvmModel233Ptr xmlElementModel = dynamic_cast<XmlElementSvmModel233Ptr> (e);
2582  SvmModel233* m = xmlElementModel->Value ();
2583  if ((!m) || (!m->valid))
2584  {
2585  log.Level (-1) << endl
2586  << "SVMModel::ReadXM ***ERROR*** SvmModel233[" << varName << "] is invalid." << endl
2587  << endl;
2588  errorsFound = true;
2589  }
2590 
2591  else if (numModeLoaded >= numOfModels)
2592  {
2593  log.Level (-1) << endl
2594  << "SVMModel::ReadXM ***ERROR*** Number of models being loaded exceeds what was expected." << endl
2595  << endl;
2596  errorsFound = true;
2597  }
2598 
2599  else
2600  {
2601  MLClassPtr class1 = MLClass::CreateNewMLClass (lastBinaryClass1Name);
2602  MLClassPtr class2 = MLClass::CreateNewMLClass (lastBinaryClass2Name);
2603 
2604  log.Level (10) << "SVMModel::ReadXM Class1[" << lastBinaryClass1Name << "] Class2[" << lastBinaryClass2Name << "]" << endl;
2605 
2606  BinaryClassParmsPtr binClassParms = svmParam->GetParamtersToUseFor2ClassCombo (class1, class2);
2607  if (!binClassParms)
2608  {
2609  log.Level (-1) << endl
2610  << "SVMModel::ReadXM ***ERROR*** Binary Class Parms are missing for classes " << lastBinaryClass1Name << " and " << lastBinaryClass2Name << endl
2611  << endl;
2612  errorsFound = true;
2613  }
2614  else
2615  {
2616  models[numModeLoaded][0] = dynamic_cast<XmlElementSvmModel233Ptr> (e)->TakeOwnership ();
2617  binaryParameters[numModeLoaded] = binClassParms;
2618 
2619  binaryFeatureEncoders[numModeLoaded]
2620  = new FeatureEncoder (fileDesc,
2621  binClassParms->Class1 (),
2622  binClassParms->Class2 (),
2623  *(binClassParms->SelectedFeatures ()),
2624  svmParam->EncodingMethod (),
2625  binClassParms->C ()
2626  );
2627  ++numModeLoaded;
2628  }
2629  }
2630  }
2631  }
2632  }
2633  delete t;
2634  t = NULL;
2635 
2636  if (cancelFlag)
2637  errorsFound = true;
2638 
2639  if (assignments.size () < 2)
2640  {
2641  log.Level (-1) << endl
2642  << "SVMModel::ReadXM ***ERROR*** 'assignments is not properly defined." << endl
2643  << endl;
2644  errorsFound = true;
2645  }
2646 
2647  if (!fileDesc)
2648  {
2649  log.Level (-1) << endl
2650  << "SVMModel::ReadXM ***ERROR*** 'fileDesc' is not defined." << endl
2651  << endl;
2652  errorsFound = true;
2653  }
2654 
2655  if (!svmParam)
2656  {
2657  log.Level (-1) << endl
2658  << "SVMModel::ReadXM ***ERROR*** 'svmParam' is not defined." << endl
2659  << endl;
2660  errorsFound = true;
2661  }
2662 
2663  if (!errorsFound)
2664  {
2665  type_table = fileDesc->CreateAttributeTypeTable ( );
2666  cardinality_table = fileDesc->CreateCardinalityTable ( );
2667 
2668  numOfClasses = (kkint32)assignments.size ();
2669  BuildClassIdxTable ();
2670  BuildCrossClassProbTable ();
2671 
2672  CalculatePredictXSpaceNeeded (log);
2673 
2675  {
2676  delete featureEncoder;
2677  featureEncoder = new FeatureEncoder (fileDesc,
2678  NULL, // class1, set to NULL because we are dealing with all classes
2679  NULL, // class2, "" "" "" ""
2680  *selectedFeatures,
2681  svmParam->EncodingMethod (),
2682  svmParam->C_Param ()
2683  );
2684  }
2685  }
2686 
2687  validModel = !errorsFound;
2688 } /* ReadXML */
2689 
2690 
2691 XmlFactoryMacro(SVMModel)
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
kkint32 CodedNumOfFeatures() const
void Gamma_Param(double _gamma)
Definition: SVMparam.h:184
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
double probability
Definition: SVMModel.h:72
VectorShort GetUniqueListOfAssignments() const
kkint32 MemoryConsumedEstimated() const
Definition: SVMModel.cpp:437
void GetMaxIndex(T *vote, kkint32 voteLength, kkint32 &maxIndex1, kkint32 &maxIndex2)
Definition: SVMModel.cpp:39
void AddQueue(const FeatureVectorList &examplesToAdd)
Add the contents of &#39;examplesToAdd&#39; to the end of this list.
ClassAssignments * ClassAssignmentsPtr
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
void Gamma(double _gamma)
void SvmPredictRaw(SvmModel233 **submodel, const svm_node *unKnownData, double &label, double &dist)
Definition: SvmWrapper.cpp:684
SVM_SelectionMethod
Definition: SVMparam.h:34
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureNumListConstPtr SelectedFeatures() const
Definition: SVMparam.h:165
void svm_GetSupportVectorStatistics(const struct SvmModel233 *model, kkint32 &numSVs, kkint32 &totalNumSVs)
Extract Support Vector statistics .
Definition: svm.cpp:4776
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
Definition: SVMModel.cpp:1328
kkuint32 NumOfFields() const
Definition: FileDesc.h:197
MLClassPtr GetMLClass(kkint16 num) const
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
SVM_MachineType
Definition: SVMparam.h:26
virtual void CancelFlag(bool _cancelFlag)
Definition: SVMModel.cpp:515
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc) const
Definition: SVMparam.cpp:443
FeatureNumListConstPtr GetFeatureNums() const
Definition: SVMModel.cpp:701
SVMparam(const SVMparam &_svmParam)
Definition: SVMparam.cpp:75
MLClassListPtr ExtractListOfClasses() const
std::vector< ProbNamePair > FindWorstSupportVectors2(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
Definition: SVMModel.cpp:1745
kkint16 GetNumForClass(MLClassPtr mlClass) const
MLClassPtr Class2() const
BinaryClassParmsPtr GetParamtersToUseFor2ClassCombo(MLClassPtr class1, MLClassPtr class2)
Definition: SVMparam.cpp:394
const FileDescPtr FileDesc() const
FeatureNumList const FeatureNumListConst
MLClassPtr Class1() const
MLClassPtr Predict(FeatureVectorPtr example)
Definition: SVMModel.cpp:1131
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: svm.cpp:144
kkint32 l
Definition: svm.h:139
SVMModel()
Default constructor used by XmlElementSVMModel to create and load a new instance from a XML Stream...
Definition: SVMModel.cpp:169
void Add(const KKStr &key, kkint32 v)
Definition: XmlStream.cpp:1203
KKStr SupportVectorName(kkint32 svIDX)
Definition: svm.cpp:134
struct SvmModel233 * svm_train(const struct svm_problem *prob, const struct svm_parameter *param)
SVM_MachineType MachineType() const
Definition: SVMparam.h:151
SVM_EncodingMethod
Definition: SVMparam.h:46
SVM_EncodingMethod EncodingMethod() const
Definition: SVMparam.h:143
double svm_predictTwoClasses(const SvmModel233 *model, const svm_node *x, double &dist, kkint32 excludeSupportVectorIDX)
Definition: svm.cpp:4218
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc) const
Definition: SVMModel.cpp:708
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
VectorInt32 CreateCardinalityTable() const
Definition: FileDesc.cpp:430
FeatureVectorList(FileDescPtr _fileDesc, bool _owner)
Will create a new empty list of FeatureVector&#39;s.
double C_Param() const
Definition: SVMparam.h:137
MLClassList GetMLClasses(kkint16 num) const
Container class for FeatureVector derived objects.
kkint32 nr_class
Definition: svm.h:105
KKTHread * KKTHreadPtr
void Add(const KKStr &key, double v)
Definition: XmlStream.cpp:1222
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
void Add(const KKStr &key, const KKStr &v)
Definition: XmlStream.cpp:1193
struct svm_node * XSpacePtr
Definition: SVMModel.h:58
FeatureNumList * FeatureNumListPtr
XmlElement * XmlElementPtr
Definition: XmlStream.h:21
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
kkint32 NumOfFeatures() const
struct svm_node ** x
Definition: svm.h:52
virtual ~SVMModel()
Frees any memory allocated by, and owned by the SVMModel.
Definition: SVMModel.cpp:303
FeatureNumListConstPtr GetFeatureNums() const
Definition: SVMparam.cpp:436
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
double * W
Definition: svm.h:53
kkint32 MemoryConsumedEstimated() const
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
XSpacePtr EncodeAExample(FeatureVectorPtr example)
Converts a single example into the svm_problem format.
std::vector< KKStr > SupportVectorNames() const
Definition: SVMModel.cpp:1618
double DistanceFromDecisionBoundary(FeatureVectorPtr example, MLClassPtr class1, MLClassPtr class2)
Definition: SVMModel.cpp:728
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
double * y
Definition: svm.h:49
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void AddMLClass(MLClassPtr mlClass, kkint16 num, RunLog &log)
void EncodeIntoSparseMatrix(FeatureVectorListPtr src, ClassAssignments &assignments, XSpacePtr &xSpace, kkint32 &totalxSpaceUsed, struct svm_problem &prob, RunLog &log)
Compresses &#39;src&#39; examples, allocating new &#39;xSpace&#39; data structure.
MLClassPtr MLClass() const
Class that is example is assigned to.
struct SvmModel233 ** SvmTrainModel(const struct svm_parameter &param, struct svm_problem &subprob)
Definition: SvmWrapper.cpp:543
MLClassPtr GetMLClassByIndex(size_t idx)
This class encapsulates are the information necessary to build a SVMModel class.
Definition: SVMparam.h:74
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Will return the probabilities for all pairs of the classes listed in &#39;classes&#39;.
Definition: SVMModel.cpp:2279
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: SVMModel.cpp:2354
kkint32 MemoryConsumedEstimated() const
Definition: svm.cpp:70
void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
Will get the probabilities assigned to each class.
Definition: SVMModel.cpp:1354
MLClassPtr Class2() const
svm_parameter param
Definition: svm.h:137
void Predict(FeatureVectorPtr example, MLClassPtr knownClass, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &probOfKnownClass, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie)
Will predict the two most likely classes of &#39;example&#39;.
Definition: SVMModel.cpp:792
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
bool PairCompareOperator(ProbNamePair l, ProbNamePair r)
Definition: SVMModel.cpp:1568
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
kkint32 * index
Definition: svm.h:51
XmlElementKeyValuePairs * XmlElementKeyValuePairsPtr
Definition: XmlStream.h:605
const svm_parameter & Param() const
KKStr ToString() const
Returns comma delimited list of all features selected; will make use of range specification.
SVM_SelectionMethod SelectionMethod() const
Definition: SVMparam.h:169
double osGetSystemTimeUsed()
Returns the number of CPU seconds used by current process.
FeatureEncoder * FeatureEncoderPtr
Definition: SVMModel.h:37
AttributeTypeVector CreateAttributeTypeTable() const
Definition: FileDesc.cpp:419
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
void SvmDestroyModel(struct SvmModel233 **subModel)
Definition: SvmWrapper.cpp:742
FeatureNumListConst * FeatureNumListConstPtr
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
FeatureEncoder(FileDescPtr _fileDesc, MLClassPtr _class1, MLClassPtr _class2, const FeatureNumList &_selectedFeatures, SVM_EncodingMethod _encodingMethod, double _c_Param)
Constructs a Feature Encoder object.
SVMModel(const SVMparam &_svmParam, FeatureVectorList &_examples, ClassAssignments &_assignments, FileDescPtr _fileDesc, RunLog &_log)
Constructor that will create a svmlib training model using the features and classes for training purp...
Definition: SVMModel.cpp:203
void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
Returns the distance from the decision border of the SVM.
Definition: SVMModel.cpp:921
std::vector< short > VectorShort
Definition: KKBaseTypes.h:142
void EncodeAExample(FeatureVectorPtr example, svm_node *xSpace, kkint32 &xSpaceUsed)
Converts a single example into the svm_problem format.
virtual TokenTypes TokenType()=0
FileDescPtr Value() const
Definition: FileDesc.cpp:947
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: SVMModel.cpp:2421
double gamma
Definition: svm.h:79
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc, MLClassPtr class1, MLClassPtr class2) const
Definition: SVMModel.cpp:715
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
double AdjProb(double prob)
Definition: SVMModel.cpp:105
Maintains a list of MLClass instances.
Definition: MLClass.h:233
std::vector< ProbNamePair > FindWorstSupportVectors(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
Definition: SVMModel.cpp:1653
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc, MLClassPtr class1, MLClassPtr class2) const
Definition: SVMparam.cpp:453
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: SVMparam.cpp:769
const svm_parameter & Param() const
Definition: SVMparam.h:159
XmlElementKeyValuePairs()
Used to construct an instance that will be written out to a XML file.
Definition: XmlStream.cpp:1134
virtual bool NormalizeNominalAttributes()
Definition: SVMModel.cpp:2246
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: FileDesc.cpp:875
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
kkint32 MemoryConsumedEstimated() const
void Add(const KKStr &key, const KKB::DateTime &v)
Definition: XmlStream.cpp:1243
kkint32 NumOfSupportVectors() const
Definition: SVMModel.cpp:1304
svm_problem * svm_BuildProbFromTwoClassModel(const SvmModel233 *model, kkint32 excludeSupportVectorIDX)
Definition: svm.cpp:4356
void WriteXML(const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1252
MLClassPtr Class1() const
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
Definition: SVMModel.cpp:1576
XmlElementFileDesc * XmlElementFileDescPtr
Definition: FileDesc.h:337
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163