SHOGUN  v3.0.1
 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  index_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 (index_t i=0; i < n_class; ++i)
35  {
36  for (index_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 (index_t i=0; i < n_class; ++i)
44  {
45  for (index_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 (index_t i=0; i < n_class; ++i)
54  {
55  for (index_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 (index_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 }
virtual float64_t evaluate(CLabels *predicted, CLabels *ground_truth)
virtual ELabelType get_label_type() const =0
int32_t index_t
Definition: common.h:60
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
virtual int32_t get_num_labels() const =0
multi-class labels 0,1,...
Definition: LabelTypes.h:16
#define SG_ERROR(...)
Definition: SGIO.h:131
Multiclass Labels for multi-class classification.
#define ASSERT(x)
Definition: SGIO.h:203
double float64_t
Definition: common.h:48
static T max(T a, T b)
return the maximum of two integers
Definition: Math.h:167
int32_t find_match_count(SGVector< int32_t > l1, int32_t m1, SGVector< int32_t > l2, int32_t m2)
index_t vlen
Definition: SGVector.h:706

SHOGUN Machine Learning Toolbox - Documentation