SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StructuredAccuracy.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) 2012-2013 Fernando José Iglesias García
8  * Copyright (C) 2012-2013 Fernando José Iglesias García
9  */
10 
14 
15 using namespace shogun;
16 
18 {
19 }
20 
22 {
23 }
24 
26 {
27  REQUIRE(predicted && ground_truth, "CLabels objects passed to evaluate "
28  "cannot be null\n");
29  REQUIRE(predicted->get_num_labels() == ground_truth->get_num_labels(),
30  "The number of predicted and ground truth labels must "
31  "be the same\n");
32  REQUIRE(predicted->get_label_type() == LT_STRUCTURED, "The predicted "
33  "labels must be of type CStructuredLabels\n");
34  REQUIRE(ground_truth->get_label_type() == LT_STRUCTURED, "The ground truth "
35  "labels must be of type CStructuredLabels\n");
36 
37  CStructuredLabels* pred_labs = CLabelsFactory::to_structured(predicted);
38  CStructuredLabels* true_labs = CLabelsFactory::to_structured(ground_truth);
39 
40  REQUIRE(pred_labs->get_structured_data_type() ==
41  true_labs->get_structured_data_type(), "Predicted and ground truth "
42  "labels must be composed of the same structured data\n");
43 
44  switch ( pred_labs->get_structured_data_type() )
45  {
46  case (SDT_REAL):
47  return evaluate_real(pred_labs, true_labs);
48  case (SDT_SEQUENCE):
49  return evaluate_sequence(pred_labs, true_labs);
50  default:
51  SG_ERROR("Unknown structured data type for evaluation\n")
52  }
53 
54  return 0.0;
55 }
56 
58  CLabels* predicted, CLabels* ground_truth)
59 {
60  SG_SERROR("Not implemented\n")
61  return SGMatrix< int32_t >();
62 }
63 
64 float64_t CStructuredAccuracy::evaluate_real(CStructuredLabels* predicted,
65  CStructuredLabels* ground_truth)
66 {
67  int32_t length = predicted->get_num_labels();
68  int32_t num_equal = 0;
69 
70  for ( int32_t i = 0 ; i < length ; ++i )
71  {
72  CRealNumber* truth = CRealNumber::obtain_from_generic(ground_truth->get_label(i));
74 
75  num_equal += truth->value == pred->value;
76 
77  SG_UNREF(truth);
78  SG_UNREF(pred);
79  }
80 
81  return (1.0*num_equal) / length;
82 }
83 
84 float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels* predicted,
85  CStructuredLabels* ground_truth)
86 {
87  int32_t length = predicted->get_num_labels();
88  // Accuracy of each each label
89  SGVector< float64_t > accuracies(length);
90  int32_t num_equal = 0;
91 
92  for ( int32_t i = 0 ; i < length ; ++i )
93  {
94  CSequence* true_seq = CSequence::obtain_from_generic(ground_truth->get_label(i));
95  CSequence* pred_seq = CSequence::obtain_from_generic(predicted->get_label(i));
96 
97  SGVector<int32_t> true_seq_data = true_seq->get_data();
98  SGVector<int32_t> pred_seq_data = pred_seq->get_data();
99 
100  REQUIRE(true_seq_data.size() == pred_seq_data.size(), "Corresponding ground "
101  "truth and predicted sequences must be equally long\n");
102 
103  num_equal = 0;
104  // Count the number of elements that are equal in both sequences
105  for ( int32_t j = 0 ; j < true_seq_data.size() ; ++j )
106  num_equal += true_seq_data[j] == pred_seq_data[j];
107 
108  accuracies[i] = (1.0*num_equal) / true_seq_data.size();
109 
110  SG_UNREF(true_seq);
111  SG_UNREF(pred_seq);
112  }
113 
114  return accuracies.mean();
115 }

SHOGUN Machine Learning Toolbox - Documentation