ModErn Text Analysis
META Enumerates Textual Applications
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
meta::classify::confusion_matrix Class Reference

Allows interpretation of classification errors. More...

#include <confusion_matrix.h>

Public Types

typedef 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. More...
 

Public Member Functions

 confusion_matrix ()
 Creates an empty confusion matrix.
 
void add (const class_label &predicted, const class_label &actual, size_t times=1)
 
void print_stats (std::ostream &out=std::cout) const
 Prints this matrix's statistics to out. More...
 
void print (std::ostream &out=std::cout) const
 Prints this matrix to out. More...
 
void print_result_pairs (std::ostream &out=std::cout) const
 Prints (predicted, actual) pairs for all judgements. More...
 
const prediction_countspredictions () const
 
double accuracy () const
 
confusion_matrix operator+ (const confusion_matrix &other) const
 operator+ for confusion matrices. More...
 
confusion_matrixoperator+= (const confusion_matrix &other)
 operator+= for confusion matrices. More...
 

Static Public Member Functions

static size_t string_pair_hash (const std::pair< std::string, std::string > &strPair)
 Implements a hash function for a pair of strings. More...
 
static bool mcnemar_significant (const confusion_matrix &a, const confusion_matrix &b)
 

Private Member Functions

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. More...
 

Private Attributes

prediction_counts predictions_
 maps predicted class to actual class frequencies
 
std::set< class_label > classes_
 Keeps track of the number of classes. More...
 
std::unordered_map< class_label, size_t > counts_
 how many times each class was predicted
 
size_t total_
 total number of classification attempts
 

Detailed Description

Allows interpretation of classification errors.

Member Typedef Documentation

typedef std::unordered_map<std::pair<class_label, class_label>, size_t, decltype(&confusion_matrix::string_pair_hash)> meta::classify::confusion_matrix::prediction_counts

typedef for predicted class assignments to counts.

Member Function Documentation

void meta::classify::confusion_matrix::add ( const class_label &  predicted,
const class_label &  actual,
size_t  times = 1 
)
Parameters
predictedThe predicted class label
actualThe actual class label
timesThe number of times this prediction was made
void meta::classify::confusion_matrix::print_stats ( std::ostream &  out = std::cout) const

Prints this matrix's statistics to out.

Parameters
outThe stream to write to (defaults to std::cout)
void meta::classify::confusion_matrix::print ( std::ostream &  out = std::cout) const

Prints this matrix to out.

Parameters
outThe stream to write to (defaults to std::cout)
void meta::classify::confusion_matrix::print_result_pairs ( std::ostream &  out = std::cout) const

Prints (predicted, actual) pairs for all judgements.

Parameters
outThe stream to write to (defaults to std::cout)
size_t meta::classify::confusion_matrix::string_pair_hash ( const std::pair< std::string, std::string > &  strPair)
static

Implements a hash function for a pair of strings.

Parameters
str_pairThe pair of strings
Returns
the hash
const confusion_matrix::prediction_counts & meta::classify::confusion_matrix::predictions ( ) const
Returns
all the predictions from this confusion_matrix.
double meta::classify::confusion_matrix::accuracy ( ) const
Returns
the accuracy for this confusion matrix
confusion_matrix meta::classify::confusion_matrix::operator+ ( const confusion_matrix other) const

operator+ for confusion matrices.

All counts are agglomerated for all predictions.

Parameters
other
Returns
a confusion_matrix containing all predictions of the parameters
confusion_matrix & meta::classify::confusion_matrix::operator+= ( const confusion_matrix other)

operator+= for confusion matrices.

All counts are agglomerated for all predictions.

Parameters
other
Returns
a confusion_matrix containing all predictions of the parameters
bool meta::classify::confusion_matrix::mcnemar_significant ( const confusion_matrix a,
const confusion_matrix b 
)
static
Parameters
aThe first matrix to compare
bThe second matrix to compare
Returns
whether results between two confusion matrices are statistically significant according to McNemar's test with Yates' correction for continuity (alpha = .05)
void meta::classify::confusion_matrix::print_class_stats ( std::ostream &  out,
const class_label &  label,
double &  prec,
double &  rec,
double &  f1,
size_t  width 
) const
private

Prints precision, recall, and F1 for each class and as a whole.

Parameters
outThe stream to print to
labelThe current class label to get statistics for
precThe precision for this class
recThe recall for this class
f1The F1 score for this class

Member Data Documentation

std::set<class_label> meta::classify::confusion_matrix::classes_
private

Keeps track of the number of classes.

We use a std::set here so the class labels are sorted alphabetically.


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