KSquare Utilities
KKMLL::ClassAssignments Class Reference

Binds MLClass objects to the appropriate number that the Learning Algorithm expects. More...

#include <ClassAssignments.h>

+ Inheritance diagram for KKMLL::ClassAssignments:

Public Member Functions

 ClassAssignments ()
 
 ClassAssignments (const MLClassList &classes)
 Creates assignment for all classes in 'classes. More...
 
void AddMLClass (MLClassPtr mlClass, kkint16 num, RunLog &log)
 
MLClassPtr GetMLClass (kkint16 num) const
 
MLClassPtr GetMLClassByIndex (size_t idx)
 
MLClassList GetMLClasses (kkint16 num) const
 
kkint16 GetNumForClass (MLClassPtr mlClass) const
 
VectorShort GetUniqueListOfAssignments () const
 
kkint32 MemoryConsumedEstimated () const
 
void ParseToString (const KKStr &_toString, RunLog &_log)
 Loads class assignments from string that was originally generated by ToString. More...
 
void Save (const KKStr &fileName, bool &successful)
 
KKStr ToString () const
 

Detailed Description

Binds MLClass objects to the appropriate number that the Learning Algorithm expects.

Author
Kurt Kramer

This class will bind MLClass objects to the appropriate number that the learning algorithm knows for this class. When originally implementing this system, this info was kept in the MLClass object. This would cause problems when doing multilevel classification. The same MLClass might be known to the L.A. as one number in one level and another number in a different level. This caused us to have to maintain a separate instantiation of these classes on the SvmModel objects.

It is sub-classed from multimap<kkint16, MLClassPtr>. This allows for fast lookup by number. There is also a secondary index by MLClass. This allows us to do a fast lookup by class.

Definition at line 36 of file ClassAssignments.h.

Constructor & Destructor Documentation

ClassAssignments::ClassAssignments ( )

Definition at line 25 of file ClassAssignments.cpp.

References ClassAssignments().

Referenced by ClassAssignments(), KKMLL::ModelOldSVM::ReadXML(), and KKMLL::SVMModel::SVMModel().

25  :
26  multimap<kkint16, MLClassPtr> ()
27 {
28 }
ClassAssignments::ClassAssignments ( const MLClassList classes)

Creates assignment for all classes in 'classes.

Constructs a list assigning a number to each class sequentially from 'classes'. Use this constructor if each class is treated uniquely by the learning algorithm.

Parameters
[in]classes,Listof classes to make assignments for.
[in,out]_log,Runlog to write messages to

Definition at line 32 of file ClassAssignments.cpp.

References ClassAssignments().

Referenced by ClassAssignments(), and KKMLL::ModelOldSVM::TrainModel().

32  :
33  multimap<kkint16, MLClassPtr> ()
34 {
35  kkint32 x = 0;
36  for (MLClassList::const_iterator idx = classes.begin (); idx != classes.end (); idx++)
37  {
38  insert (pair<kkint16, MLClassPtr> (x, *idx));
39  classLookUp.insert (pair<MLClassPtr, kkint16> (*idx, x));
40  x++;
41  }
42 }
__int32 kkint32
Definition: KKBaseTypes.h:88
std::vector< MLClass * >::const_iterator const_iterator
Definition: KKQueue.h:89

Member Function Documentation

void ClassAssignments::AddMLClass ( MLClassPtr  mlClass,
kkint16  num,
RunLog log 
)

Definition at line 56 of file ClassAssignments.cpp.

Referenced by ParseToString().

60 {
61  ClassLookUpIterator idx;
62  idx = classLookUp.find (mlClass);
63  if (idx != classLookUp.end ())
64  {
65  log.Level (-1) << endl << endl
66  << "ClassAssignments::AddMLClass ***ERROR*** Duplicate Class Being Added[" << mlClass->Name () << "]." << endl
67  << " Num Found [" << idx->second << "]" << endl
68  << " New Num Being Added [" << num << endl
69  << endl;
70  return;
71  }
72 
73  insert (pair<kkint16, MLClassPtr> (num, mlClass));
74  classLookUp.insert (pair<MLClassPtr, kkint16> (mlClass, num));
75 } /* AddMLClass */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
MLClassPtr ClassAssignments::GetMLClass ( kkint16  num) const

Definition at line 81 of file ClassAssignments.cpp.

Referenced by KKMLL::SVMModel::Predict(), KKMLL::SVMModel::PredictRaw(), KKMLL::SVMModel::ProbabilitiesByClass(), and KKMLL::SVMparam::ProbClassPairsInitialize().

82 {
83  multimap<kkint16, MLClassPtr>::const_iterator p;
84  p = find (num);
85  if (p == end ())
86  return NULL;
87  return p->second;
88 } /* GetMLClass */
MLClassPtr ClassAssignments::GetMLClassByIndex ( size_t  idx)

Definition at line 144 of file ClassAssignments.cpp.

145 {
146  if ((idx < 0) || (idx >= (size_t)size ()))
147  {
148  cerr << endl
149  << endl
150  << "ClassAssignments::GetMLClassByIndex *** ERROR ***" << endl
151  << " idx[" << idx << "] is out of range." << endl
152  << endl;
153  return NULL;
154  }
155 
156  iterator i = begin ();
157  for (size_t x = 0; x < idx; x++)
158  i++;
159 
160  return i->second;
161 } /* GetMLClassByIndex */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
MLClassList ClassAssignments::GetMLClasses ( kkint16  num) const

