SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 

SHOGUN Machine Learning Toolbox - Documentation