[mlpack-git] master: delete Impute() overloads that produce output matrix (d043235)

gitdub at mlpack.org gitdub at mlpack.org
Fri Jul 22 23:54:08 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/ecbfd24defe31d9f39708c0b4c6ad352cd46ed5c...7eec0609aa21cb12aeed3cbcaa1e411dad0359f2

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

commit d04323513302b5e039001e45852e79a74aba3740
Author: Keon Kim <kwk236 at gmail.com>
Date:   Sat Jul 23 12:54:08 2016 +0900

    delete Impute() overloads that produce output matrix


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

d04323513302b5e039001e45852e79a74aba3740
 .../data/imputation_methods/custom_imputation.hpp  |  74 ------
 .../data/imputation_methods/listwise_deletion.hpp  |  44 ----
 .../data/imputation_methods/mean_imputation.hpp    |  99 --------
 .../data/imputation_methods/median_imputation.hpp  |  88 -------
 src/mlpack/core/data/imputer.hpp                   |  19 --
 src/mlpack/tests/imputation_test.cpp               | 270 +++++++++------------
 6 files changed, 118 insertions(+), 476 deletions(-)

diff --git a/src/mlpack/core/data/imputation_methods/custom_imputation.hpp b/src/mlpack/core/data/imputation_methods/custom_imputation.hpp
index 03b9d77..c8674a3 100644
--- a/src/mlpack/core/data/imputation_methods/custom_imputation.hpp
+++ b/src/mlpack/core/data/imputation_methods/custom_imputation.hpp
@@ -28,80 +28,6 @@ class CustomImputation
   /**
    * Impute function searches through the input looking for mappedValue and
    * replaces it with the user-defined custom value of the given dimension.
-   * The result is saved to the output. Custom value must be set when
-   * initializing the CustomImputation object.
-   *
-   * @param input Matrix that contains mappedValue.
-   * @param output Matrix that the result will be saved into.
-   * @param mappedValue Value that the user wants to get rid of.
-   * @param dimension Index of the dimension of the mappedValue.
-   * @param columnMajor State of whether the input matrix is columnMajor or not.
-   */
-  void Impute(const arma::Mat<T>& input,
-              arma::Mat<T>& output,
-              const T& mappedValue,
-              const size_t dimension,
-              const bool columnMajor = true)
-  {
-    // set size of the output
-    output.set_size(input.n_rows, input.n_cols);
-
-    // replace the target value to custom value
-    if (columnMajor)
-    {
-      for (size_t row = 0; row < input.n_rows; ++row)
-      {
-        for (size_t col = 0; col < input.n_cols; ++col)
-        {
-          if (row == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              output(row, col) = customValue;
-            }
-            else
-            {
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-    else
-    {
-      for (size_t col = 0; col < input.n_cols; ++ col)
-      {
-        for (size_t row = 0; row < input.n_rows; ++row)
-        {
-          if (col == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              output(row, col) = customValue;
-            }
-            else
-            {
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Impute function searches through the input looking for mappedValue and
-   * replaces it with the user-defined custom value of the given dimension.
    * The result is overwritten to the input, not creating any copy. Custom value
    * must be set when initializing the CustomImputation object.
    *
diff --git a/src/mlpack/core/data/imputation_methods/listwise_deletion.hpp b/src/mlpack/core/data/imputation_methods/listwise_deletion.hpp
index 0ac84ae..36eeeeb 100644
--- a/src/mlpack/core/data/imputation_methods/listwise_deletion.hpp
+++ b/src/mlpack/core/data/imputation_methods/listwise_deletion.hpp
@@ -22,50 +22,6 @@ class ListwiseDeletion
  public:
   /**
    * Impute function searches through the input looking for mappedValue and
-   * remove the whole row or column. The result is saved to the output.
-   *
-   * @param input Matrix that contains mappedValue.
-   * @param output Matrix that the result will be saved into.
-   * @param mappedValue Value that the user wants to get rid of.
-   * @param dimension Index of the dimension of the mappedValue.
-   * @param columnMajor State of whether the input matrix is columnMajor or not.
-   */
-  void Impute(const arma::Mat<T>& input,
-              arma::Mat<T>& output,
-              const T& mappedValue,
-              const size_t dimension,
-              const bool columnMajor = true)
-  {
-    std::vector<arma::uword> colsToKeep;
-
-    if (columnMajor)
-    {
-      for (size_t i = 0; i < input.n_cols; ++i)
-      {
-         if (!(input(dimension, i) == mappedValue ||
-             std::isnan(input(dimension, i))))
-         {
-           colsToKeep.push_back(i);
-         }
-      }
-      output = input.cols(arma::uvec(colsToKeep));
-    }
-    else
-    {
-      for (size_t i = 0; i < input.n_rows; ++i)
-      {
-        if (!(input(i, dimension) == mappedValue ||
-             std::isnan(input(i, dimension))))
-        {
-           colsToKeep.push_back(i);
-        }
-      }
-      output = input.rows(arma::uvec(colsToKeep));
-    }
-  }
-
-  /**
-   * Impute function searches through the input looking for mappedValue and
    * remove the whole row or column. The result is overwritten to the input.
    *
    * @param input Matrix that contains mappedValue.
diff --git a/src/mlpack/core/data/imputation_methods/mean_imputation.hpp b/src/mlpack/core/data/imputation_methods/mean_imputation.hpp
index b276ca8..e7a955e 100644
--- a/src/mlpack/core/data/imputation_methods/mean_imputation.hpp
+++ b/src/mlpack/core/data/imputation_methods/mean_imputation.hpp
@@ -21,105 +21,6 @@ class MeanImputation
  public:
   /**
    * Impute function searches through the input looking for mappedValue and
-   * replaces it with the mean of the given dimension. The result is saved
-   * to the output.
-   *
-   * @param input Matrix that contains mappedValue.
-   * @param output Matrix that the result will be saved into.
-   * @param mappedValue Value that the user wants to get rid of.
-   * @param dimension Index of the dimension of the mappedValue.
-   * @param columnMajor State of whether the input matrix is columnMajor or not.
-   */
-  void Impute(const arma::Mat<T>& input,
-              arma::Mat<T>& output,
-              const T& mappedValue,
-              const size_t dimension,
-              const bool columnMajor = true)
-  {
-    // set size of the output
-    output.set_size(input.n_rows, input.n_cols);
-
-    double sum = 0;
-    size_t elems = 0; // excluding nan or missing target
-
-    using PairType = std::pair<size_t, size_t>;
-    // dimensions and indexes are saved as pairs inside this vector.
-    std::vector<PairType> targets;
-
-    // calculate number of elements and sum of them excluding mapped value or
-    // nan. while doing that, remember where mappedValue or NaN exists.
-    if (columnMajor)
-    {
-      for (size_t row = 0; row < input.n_rows; ++row)
-      {
-        for (size_t col = 0; col < input.n_cols; ++col)
-        {
-          if (row == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              targets.emplace_back(row, col);
-            }
-            else
-            {
-              elems++;
-              sum += input(row, col);
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-    else
-    {
-      for (size_t col = 0; col < input.n_cols; ++col)
-      {
-        for (size_t row = 0; row < input.n_rows; ++row)
-        {
-          if (col == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              targets.emplace_back(row, col);
-            }
-            else
-            {
-              elems++;
-              sum += input(row, col);
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-
-    if (elems == 0)
-      Log::Fatal << "it is impossible to calculate mean; no valid elements in "
-          << "the dimension" << std::endl;
-
-    // calculate mean;
-    const double mean = sum / elems;
-
-    // Now replace the calculated mean to the missing variables
-    // It only needs to loop through targets vector, not the whole matrix.
-    for (const PairType& target : targets)
-    {
-      output(target.first, target.second) = mean;
-    }
-  }
-
-  /**
-   * Impute function searches through the input looking for mappedValue and
    * replaces it with the mean of the given dimension. The result is overwritten
    * to the input matrix.
    *
diff --git a/src/mlpack/core/data/imputation_methods/median_imputation.hpp b/src/mlpack/core/data/imputation_methods/median_imputation.hpp
index 658816e..828d22a 100644
--- a/src/mlpack/core/data/imputation_methods/median_imputation.hpp
+++ b/src/mlpack/core/data/imputation_methods/median_imputation.hpp
@@ -22,94 +22,6 @@ class MedianImputation
  public:
   /**
    * Impute function searches through the input looking for mappedValue and
-   * replaces it with the median of the given dimension. The result is saved
-   * to the output.
-   *
-   * @param input Matrix that contains mappedValue.
-   * @param output Matrix that the result will be saved into.
-   * @param mappedValue Value that the user wants to get rid of.
-   * @param dimension Index of the dimension of the mappedValue.
-   * @param columnMajor State of whether the input matrix is columnMajor or not.
-   */
-  void Impute(const arma::Mat<T>& input,
-              arma::Mat<T>& output,
-              const T& mappedValue,
-              const size_t dimension,
-              const bool columnMajor = true)
-  {
-    // set size of the output
-    output.set_size(input.n_rows, input.n_cols);
-
-    using PairType = std::pair<size_t, size_t>;
-    // dimensions and indexes are saved as pairs inside this vector.
-    std::vector<PairType> targets;
-    // good elements are kept inside this vector.
-    std::vector<double> elemsToKeep;
-
-    if (columnMajor)
-    {
-      for (size_t row = 0; row < input.n_rows; ++row)
-      {
-        for (size_t col = 0; col < input.n_cols; ++col)
-        {
-          if (row == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              targets.emplace_back(row, col);
-            }
-            else
-            {
-              elemsToKeep.push_back(input(row, col));
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-    else
-    {
-      for (size_t col = 0; col < input.n_cols; ++col)
-      {
-        for (size_t row = 0; row < input.n_rows; ++row)
-        {
-          if (col == dimension)
-          {
-            if (input(row, col) == mappedValue ||
-                std::isnan(input(row, col)))
-            {
-              targets.emplace_back(row, col);
-            }
-            else
-            {
-              elemsToKeep.push_back(input(row, col));
-              output(row, col) = input(row, col);
-            }
-          }
-          else
-          {
-            output(row, col) = input(row, col);
-          }
-        }
-      }
-    }
-
-    // calculate median
-    const double median = arma::median(arma::vec(elemsToKeep));
-
-    for (const PairType& target : targets)
-    {
-       output(target.first, target.second) = median;
-    }
-  }
-
-  /**
-   * Impute function searches through the input looking for mappedValue and
    * replaces it with the median of the given dimension. The result is
    * overwritten to the input matrix.
    *
diff --git a/src/mlpack/core/data/imputer.hpp b/src/mlpack/core/data/imputer.hpp
index f6134a7..ea1ac68 100644
--- a/src/mlpack/core/data/imputer.hpp
+++ b/src/mlpack/core/data/imputer.hpp
@@ -45,25 +45,6 @@ class Imputer
 
   /**
   * Given an input dataset, replace missing values with given imputation
-  * strategy. This overload saves the result into the output matrix and does not
-  * change the input matrix.
-  *
-  * @param input Input dataset to apply imputation.
-  * @param output Armadillo matrix to save the results
-  * @oaran missingValue User defined missing value; it can be anything.
-  * @param dimension Dimension to apply the imputation.
-  */
-  void Impute(const arma::Mat<T>& input,
-              arma::Mat<T>& output,
-              const std::string& missingValue,
-              const size_t dimension)
-  {
-    T mappedValue = static_cast<T>(mapper.UnmapValue(missingValue, dimension));
-    strategy.Impute(input, output, mappedValue, dimension, columnMajor);
-  }
-
-  /**
-  * Given an input dataset, replace missing values with given imputation
   * strategy. This overload does not produce output matrix, but overwrites the
   * result into the input matrix.
   *
diff --git a/src/mlpack/tests/imputation_test.cpp b/src/mlpack/tests/imputation_test.cpp
index 2e815ef..ce48ad0 100644
--- a/src/mlpack/tests/imputation_test.cpp
+++ b/src/mlpack/tests/imputation_test.cpp
@@ -25,19 +25,6 @@ using namespace std;
 
 BOOST_AUTO_TEST_SUITE(ImputationTest);
 /**
- * Check if two matrixes are equal.
- */
-void CheckEqual(const arma::mat& lhs, const arma::mat& rhs)
-{
-  BOOST_REQUIRE(lhs.n_rows == rhs.n_rows);
-  BOOST_REQUIRE(lhs.n_cols == rhs.n_cols);
-  for(size_t i = 0; i != lhs.n_elem; ++i)
-  {
-    BOOST_REQUIRE_CLOSE(lhs[i], rhs[i], 1e-5);
-  }
-}
-
-/**
  * 1. Make sure a CSV is loaded correctly with mappings using MissingPolicy.
  * 2. Try Imputer object with CustomImputation method to impute data "a".
  * (It is ok to test on one method since the other ones will be covered in the
@@ -53,7 +40,6 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
   f.close();
 
   arma::mat input;
-  arma::mat output;
 
   std::set<string> mset;
   mset.insert("a");
@@ -83,18 +69,18 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
           DatasetMapper<MissingPolicy>,
           CustomImputation<double>> imputer(info, customStrategy);
   // convert a or nan to 99 for dimension 0
-  imputer.Impute(input, output, "a", 0);
+  imputer.Impute(input, "a", 0);
 
   // Custom imputation result check
-  BOOST_REQUIRE_CLOSE(output(0, 0), 99.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 1), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 2), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 0), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 2), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 0), 3.0, 1e-5);
-  BOOST_REQUIRE(std::isnan(output(2, 1)) == true); // remains as NaN
-  BOOST_REQUIRE_CLOSE(output(2, 2), 10.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(0, 0), 99.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(0, 1), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(0, 2), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(1, 0), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(1, 2), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(input(2, 0), 3.0, 1e-5);
+  BOOST_REQUIRE(std::isnan(input(2, 1)) == true); // remains as NaN
+  BOOST_REQUIRE_CLOSE(input(2, 2), 10.0, 1e-5);
 
   // Remove the file.
   remove("test_file.csv");
@@ -105,51 +91,46 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
  */
 BOOST_AUTO_TEST_CASE(CustomImputationTest)
 {
-  arma::mat input("3.0 0.0 2.0 0.0;"
+  arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
                   "5.0 6.0 0.0 6.0;"
                   "9.0 8.0 4.0 8.0;");
-  arma::mat outputT; // assume input is column wise
-  arma::mat output;  // assume input is row wise
+  arma::mat rowWiseInput(columnWiseInput);
   double customValue = 99;
   double mappedValue = 0.0;
 
   CustomImputation<double> imputer(customValue);
 
   // column wise
-  imputer.Impute(input, outputT, mappedValue, 0/*dimension*/, true);
-
-  BOOST_REQUIRE_CLOSE(outputT(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 1), 99.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 3), 99.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 3), 8.0, 1e-5);
+  imputer.Impute(columnWiseInput, mappedValue, 0/*dimension*/, true);
+
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 99.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 99.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
 
   // row wise
-  imputer.Impute(input, output, mappedValue, 1, false);
-
-  BOOST_REQUIRE_CLOSE(output(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 1), 99.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 3), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 3), 8.0, 1e-5);
-
-  // overwrite to the input
-  imputer.Impute(input, mappedValue, 0/*dimension*/, true);
-  CheckEqual(input, outputT);
+  imputer.Impute(rowWiseInput, mappedValue, 1, false);
+
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 99.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 3), 8.0, 1e-5);
 }
 
 /**
@@ -158,50 +139,45 @@ BOOST_AUTO_TEST_CASE(CustomImputationTest)
  */
 BOOST_AUTO_TEST_CASE(MeanImputationTest)
 {
-  arma::mat input("3.0 0.0 2.0 0.0;"
+  arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
                   "5.0 6.0 0.0 6.0;"
                   "9.0 8.0 4.0 8.0;");
-  arma::mat outputT; // assume input is column wise
-  arma::mat output;  // assume input is row wise
+  arma::mat rowWiseInput(columnWiseInput);
   double mappedValue = 0.0;
 
   MeanImputation<double> imputer;
 
   // column wise
-  imputer.Impute(input, outputT, mappedValue, 0, true);
-
-  BOOST_REQUIRE_CLOSE(outputT(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 1), 2.5, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 3), 2.5, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 3), 8.0, 1e-5);
+  imputer.Impute(columnWiseInput, mappedValue, 0, true);
+
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 2.5, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 2.5, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
 
   // row wise
-  imputer.Impute(input, output, mappedValue, 1, false);
-
-  BOOST_REQUIRE_CLOSE(output(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 1), 7.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 3), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 3), 8.0, 1e-5);
-
-  // overwrite to the input
-  imputer.Impute(input, mappedValue, 0/*dimension*/, true);
-  CheckEqual(input, outputT);
+  imputer.Impute(rowWiseInput, mappedValue, 1, false);
+
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 7.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 3), 8.0, 1e-5);
 }
 
 /**
@@ -210,49 +186,44 @@ BOOST_AUTO_TEST_CASE(MeanImputationTest)
  */
 BOOST_AUTO_TEST_CASE(MedianImputationTest)
 {
-  arma::mat input("3.0 0.0 2.0 0.0;"
+  arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
                   "5.0 6.0 0.0 6.0;"
                   "9.0 8.0 4.0 8.0;");
-  arma::mat outputT; // assume input is column wise
-  arma::mat output;  // assume input is row wise
+  arma::mat rowWiseInput(columnWiseInput);
   double mappedValue = 0.0;
 
   MedianImputation<double> imputer;
 
   // column wise
-  imputer.Impute(input, outputT, mappedValue, 1, true);
-
-  BOOST_REQUIRE_CLOSE(outputT(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 1), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 3), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 2), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 3), 8.0, 1e-5);
+  imputer.Impute(columnWiseInput, mappedValue, 1, true);
+
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
 
   // row wise
-  imputer.Impute(input, output, mappedValue, 1, false);
-
-  BOOST_REQUIRE_CLOSE(output(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 1), 7.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 2), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 3), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(2, 2), 4.0, 1e-5);
-
-  // overwrite to the input
-  imputer.Impute(input, mappedValue, 1/*dimension*/, true);
-  CheckEqual(input, outputT);
+  imputer.Impute(rowWiseInput, mappedValue, 1, false);
+
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 7.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
 }
 
 /**
@@ -261,40 +232,35 @@ BOOST_AUTO_TEST_CASE(MedianImputationTest)
  */
 BOOST_AUTO_TEST_CASE(ListwiseDeletionTest)
 {
-  arma::mat input("3.0 0.0 2.0 0.0;"
+  arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
                   "5.0 6.0 0.0 6.0;"
                   "9.0 8.0 4.0 8.0;");
-  arma::mat outputT; // assume input is column wise
-  arma::mat output;  // assume input is row wise
+  arma::mat rowWiseInput(columnWiseInput);
   double mappedValue = 0.0;
 
   ListwiseDeletion<double> imputer;
 
   // column wise
-  imputer.Impute(input, outputT, mappedValue, 0, true); // column wise
+  imputer.Impute(columnWiseInput, mappedValue, 0, true); // column wise
 
-  BOOST_REQUIRE_CLOSE(outputT(0, 0), 3.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(0, 1), 2.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(1, 1), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(outputT(2, 1), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 2.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 4.0, 1e-5);
 
   // row wise
-  imputer.Impute(input, output, mappedValue, 1, false); // row wise
-
-  BOOST_REQUIRE_CLOSE(output(0, 0), 5.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 1), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 2), 0.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(0, 3), 6.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 0), 9.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 1), 8.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 2), 4.0, 1e-5);
-  BOOST_REQUIRE_CLOSE(output(1, 3), 8.0, 1e-5);
-
-  // overwrite to the input
-  imputer.Impute(input, mappedValue, 0, true); // column wise
-  CheckEqual(input, outputT);
+  imputer.Impute(rowWiseInput, mappedValue, 1, false); // row wise
+
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 5.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 0.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 6.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 9.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 8.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 4.0, 1e-5);
+  BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 8.0, 1e-5);
 }
 
 BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-git mailing list