SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ClusteringEvaluation.cpp
浏览该文件的文档.
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 }
virtual ELabelType get_label_type() const =0
void best_map(CLabels *predicted, CLabels *ground_truth)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
multi-class labels 0,1,...
Definition: LabelTypes.h:20
Munkres.
Definition: munkres.h:36
Multiclass Labels for multi-class classification.
int32_t find_mismatch_count(SGVector< int32_t > l1, int32_t m1, SGVector< int32_t > l2, int32_t m2)
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
void solve()
Definition: munkres.h:46
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
int32_t find_match_count(SGVector< int32_t > l1, int32_t m1, SGVector< int32_t > l2, int32_t m2)
Matrix::Scalar max(Matrix m)
Definition: Redux.h:66

SHOGUN 机器学习工具包 - 项目文档