SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClusteringMutualInformation.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 <shogun/lib/SGVector.h>
14 
15 using namespace shogun;
16 
18 {
19  ASSERT(predicted && ground_truth)
20  ASSERT(predicted->get_label_type() == LT_MULTICLASS)
21  ASSERT(ground_truth->get_label_type() == LT_MULTICLASS)
22  SGVector<float64_t> label_p=((CMulticlassLabels*) predicted)->get_unique_labels();
23  SGVector<float64_t> label_g=((CMulticlassLabels*) ground_truth)->get_unique_labels();
24 
25  if (label_p.vlen != label_g.vlen)
26  SG_ERROR("Number of classes are different\n")
27  uint32_t n_class=label_p.vlen;
28  float64_t n_label=predicted->get_num_labels();
29 
30  SGVector<int32_t> ilabels_p=((CMulticlassLabels*) predicted)->get_int_labels();
31  SGVector<int32_t> ilabels_g=((CMulticlassLabels*) ground_truth)->get_int_labels();
32 
33  SGMatrix<float64_t> G(n_class, n_class);
34  for (size_t i=0; i < n_class; ++i)
35  {
36  for (size_t j=0; j < n_class; ++j)
37  G(i, j)=find_match_count(ilabels_g, label_g[i],
38  ilabels_p, label_p[j])/n_label;
39  }
40 
41  SGVector<float64_t> G_rowsum(n_class);
42  SGVector<float64_t> G_colsum(n_class);
43  for (size_t i=0; i < n_class; ++i)
44  {
45  for (size_t j=0; j < n_class; ++j)
46  {
47  G_rowsum[i] += G(i, j);
48  G_colsum[i] += G(j, i);
49  }
50  }
51 
52  float64_t mutual_info = 0;
53  for (size_t i=0; i < n_class; ++i)
54  {
55  for (size_t j=0; j < n_class; ++j)
56  {
57  if (G(i, j) != 0)
58  mutual_info += G(i, j) * log(G(i,j) /
59  (G_rowsum[i]*G_colsum[j]))/log(2.);
60  }
61  }
62 
63  float64_t entropy_p = 0;
64  float64_t entropy_g = 0;
65  for (size_t i=0; i < n_class; ++i)
66  {
67  entropy_g += -G_rowsum[i] * log(G_rowsum[i])/log(2.);
68  entropy_p += -G_colsum[i] * log(G_colsum[i])/log(2.);
69  }
70 
71  return mutual_info / CMath::max(entropy_g, entropy_p);
72 }

SHOGUN Machine Learning Toolbox - Documentation