Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
1 /******************************************************************************
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnConfusionMatrix.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
13 #pragma once
15 #include <vector>
16 #include <iostream>
17 #include <string>
19 #include "drwnDataset.h"
20 #include "drwnClassifier.h"
22 // drwnConfusionMatrix ------------------------------------------------------
31  public:
32  static std::string COL_SEP;
33  static std::string ROW_BEGIN;
34  static std::string ROW_END;
36  protected:
37  std::vector<std::vector<unsigned> > _matrix;
39  public:
41  drwnConfusionMatrix(int n);
43  drwnConfusionMatrix(int n, int m);
44  virtual ~drwnConfusionMatrix();
47  int numRows() const;
49  int numCols() const;
52  void clear();
54  void accumulate(int actual, int predicted);
56  void accumulate(const std::vector<int>& actual,
57  const std::vector<int>& predicted);
59  void accumulate(const drwnClassifierDataset& dataset,
60  drwnClassifier const *classifier);
62  void accumulate(const drwnConfusionMatrix& confusion);
65  void printCounts(std::ostream &os = std::cout,
66  const char *header = NULL) const;
68  void printRowNormalized(std::ostream &os = std::cout,
69  const char *header = NULL) const;
71  void printColNormalized(std::ostream &os = std::cout,
72  const char *header = NULL) const;
74  void printNormalized(std::ostream &os = std::cout,
75  const char *header = NULL) const;
77  void printPrecisionRecall(std::ostream &os = std::cout,
78  const char *header = NULL) const;
80  void printF1Score(std::ostream &os = std::cout,
81  const char *header = NULL) const;
83  void printJaccard(std::ostream &os = std::cout,
84  const char *header = NULL) const;
87  void write(std::ostream &os) const;
89  void read(std::istream &is);
92  double rowSum(int n) const;
94  double colSum(int m) const;
96  double diagSum() const;
98  double totalSum() const;
100  double accuracy() const;
102  double avgPrecision() const;
104  double avgRecall() const;
106  double avgJaccard() const;
109  double precision(int n) const;
111  double recall(int n) const;
113  double jaccard(int n) const;
116  const unsigned& operator()(int x, int y) const;
118  unsigned& operator()(int x, int y);
119 };
const unsigned & operator()(int x, int y) const
returns the counts at location (x,y)
Definition: drwnConfusionMatrix.cpp:401
double avgJaccard() const
average of TP / (TP + FP + FN); diagonal / (rowSum + colSum - diagonal)
Definition: drwnConfusionMatrix.cpp:363
double colSum(int m) const
returns the sum of entries down a column
Definition: drwnConfusionMatrix.cpp:295
void printColNormalized(std::ostream &os=std::cout, const char *header=NULL) const
print the confusion normalized by column
Definition: drwnConfusionMatrix.cpp:157
void printPrecisionRecall(std::ostream &os=std::cout, const char *header=NULL) const
print precision and recall for each class
Definition: drwnConfusionMatrix.cpp:202
double recall(int n) const
recall for class n
Definition: drwnConfusionMatrix.cpp:385
double precision(int n) const
precision for class n
Definition: drwnConfusionMatrix.cpp:378
double diagSum() const
returns the sum of entries along the diagonal
Definition: drwnConfusionMatrix.cpp:304
drwnConfusionMatrix(int n)
construct a confusion matrix for n classes
Definition: drwnConfusionMatrix.cpp:33
void printF1Score(std::ostream &os=std::cout, const char *header=NULL) const
print the F1-score for each class
Definition: drwnConfusionMatrix.cpp:230
double rowSum(int n) const
returns the sum of entries along a row
Definition: drwnConfusionMatrix.cpp:286
double accuracy() const
diagSum / totalSum
Definition: drwnConfusionMatrix.cpp:326
Utility class for computing and printing confusion matrices.
Definition: drwnConfusionMatrix.h:30
double avgPrecision() const
average of TP / (TP + FP); diagonal / colSum
Definition: drwnConfusionMatrix.cpp:331
double jaccard(int n) const
jaccard coefficient for class n
Definition: drwnConfusionMatrix.cpp:392
int numCols() const
returns the number of columns
Definition: drwnConfusionMatrix.cpp:59
void printCounts(std::ostream &os=std::cout, const char *header=NULL) const
print the confusion matrix
Definition: drwnConfusionMatrix.cpp:119
void accumulate(int actual, int predicted)
accumulate a prediction/actual pair
Definition: drwnConfusionMatrix.cpp:73
Implements the interface for a generic machine learning classifier.
Definition: drwnClassifier.h:31
static std::string COL_SEP
string for separating columns when printing
Definition: drwnConfusionMatrix.h:32
static std::string ROW_BEGIN
string for starting a row when printing
Definition: drwnConfusionMatrix.h:33
void printRowNormalized(std::ostream &os=std::cout, const char *header=NULL) const
print the confusion normalized by row
Definition: drwnConfusionMatrix.cpp:137
void printNormalized(std::ostream &os=std::cout, const char *header=NULL) const
print the confusion matrix normalized by total count
Definition: drwnConfusionMatrix.cpp:181
void write(std::ostream &os) const
write the confusion matrix
Definition: drwnConfusionMatrix.cpp:272
double totalSum() const
returns the sum of all entries in the confusion matrix
Definition: drwnConfusionMatrix.cpp:315
static std::string ROW_END
string for ending a row when printing
Definition: drwnConfusionMatrix.h:34
Implements a cacheable dataset containing feature vectors, labels and optional weights.
Definition: drwnDataset.h:43
void clear()
clear all counts in the confusion matrix
Definition: drwnConfusionMatrix.cpp:66
void printJaccard(std::ostream &os=std::cout, const char *header=NULL) const
print the Jaccard (intersection-over-union) score for each class
Definition: drwnConfusionMatrix.cpp:252
void read(std::istream &is)
read a confusion matrix
Definition: drwnConfusionMatrix.cpp:277
double avgRecall() const
average of TP / (TP + FN); diagonal / rowSum
Definition: drwnConfusionMatrix.cpp:342
int numRows() const
returns the number of rows
Definition: drwnConfusionMatrix.cpp:54