[mlpack-git] master: use iteration instead of conv_to in binarize (57ec362)
gitdub at mlpack.org
gitdub at mlpack.org
Mon Jun 6 14:56:19 EDT 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/a0b31abe5ff69117645c664dbeac1476dd5e48f7...2da9c5bac14a00145c757b8139c245913b86e034
>---------------------------------------------------------------
commit 57ec362f0858237fc551801cf465a9954785f35f
Author: Keon Kim <kwk236 at gmail.com>
Date: Tue Jun 7 03:56:19 2016 +0900
use iteration instead of conv_to in binarize
>---------------------------------------------------------------
57ec362f0858237fc551801cf465a9954785f35f
src/mlpack/core/data/binarize.hpp | 63 ++++++++++++++++++++++++++++++++++++---
1 file changed, 59 insertions(+), 4 deletions(-)
diff --git a/src/mlpack/core/data/binarize.hpp b/src/mlpack/core/data/binarize.hpp
index 59d1356..ae7551d 100644
--- a/src/mlpack/core/data/binarize.hpp
+++ b/src/mlpack/core/data/binarize.hpp
@@ -78,26 +78,81 @@ void Binarize(arma::Mat<T>& input,
}
}
+/**
+ * Given an input dataset and threshold, set values greater than threshold to
+ * 1 and values less than or equal to the threshold to 0. This overload applies
+ * the changes to all dimensions.
+ *
+ * @code
+ * arma::mat input = loadData();
+ * arma::mat output;
+ * double threshold = 0.5;
+ *
+ * // Binarize the whole Matrix. All positive values in will be set to 1 and
+ * // the values less than or equal to 0.5 will become 0.
+ * Binarize(input, output, threshold);
+ * @endcode
+ *
+ * @param input Input matrix to Binarize.
+ * @param output Matrix you want to save binarized data into.
+ * @param threshold Threshold can by any number.
+ */
template<typename T>
void Binarize(const arma::Mat<T>& input,
arma::Mat<T>& output,
const double threshold)
{
+ output.copy_size(input);
+
for (size_t i = 0; i < input.n_cols; ++i)
{
- output.row(i) =
- arma::conv_to<arma::Mat<T>>::from(input.row(i) > threshold);
+ for (size_t j = 0; j < input.n_rows; ++j)
+ {
+ if (input(i, j) > threshold)
+ output(i, j) = 1;
+ else
+ output(i, j) = 0;
+ }
}
}
+/**
+ * Given an input dataset and threshold, set values greater than threshold to
+ * 1 and values less than or equal to the threshold to 0. This overload takes
+ * a dimension and applys the changes to the given dimension.
+ *
+ * @code
+ * arma::mat input = loadData();
+ * arma::mat output;
+ * double threshold = 0.5;
+ * size_t dimension = 0;
+ *
+ * // Binarize the first dimension. All positive values in the first dimension
+ * // will be set to 1 and the values less than or equal to 0 will become 0.
+ * Binarize(input, output, threshold, dimension);
+ * @endcode
+ *
+ * @param input Input matrix to Binarize.
+ * @param output Matrix you want to save binarized data into.
+ * @param threshold Threshold can by any number.
+ * @param dimension Feature to apply the Binarize function.
+ */
+
template<typename T>
void Binarize(const arma::Mat<T>& input,
arma::Mat<T>& output,
const double threshold,
const size_t dimension)
{
- output.row(dimension) =
- arma::conv_to<arma::Mat<T>>::from(input.row(dimension) > threshold);
+ output.copy_size(input);
+
+ for (size_t i = 0; i < input.n_cols; ++i)
+ {
+ if (input(dimension, i) > threshold)
+ output(dimension, i) = 1;
+ else
+ output(dimension, i) = 0;
+ }
}
} // namespace data
More information about the mlpack-git
mailing list