ModErn Text Analysis
META Enumerates Textual Applications
confusion_matrix.h
Go to the documentation of this file.
1 
10 #ifndef META_CONFUSION_MATRIX_H_
11 #define META_CONFUSION_MATRIX_H_
12 
13 #include <set>
14 #include "index/forward_index.h"
15 #include "meta.h"
16 
17 namespace meta
18 {
19 namespace classify
20 {
21 
26 {
27  public:
32 
38  void add(const class_label& predicted, const class_label& actual,
39  size_t times = 1);
40 
46  void print_stats(std::ostream& out = std::cout) const;
47 
53  void print(std::ostream& out = std::cout) const;
54 
60  void print_result_pairs(std::ostream& out = std::cout) const;
61 
67  static size_t
68  string_pair_hash(const std::pair<std::string, std::string>& strPair);
69 
70  // note: the following *cannot* be converted to a using declaration
71  // without causing in internal compiler error (segmentation fault) in
72  // GCC 4.8.2
74  typedef std::unordered_map<std::pair<class_label, class_label>, size_t,
77 
81  const prediction_counts& predictions() const;
82 
86  double accuracy() const;
87 
94  confusion_matrix operator+(const confusion_matrix& other) const;
95 
103 
111  static bool mcnemar_significant(const confusion_matrix& a,
112  const confusion_matrix& b);
113 
114  private:
123  void print_class_stats(std::ostream& out, const class_label& label,
124  double& prec, double& rec, double& f1,
125  size_t width) const;
126 
129 
134  std::set<class_label> classes_;
135 
137  std::unordered_map<class_label, size_t> counts_;
138 
140  size_t total_;
141 };
142 }
143 }
144 
145 #endif
void print(std::ostream &out=std::cout) const
Prints this matrix to out.
Definition: confusion_matrix.cpp:43
Contains top-level namespace documentation for the META toolkit.
confusion_matrix operator+(const confusion_matrix &other) const
operator+ for confusion matrices.
Definition: confusion_matrix.cpp:208
class_label label(const std::string &text)
Extracts a class_label from a string in libsvm format.
Definition: libsvm_parser.cpp:18
std::set< class_label > classes_
Keeps track of the number of classes.
Definition: confusion_matrix.h:134
void print_stats(std::ostream &out=std::cout) const
Prints this matrix's statistics to out.
Definition: confusion_matrix.cpp:128
Allows interpretation of classification errors.
Definition: confusion_matrix.h:25
static size_t string_pair_hash(const std::pair< std::string, std::string > &strPair)
Implements a hash function for a pair of strings.
Definition: confusion_matrix.cpp:197
confusion_matrix()
Creates an empty confusion matrix.
Definition: confusion_matrix.cpp:19
double accuracy() const
Definition: confusion_matrix.cpp:189
std::unordered_map< std::pair< class_label, class_label >, size_t, decltype(&confusion_matrix::string_pair_hash)> prediction_counts
typedef for predicted class assignments to counts.
Definition: confusion_matrix.h:76
void print_result_pairs(std::ostream &out=std::cout) const
Prints (predicted, actual) pairs for all judgements.
Definition: confusion_matrix.cpp:36
prediction_counts predictions_
maps predicted class to actual class frequencies
Definition: confusion_matrix.h:128
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
size_t total_
total number of classification attempts
Definition: confusion_matrix.h:140
const prediction_counts & predictions() const
Definition: confusion_matrix.cpp:203
void print_class_stats(std::ostream &out, const class_label &label, double &prec, double &rec, double &f1, size_t width) const
Prints precision, recall, and F1 for each class and as a whole.
Definition: confusion_matrix.cpp:100
static bool mcnemar_significant(const confusion_matrix &a, const confusion_matrix &b)
Definition: confusion_matrix.cpp:225
std::unordered_map< class_label, size_t > counts_
how many times each class was predicted
Definition: confusion_matrix.h:137
void add(const class_label &predicted, const class_label &actual, size_t times=1)
Definition: confusion_matrix.cpp:25
confusion_matrix & operator+=(const confusion_matrix &other)
operator+= for confusion matrices.
Definition: confusion_matrix.cpp:219