SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MultilabelAccuracy.cpp
Go to the documentation of this file.
1 /*
2  * This software is distributed under BSD 3-clause license (see LICENSE file).
3  *
4  * Copyright(C) 2014 Abinash Panda
5  * Written(W) 2014 Abinash Panda
6  */
7 
10 
11 using namespace shogun;
12 
14  : CEvaluation()
15 {
16 }
17 
19 {
20 }
21 
23  CLabels* ground_truth)
24 {
26  "predicted label should be of multilabels type\n");
27  REQUIRE(ground_truth->get_label_type() == LT_SPARSE_MULTILABEL,
28  "actual label should be of multilabels type\n");
29  REQUIRE(ground_truth->get_label_type() == predicted->get_label_type(),
30  "predicted labels and actual labels should be of same type\n");
31 
32  CMultilabelLabels* m_predicted = (CMultilabelLabels*) predicted;
33  CMultilabelLabels* m_ground_truth = (CMultilabelLabels*) ground_truth;
34 
35  REQUIRE(m_predicted->get_num_labels() == m_ground_truth->get_num_labels(),
36  "predicted labels and actual labels should have same number of labels\n");
37  REQUIRE(m_predicted->get_num_classes() == m_ground_truth->get_num_classes(),
38  "predicted labels and actual labels should have same number of classes\n");
39 
40  int32_t num_labels = predicted->get_num_labels();
41  float64_t accuracy = 0.0;
42 
43  for (index_t k=0; k<num_labels; k++)
44  {
45  SGVector<int32_t> slabel_true = m_ground_truth->get_label(k);
46  SGVector<int32_t> slabel_pred = m_predicted->get_label(k);
47 
48  int32_t true_pos = 0;
49  index_t i = 0, j = 0;
50 
51  while (i<slabel_true.vlen && j<slabel_pred.vlen)
52  {
54  if (slabel_true[i] == slabel_pred[j])
55  {
56  true_pos ++;
57  i++;
58  j++;
59  }
61  else if (slabel_true[i] < slabel_pred[j])
62  {
63  i++;
64  }
66  else
67  {
68  j++;
69  }
70  }
71 
72  accuracy += ((float)true_pos /
73  (float)(slabel_true.vlen + slabel_pred.vlen - true_pos));
74  }
75 
76  return accuracy/num_labels;
77 }
78 
virtual ELabelType get_label_type() const =0
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
virtual float64_t evaluate(CLabels *predicted, CLabels *ground_truth)
virtual int32_t get_num_labels() const
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t vlen
Definition: SGVector.h:494
virtual int32_t get_num_classes() const
double float64_t
Definition: common.h:50
SGVector< int32_t > get_label(int32_t j)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
sparse label class for multilabel classification (sets of labels)
Definition: LabelTypes.h:28
Multilabel Labels for multi-label classification.
Class Evaluation, a base class for other classes used to evaluate labels, e.g. accuracy of classifica...
Definition: Evaluation.h:40

SHOGUN Machine Learning Toolbox - Documentation