Skip to content

Commit

Permalink
Refine the code according to coding convention.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskid committed Apr 5, 2012
1 parent 9473a5d commit 49b6e2e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 64 deletions.
26 changes: 26 additions & 0 deletions src/shogun/evaluation/ClusteringAccuracy.cpp
@@ -0,0 +1,26 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Chiyuan Zhang
* Copyright (C) 2012 Chiyuan Zhang
*/

#include <shogun/evaluation/ClusteringAccuracy.h>

using namespace shogun;

float64_t CClusteringAccuracy::evaluate(CLabels* predicted, CLabels* ground_truth)
{
SGVector<int32_t> predicted_ilabels=predicted->get_int_labels();
SGVector<int32_t> groundtruth_ilabels=ground_truth->get_int_labels();
int32_t correct=0;
for (int32_t i=0; i < predicted_ilabels.vlen; ++i)
{
if (predicted_ilabels[i] == groundtruth_ilabels[i])
correct++;
}
return float64_t(correct)/predicted_ilabels.vlen;
}
13 changes: 1 addition & 12 deletions src/shogun/evaluation/ClusteringAccuracy.h
Expand Up @@ -35,18 +35,7 @@ class CClusteringAccuracy: public CClusteringEvaluation
* @param ground_truth labels assumed to be correct
* @return evaluation result
*/
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth)
{
SGVector<int32_t> predicted_ilabels=predicted->get_int_labels();
SGVector<int32_t> groundtruth_ilabels=ground_truth->get_int_labels();
int32_t correct=0;
for (int32_t i=0; i < predicted_ilabels.vlen; ++i)
{
if (predicted_ilabels[i] == groundtruth_ilabels[i])
correct++;
}
return float64_t(correct)/predicted_ilabels.vlen;
}
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth);

/** @return whether criterium has to be maximized or minimized */
virtual EEvaluationDirection get_evaluation_direction()
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/evaluation/ClusteringEvaluation.h
Expand Up @@ -11,6 +11,8 @@
#ifndef __CLUSTERINGEVALUATION_H__
#define __CLUSTERINGEVALUATION_H__

#include <vector>

#include <shogun/evaluation/Evaluation.h>
#include <shogun/features/Labels.h>

Expand Down
69 changes: 69 additions & 0 deletions src/shogun/evaluation/ClusteringMutualInformation.cpp
@@ -0,0 +1,69 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Chiyuan Zhang
* Copyright (C) 2012 Chiyuan Zhang
*/

#include <vector>
#include <cmath>
#include <algorithm>

#include <shogun/evaluation/ClusteringMutualInformation.h>

using namespace shogun;

float64_t CClusteringMutualInformation::evaluate(CLabels* predicted, CLabels* ground_truth)
{
std::vector<int32_t> label_p=unique_labels(predicted);
std::vector<int32_t> label_g=unique_labels(ground_truth);

if (label_p.size() != label_g.size())
SG_ERROR("Number of classes are different\n");
uint32_t n_class=label_p.size();
float64_t n_label=predicted->get_num_labels();

SGVector<int32_t> ilabels_p=predicted->get_int_labels();
SGVector<int32_t> ilabels_g=ground_truth->get_int_labels();

SGMatrix<float64_t> G(n_class, n_class);
for (size_t i=0; i < n_class; ++i)
{
for (size_t j=0; j < n_class; ++j)
G(i, j)=find_match_count(ilabels_g, label_g[i],
ilabels_p, label_p[j])/n_label;
}

std::vector<float64_t> G_rowsum(n_class), G_colsum(n_class);
for (size_t i=0; i < n_class; ++i)
{
for (size_t j=0; j < n_class; ++j)
{
G_rowsum[i] += G(i, j);
G_colsum[i] += G(j, i);
}
}

float64_t mutual_info = 0;
for (size_t i=0; i < n_class; ++i)
{
for (size_t j=0; j < n_class; ++j)
{
if (G(i, j) != 0)
mutual_info += G(i, j) * log(G(i,j) /
(G_rowsum[i]*G_colsum[j]))/log(2.);
}
}

float64_t entropy_p = 0, entropy_g = 0;
for (size_t i=0; i < n_class; ++i)
{
entropy_g += -G_rowsum[i] * log(G_rowsum[i])/log(2.);
entropy_p += -G_colsum[i] * log(G_colsum[i])/log(2.);
}

return mutual_info / std::max(entropy_g, entropy_p);
}
53 changes: 1 addition & 52 deletions src/shogun/evaluation/ClusteringMutualInformation.h
Expand Up @@ -11,10 +11,6 @@
#ifndef __CLUSTERINGMUTUALINFORMATION_H__
#define __CLUSTERINGMUTUALINFORMATION_H__

#include <vector>
#include <cmath>
#include <algorithm>

#include <shogun/evaluation/ClusteringEvaluation.h>

namespace shogun
Expand All @@ -39,54 +35,7 @@ class CClusteringMutualInformation: public CClusteringEvaluation
* @param ground_truth labels assumed to be correct
* @return evaluation result
*/
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth)
{
std::vector<int32_t> label_p=unique_labels(predicted);
std::vector<int32_t> label_g=unique_labels(ground_truth);

if (label_p.size() != label_g.size())
SG_ERROR("Number of classes are different\n");
uint32_t n_class=label_p.size();
float64_t n_label=predicted->get_num_labels();

SGVector<int32_t> ilabels_p=predicted->get_int_labels();
SGVector<int32_t> ilabels_g=ground_truth->get_int_labels();

SGMatrix<float64_t> G(n_class, n_class);
for (size_t i=0; i < n_class; ++i)
for (size_t j=0; j < n_class; ++j)
G(i, j)=find_match_count(ilabels_g, label_g[i],
ilabels_p, label_p[j])/n_label;
std::vector<float64_t> G_rowsum(n_class), G_colsum(n_class);
for (size_t i=0; i < n_class; ++i)
{
for (size_t j=0; j < n_class; ++j)
{
G_rowsum[i] += G(i, j);
G_colsum[i] += G(j, i);
}
}

float64_t mutual_info = 0;
for (size_t i=0; i < n_class; ++i)
{
for (size_t j=0; j < n_class; ++j)
{
if (G(i, j) != 0)
mutual_info += G(i, j) * log(G(i,j) /
(G_rowsum[i]*G_colsum[j]))/log(2.);
}
}

float64_t entropy_p = 0, entropy_g = 0;
for (size_t i=0; i < n_class; ++i)
{
entropy_g += -G_rowsum[i] * log(G_rowsum[i])/log(2.);
entropy_p += -G_colsum[i] * log(G_colsum[i])/log(2.);
}

return mutual_info / std::max(entropy_g, entropy_p);
}
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth);

/** @return whether criterium has to be maximized or minimized */
virtual EEvaluationDirection get_evaluation_direction()
Expand Down

0 comments on commit 49b6e2e

Please sign in to comment.