SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClusteringEvaluation.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 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #include <set>
12 #include <map>
13 #include <vector>
14 #include <algorithm>
15 
19 
20 using namespace shogun;
21 using namespace std;
22 
24 {
25  int32_t match_count=0;
26  for (int32_t i=l1.vlen-1; i >= 0; --i)
27  {
28  if (l1[i] == m1 && l2[i] == m2)
29  match_count++;
30  }
31 
32  return match_count;
33 }
34 
36 {
37  return l1.vlen - find_match_count(l1, m1, l2, m2);
38 }
39 
40 void CClusteringEvaluation::best_map(CLabels* predicted, CLabels* ground_truth)
41 {
42  ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels())
43  ASSERT(predicted->get_label_type() == LT_MULTICLASS)
44  ASSERT(ground_truth->get_label_type() == LT_MULTICLASS)
45 
46  SGVector<float64_t> label_p=((CMulticlassLabels*) predicted)->get_unique_labels();
47  SGVector<float64_t> label_g=((CMulticlassLabels*) ground_truth)->get_unique_labels();
48 
49  SGVector<int32_t> predicted_ilabels=((CMulticlassLabels*) predicted)->get_int_labels();
50  SGVector<int32_t> groundtruth_ilabels=((CMulticlassLabels*) ground_truth)->get_int_labels();
51 
52  int32_t n_class=max(label_p.vlen, label_g.vlen);
53  SGMatrix<float64_t> G(n_class, n_class);
54  G.zero();
55 
56  for (int32_t i=0; i < label_g.vlen; ++i)
57  {
58  for (int32_t j=0; j < label_p.vlen; ++j)
59  {
60  G(i, j)=find_mismatch_count(groundtruth_ilabels, static_cast<int32_t>(label_g[i]),
61  predicted_ilabels, static_cast<int32_t>(label_p[j]));
62  }
63  }
64 
65  Munkres munkres_solver(G);
66  munkres_solver.solve();
67 
68  std::map<int32_t, int32_t> label_map;
69  for (int32_t i=0; i < label_p.vlen; ++i)
70  {
71  for (int32_t j=0; j < label_g.vlen; ++j)
72  {
73  if (G(j, i) == 0)
74  {
75  label_map.insert(make_pair(static_cast<int32_t>(label_p[i]),
76  static_cast<int32_t>(label_g[j])));
77  break;
78  }
79  }
80  }
81 
82  for (int32_t i= 0; i < predicted_ilabels.vlen; ++i)
83  ((CMulticlassLabels*) predicted)->set_int_label(i, label_map[predicted_ilabels[i]]);
84 }

SHOGUN Machine Learning Toolbox - Documentation