[mlpack-git] master: Refactor CountMostFreq() so it is faster, simpler, and doesn't sometimes return uninitialized values. (e4a0ff4)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:03:04 EST 2015


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

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

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

commit e4a0ff4c5b01b5635f88415f65d953c1ce4a040a
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Nov 11 18:46:31 2014 +0000

    Refactor CountMostFreq() so it is faster, simpler, and doesn't sometimes return
    uninitialized values.


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

e4a0ff4c5b01b5635f88415f65d953c1ce4a040a
 .../methods/decision_stump/decision_stump.hpp      |  0
 .../methods/decision_stump/decision_stump_impl.hpp | 54 +++++++++-------------
 2 files changed, 22 insertions(+), 32 deletions(-)

diff --git a/src/mlpack/methods/decision_stump/decision_stump_impl.hpp b/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
index 5775653..d18365c 100644
--- a/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
+++ b/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
@@ -356,44 +356,34 @@ template <typename MatType>
 template <typename rType>
 rType DecisionStump<MatType>::CountMostFreq(const arma::Row<rType>& subCols)
 {
-  // Sort subCols for easier processing.
-  arma::Row<rType> sortCounts = arma::sort(subCols);
-  rType element;
-  int count = 0, localCount = 0;
+  // We'll create a map of elements and the number of times that each element is
+  // seen.
+  std::map<rType, size_t> countMap;
 
-  if (sortCounts.n_elem == 1)
-    return sortCounts[0];
-
-  // An O(n) loop which counts the most frequent element in sortCounts
-  for (size_t i = 0; i < sortCounts.n_elem; ++i)
+  for (size_t i = 0; i < subCols.n_elem; ++i)
   {
-    if (i == sortCounts.n_elem - 1)
-    {
-      if (sortCounts(i - 1) == sortCounts(i))
-      {
-        // element = sortCounts(i - 1);
-        localCount++;
-      }
-      else if (localCount > count)
-        count = localCount;
-    }
-    else if (sortCounts(i) != sortCounts(i + 1))
-    {
-      localCount = 0;
-      count++;
-    }
+    if (countMap.count(subCols[i]) == 0)
+      countMap[subCols[i]] = 1;
     else
+      ++countMap[subCols[i]];
+  }
+
+  // Now find the maximum value.
+  typename std::map<rType, size_t>::iterator it = countMap.begin();
+  rType mostFreq = it->first;
+  size_t mostFreqCount = it->second;
+  while (it != countMap.end())
+  {
+    if (it->second >= mostFreqCount)
     {
-      localCount++;
-      if (localCount > count)
-      {
-        count = localCount;
-        if (localCount == 1)
-          element = sortCounts(i);
-      }
+      mostFreq = it->first;
+      mostFreqCount = it->second;
     }
+
+    ++it;
   }
-  return element;
+
+  return mostFreq;
 }
 
 /**



More information about the mlpack-git mailing list