KSquare Utilities
ClassProb.h
Go to the documentation of this file.
1 #if !defined(_CLASSPROB_)
2 #define _CLASSPROB_
3 /**
4  *@class KKMLL::ClassProb
5  *@brief Used to record probability for a specified class; and a list of classes.
6  *@author Kurt Kramer
7  */
8 
9 #include "XmlStream.h"
10 #include "KKBaseTypes.h"
11 #include "KKQueue.h"
12 #include "KKStr.h"
13 
14 
15 namespace KKMLL
16 {
17  #if !defined(_MLCLASS_)
18  class MLClass;
19  typedef MLClass* MLClassPtr;
20  class MLClassList;
21  typedef MLClassList* MLClassListPtr;
22  #endif
23 
24 
25  class ClassProb
26  {
27  public:
28  ClassProb (MLClassPtr _classLabel,
29  double _probability,
30  float _votes
31  );
32 
33  ClassProb (const ClassProb& _pair);
34 
35  MLClassPtr classLabel;
36  double probability;
37  float votes;
38  };
39  typedef ClassProb* ClassProbPtr;
40 
41 #define _ClassProb_Defined_
42 
43 
45  {
46  public:
48 
49  ClassProbList ();
50  ClassProbList (bool owner);
51  ClassProbList (const ClassProbList& pairList);
52 
54 
55  void SortByClassName ();
56  void SortByProbability (bool highToLow = true);
57  void SortByVotes (bool highToLow = true);
58 
59  const ClassProbPtr LookUp (MLClassPtr targetClass) const;
60 
61  virtual void DeleteEntry (ClassProbPtr cp);
62  virtual void DeleteEntry (kkuint32 idx);
63  virtual void PushOnBack (ClassProbPtr cp);
64  virtual void PushOnFront (ClassProbPtr cp);
65 
66 
67  /**
68  *@brief Adds the contents of 'otherPredictions' to this list.
69  *@details there os not assumption that this list or 'otherPredictions' sum up to "1.0".
70  */
71  void AddIn (const ClassProbListPtr otherPredictions);
72 
73 
74  /**
75  *@brief Returns the position that 'targetClass' has in the order; good time to use would be after sorting by probability.
76  */
77  kkint32 LookUpPlace (MLClassPtr targetClass) const;
78 
79 
80  /**
81  *@brief Adds the Prediction in 'cp' into this list.
82  *@details If the class indicated by 'cp->classLabel' already exist in this
83  * list will then add to existing entry otherwise will create a new entry for
84  * the class.
85  */
86  void MergeIn (const ClassProbPtr cp);
87 
88  /**
89  *@brief Adds the prediction of 'target' with 'probability' into this list.
90  *@details If the class indicated by 'target' already exist in this
91  * list will then add to existing entry otherwise will create a new entry for
92  * the class.
93  */
94  void MergeIn (MLClassPtr target,
95  double probability,
96  float votes
97  );
98 
99  /**
100  *@brief Merges in the predictions in 'subPredictions' by replacing the entry in our list with
101  * label 'target' with contents of 'subPredictions'.
102  *@details It is assumed that the sum of probabilities of 'subPredictions' is equal to 1.0.
103  */
104  void MergeIn (MLClassPtr target,
105  const ClassProbListPtr subPredictions
106  );
107 
108 
109  /**
110  **@brief Will normalize the list of predictions such that the total probability will equal 1.0 and
111  * total votes will = (N)(N-1)/2 where N = number of predictions.
112  */
113  void NormalizeToOne ();
114 
115  void ReadXML (XmlStream& s,
116  XmlTagConstPtr tag,
117  VolConstBool& cancelFlag,
118  RunLog& log
119  );
120 
121  void WriteXML (const KKStr& varName,
122  std::ostream& o
123  ) const;
124 
125 
126 
127  static
128  ClassProbListPtr CreateFromXMLStream (std::istream& i);
129 
130  private:
131  typedef std::map<MLClassPtr,ClassProbPtr> MLClassIndexType;
132  typedef std::pair<MLClassPtr,ClassProbPtr> MLClassIndexPair;
133 
134  static bool CompairByClassName (const ClassProbPtr left, const ClassProbPtr right);
135 
136  class ProbabilityComparer;
137  class VotesComparer;
138 
139  MLClassIndexType classIndex;
140  };
142 
143 #define _ClassProbList_Defined_
144 
145 
148 } /* namespace KKMLL */
149 
150 #endif
void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: ClassProb.cpp:423
ClassProbList * ClassProbListPtr
Definition: ClassProb.h:47
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 MemoryConsumedEstimated() const
Definition: ClassProb.cpp:68
void MergeIn(const ClassProbPtr cp)
Adds the Prediction in &#39;cp&#39; into this list.
Definition: ClassProb.cpp:268
virtual void PushOnBack(ClassProbPtr cp)
Definition: ClassProb.cpp:212
kkint32 LookUpPlace(MLClassPtr targetClass) const
Returns the position that &#39;targetClass&#39; has in the order; good time to use would be after sorting by ...
Definition: ClassProb.cpp:174
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: ClassProb.cpp:400
virtual void PushOnFront(ClassProbPtr cp)
Definition: ClassProb.cpp:229
const ClassProbPtr LookUp(MLClassPtr targetClass) const
Definition: ClassProb.cpp:160
void SortByProbability(bool highToLow=true)
Definition: ClassProb.cpp:143
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
void MergeIn(MLClassPtr target, double probability, float votes)
Adds the prediction of &#39;target&#39; with &#39;probability&#39; into this list.
Definition: ClassProb.cpp:276
XmlElementTemplate< ClassProbList > XmlElementClassProbList
Definition: ClassProb.h:146
virtual void DeleteEntry(kkuint32 idx)
Definition: ClassProb.cpp:198
ClassProbList(bool owner)
Definition: ClassProb.cpp:48
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassProbList * ClassProbListPtr
Definition: Classifier2.h:30
double probability
Definition: ClassProb.h:36
ClassProbList(const ClassProbList &pairList)
Definition: ClassProb.cpp:55
void AddIn(const ClassProbListPtr otherPredictions)
Adds the contents of &#39;otherPredictions&#39; to this list.
Definition: ClassProb.cpp:246
void SortByVotes(bool highToLow=true)
Definition: ClassProb.cpp:151
virtual void DeleteEntry(ClassProbPtr cp)
Definition: ClassProb.cpp:187
void MergeIn(MLClassPtr target, const ClassProbListPtr subPredictions)
Merges in the predictions in &#39;subPredictions&#39; by replacing the entry in our list with label &#39;target&#39; ...
Definition: ClassProb.cpp:301
ClassProb(const ClassProb &_pair)
Definition: ClassProb.cpp:33
static ClassProbListPtr CreateFromXMLStream(std::istream &i)
Definition: ClassProb.cpp:362
void NormalizeToOne()
Will normalize the list of predictions such that the total probability will equal 1...
Definition: ClassProb.cpp:336
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)
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Definition: ClassProb.cpp:22
MLClassPtr classLabel
Definition: ClassProb.h:35
ClassProb * ClassProbPtr
Definition: Classifier2.h:28
XmlElementClassProbList * XmlElementClassProbListPtr
Definition: ClassProb.h:147
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163