[mlpack] 55/58: Forward-port fix for usage of log2().

Barak A. Pearlmutter barak+git at cs.nuim.ie
Tue Sep 9 13:19:43 UTC 2014


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 9c1bbcf6722c06b8c3bcee71bb77b0ba3f1a8181
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Fri Aug 29 14:45:29 2014 +0000

    Forward-port fix for usage of log2().
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17131 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 .../methods/decision_stump/decision_stump_impl.hpp      | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/mlpack/methods/decision_stump/decision_stump_impl.hpp b/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
index e43f416..24af7e1 100644
--- a/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
+++ b/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
@@ -178,7 +178,7 @@ double DecisionStump<MatType>::SetupSplitAttribute(
   for (i = 0; i < attribute.n_elem; i++)
   {
     sortedLabels(i) = labels(sortedIndexAtt(i));
-    
+
     if(isWeight)
       tempD(i) = weightD(sortedIndexAtt(i));
   }
@@ -435,7 +435,7 @@ double DecisionStump<MatType>::CalculateEntropy(
   double accWeight = 0.0;
   // Populate numElem; they are used as helpers to calculate entropy.
 
-  if(isWeight)
+  if (isWeight)
   {
     for (j = 0; j < labels.n_elem; j++)
     {
@@ -448,7 +448,10 @@ double DecisionStump<MatType>::CalculateEntropy(
     {
       const double p1 = ((double) numElem(j) / accWeight);
 
-      entropy += (p1 == 0) ? 0 : p1 * log2(p1);
+      // Instead of using log2(), which is C99 and may not exist on some
+      // compilers, use std::log(), then use the change-of-base formula to make
+      // the result correct.
+      entropy += (p1 == 0) ? 0 : p1 * std::log(p1);
     }
   }
   else
@@ -460,10 +463,14 @@ double DecisionStump<MatType>::CalculateEntropy(
     {
       const double p1 = ((double) numElem(j) / labels.n_elem);
 
-      entropy += (p1 == 0) ? 0 : p1 * log2(p1);
+      // Instead of using log2(), which is C99 and may not exist on some
+      // compilers, use std::log(), then use the change-of-base formula to make
+      // the result correct.
+      entropy += (p1 == 0) ? 0 : p1 * std::log(p1);
     }
   }
-  return entropy;
+
+  return entropy / std::log(2.0);
 }
 
 }; // namespace decision_stump

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list