SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskROCEvaluation.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  * Copyright (C) 2012 Sergey Lisitsyn
8  */
9 
12 
13 #include <set>
14 #include <vector>
15 
16 using namespace std;
17 using namespace shogun;
18 
19 void CMultitaskROCEvaluation::set_indices(SGVector<index_t> indices)
20 {
21  indices.display_vector("indices");
22  ASSERT(m_task_relation)
23 
24  set<index_t> indices_set;
25  for (int32_t i=0; i<indices.vlen; i++)
26  indices_set.insert(indices[i]);
27 
28  if (m_num_tasks>0)
29  {
30  SG_FREE(m_tasks_indices);
31  }
32  m_num_tasks = m_task_relation->get_num_tasks();
33  m_tasks_indices = SG_MALLOC(SGVector<index_t>, m_num_tasks);
34 
35  SGVector<index_t>* tasks_indices = m_task_relation->get_tasks_indices();
36  for (int32_t t=0; t<m_num_tasks; t++)
37  {
38  vector<index_t> task_indices_cut;
39  SGVector<index_t> task_indices = tasks_indices[t];
40  //task_indices.display_vector("task indices");
41  for (int32_t i=0; i<task_indices.vlen; i++)
42  {
43  if (indices_set.count(task_indices[i]))
44  {
45  //SG_SPRINT("%d is in %d task\n",task_indices[i],t)
46  task_indices_cut.push_back(task_indices[i]);
47  }
48  }
49 
50  SGVector<index_t> cutted(task_indices_cut.size());
51  for (int32_t i=0; i<cutted.vlen; i++)
52  cutted[i] = task_indices_cut[i];
53  //cutted.display_vector("cutted");
54  m_tasks_indices[t] = cutted;
55  }
56  SG_FREE(tasks_indices);
57 }
58 
59 float64_t CMultitaskROCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth)
60 {
61  //SG_SPRINT("Evaluate\n")
62  predicted->remove_all_subsets();
63  ground_truth->remove_all_subsets();
64  float64_t result = 0.0;
65  for (int32_t t=0; t<m_num_tasks; t++)
66  {
67  //SG_SPRINT("%d task", t)
68  //m_tasks_indices[t].display_vector();
69  predicted->add_subset(m_tasks_indices[t]);
70  ground_truth->add_subset(m_tasks_indices[t]);
71  result += evaluate_roc(predicted,ground_truth)/m_tasks_indices[t].vlen;
72  predicted->remove_subset();
73  ground_truth->remove_subset();
74  }
75  return result;
76 }

SHOGUN Machine Learning Toolbox - Documentation