SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MulticlassAccuracy.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Sergey Lisitsyn
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
12 #include <shogun/labels/Labels.h>
15 
16 using namespace shogun;
17 
19 {
20  ASSERT(predicted && ground_truth)
21  ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels())
22  ASSERT(predicted->get_label_type() == LT_MULTICLASS)
23  ASSERT(ground_truth->get_label_type() == LT_MULTICLASS)
24  int32_t length = predicted->get_num_labels();
25  int32_t correct = 0;
26  if (m_ignore_rejects)
27  {
28  for (int32_t i=0; i<length; i++)
29  {
30  if (((CMulticlassLabels*) predicted)->get_int_label(i)==((CMulticlassLabels*) ground_truth)->get_int_label(i))
31  correct++;
32  }
33  return ((float64_t)correct)/length;
34  }
35  else
36  {
37  int32_t total = length;
38  for (int32_t i=0; i<length; i++)
39  {
40  int32_t predicted_label = ((CMulticlassLabels*) predicted)->get_int_label(i);
41 
42  if (predicted_label==((CMulticlassLabels*) predicted)->REJECTION_LABEL)
43  total--;
44  else if (predicted_label==((CMulticlassLabels*) ground_truth)->get_int_label(i))
45  correct++;
46  }
47  m_rejects_num = length-total;
48  SG_DEBUG("correct=%d, total=%d, rejected=%d\n",correct,total,length-total)
49  return ((float64_t)correct)/total;
50  }
51  return 0.0;
52 }
53 
55 {
56  ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels())
57  int32_t length = ground_truth->get_num_labels();
58  int32_t num_classes = ((CMulticlassLabels*) ground_truth)->get_num_classes();
59  SGMatrix<int32_t> confusion_matrix(num_classes, num_classes);
60  memset(confusion_matrix.matrix,0,sizeof(int32_t)*num_classes*num_classes);
61  for (int32_t i=0; i<length; i++)
62  {
63  int32_t predicted_label = ((CMulticlassLabels*) predicted)->get_int_label(i);
64  int32_t ground_truth_label = ((CMulticlassLabels*) ground_truth)->get_int_label(i);
65 
66  if (predicted_label==((CMulticlassLabels*) predicted)->REJECTION_LABEL)
67  continue;
68 
69  confusion_matrix[predicted_label*num_classes+ground_truth_label]++;
70  }
71  return confusion_matrix;
72 }
73 

SHOGUN Machine Learning Toolbox - Documentation