[mlpack-git] master: Forward-port fix for usage of log2(). (57cfaf1)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:59:55 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 57cfaf1c03f633d3aac8a654c16123333bc7a3bf
Author: Ryan Curtin <ryan at ratml.org>
Date:   Fri Aug 29 14:45:29 2014 +0000

    Forward-port fix for usage of log2().


>---------------------------------------------------------------

57cfaf1c03f633d3aac8a654c16123333bc7a3bf
 src/mlpack/methods/decision_stump/decision_stump_impl.hpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 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
@@ -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



More information about the mlpack-git mailing list