Definition at line 94 of file ClassAssignments.cpp.

95 {
96  ClassAssignments::const_iterator idx;
97 
98  MLClassList results;
99  for (idx = begin (); idx != end (); idx++)
100  {
101  if (idx->first == num)
102  results.PushOnBack (idx->second);
103  }
104 
105  return results;
106 } /* GetMLClasses */
virtual void PushOnBack(MLClassPtr mlClass)
Definition: MLClass.cpp:798
Maintains a list of MLClass instances.
Definition: MLClass.h:233
kkint16 ClassAssignments::GetNumForClass ( MLClassPtr  mlClass) const

Definition at line 167 of file ClassAssignments.cpp.

Referenced by KKMLL::FeatureEncoder::EncodeIntoSparseMatrix(), and KKMLL::SVMModel::Predict().

168 {
169  ClassLookUp::const_iterator idx;
170  idx = classLookUp.find (mlClass);
171  if (idx == classLookUp.end ())
172  return -1;
173  else
174  return idx->second;
175 } /* GetNumForClass */
VectorShort ClassAssignments::GetUniqueListOfAssignments ( ) const

Definition at line 111 of file ClassAssignments.cpp.

112 {
113  VectorShort nums;
114 
115  ClassAssignments::const_iterator idx;
116 
117  for (idx = begin (); idx != end (); idx++)
118  {
119  nums.push_back (idx->first);
120  }
121 
122  sort (nums.begin (), nums.end ());
123 
124  VectorShort results;
125 
126  kkint16 lastNum = -999;
127  VectorShort::const_iterator idx2;
128  for (idx2 = nums.begin (); idx2 != nums.end (); idx2++)
129  {
130  if (*idx2 != lastNum)
131  {
132  lastNum = *idx2;
133  results.push_back (lastNum);
134  }
135  }
136 
137  return results;
138 } /* GetUniqueListOfAssignments */
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
std::vector< short > VectorShort
Definition: KKBaseTypes.h:142
kkint32 ClassAssignments::MemoryConsumedEstimated ( ) const

Definition at line 46 of file ClassAssignments.cpp.

Referenced by KKMLL::ModelOldSVM::MemoryConsumedEstimated(), and KKMLL::SVMModel::MemoryConsumedEstimated().

47 {
48  kkint32 memoryConsumedEstimated = sizeof (ClassAssignments)
49  + (classLookUp.size () * (sizeof (MLClassPtr) + sizeof (kkint16)));
50  return memoryConsumedEstimated;
51 }
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
MLClass * MLClassPtr
Definition: MLClass.h:46
__int32 kkint32
Definition: KKBaseTypes.h:88
void ClassAssignments::ParseToString ( const KKStr _toString,
RunLog _log 
)

Loads class assignments from string that was originally generated by ToString.

Parameters
[in]_toString,KKStrcontaining class assignments info, will expect to be formated the way ToString() creates them.

Definition at line 222 of file ClassAssignments.cpp.

References AddMLClass(), KKB::KKStr::Concat(), KKMLL::MLClass::CreateNewMLClass(), KKB::KKStr::Empty(), KKB::KKStr::ExtractToken(), KKB::KKStr::ExtractTokenInt(), and KKB::KKStr::KKStr().

225 {
226  erase (begin (), end ());
227 
228  KKStr toString (_toString);
229 
230  // Remove Class count from string
231  toString.ExtractTokenInt ("\t\n\r");
232 
233  while (!toString.Empty ())
234  {
235  kkint32 assignmentNum = toString.ExtractTokenInt ("\t\n\r");
236  KKStr className = toString.ExtractToken ("\t\n\r");
237 
238  if (!className.Empty ())
239  {
240  MLClassPtr mlClass = MLClass::CreateNewMLClass (className);
241  AddMLClass (mlClass, assignmentNum, _log);
242  }
243  }
244 } /* ParseToString */
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
bool Empty() const
Definition: KKStr.h:241
void AddMLClass(MLClassPtr mlClass, kkint16 num, RunLog &log)
static MLClassPtr CreateNewMLClass(const KKStr &_name, kkint32 _classId=-1)
Static method used to create a new instance of a MLClass object.
Definition: MLClass.cpp:100
void ClassAssignments::Save ( const KKStr fileName,
bool &  successful 
)

Definition at line 180 of file ClassAssignments.cpp.

183 {
184  ofstream outFile (fileName.Str ());
185 
186  for (iterator idx = begin (); idx != end (); idx++)
187  {
188  outFile << idx->second->Name () << "\t" << idx->first << endl;
189  }
190 
191  successful = true;
192  return;
193 }
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
KKStr ClassAssignments::ToString ( ) const

Definition at line 198 of file ClassAssignments.cpp.

References KKB::KKStr::Concat().

Referenced by KKMLL::ModelOldSVM::WriteXML(), and KKMLL::SVMModel::WriteXML().

199 {
200  KKStr result ((kkint32)(size () * 20));
201 
202  result << kkint32 (size ());
203 
204  ClassAssignments::const_iterator idx;
205  for (idx = begin (); idx != end (); idx++)
206  {
207  result << "\t" << idx->first << "\t" << idx->second->Name ();
208  }
209 
210  return result;
211 } /* ToString */
__int32 kkint32
Definition: KKBaseTypes.h:88

The documentation for this class was generated from the following files: