SHOGUN  v2.0.0
 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 Fernando José Iglesias García
8  * Copyright (C) 2012 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 
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 =
74  CRealNumber* pred =
76 
77  num_equal += truth->value == pred->value;
78 
79  SG_UNREF(truth);
80  SG_UNREF(pred);
81  }
82 
83  return (1.0*num_equal) / length;
84 }
85 
86 float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels* predicted,
87  CStructuredLabels* ground_truth)
88 {
89  int32_t length = predicted->get_num_labels();
90  // Accuracy of each each label
91  SGVector< float64_t > accuracies(length);
92  int32_t num_equal = 0;
93 
94  for ( int32_t i = 0 ; i < length ; ++i )
95  {
96  CSequence* true_seq =
97  CSequence::obtain_from_generic(ground_truth->get_label(i));
98  CSequence* pred_seq =
100 
101  SGVector<int32_t> true_seq_data = true_seq->get_data();
102  SGVector<int32_t> pred_seq_data = pred_seq->get_data();
103 
104  REQUIRE(true_seq_data.size() == pred_seq_data.size(), "Corresponding ground "
105  "truth and predicted sequences must be equally long\n");
106 
107  num_equal = 0;
108  // Count the number of elements that are equal in both sequences
109  for ( int32_t j = 0 ; j < true_seq_data.size() ; ++j )
110  num_equal += true_seq_data[j] == pred_seq_data[j];
111 
112  accuracies[i] = (1.0*num_equal) / true_seq_data.size();
113 
114  SG_UNREF(true_seq);
115  SG_UNREF(pred_seq);
116  }
117 
118  return accuracies.mean();
119 }

SHOGUN Machine Learning Toolbox - Documentation