mlpack 3.4.2
median_imputation.hpp
Go to the documentation of this file.
1
12#ifndef MLPACK_CORE_DATA_IMPUTE_STRATEGIES_MEDIAN_IMPUTATION_HPP
13#define MLPACK_CORE_DATA_IMPUTE_STRATEGIES_MEDIAN_IMPUTATION_HPP
14
15#include <mlpack/prereqs.hpp>
16
17namespace mlpack {
18namespace data {
24template <typename T>
26{
27 public:
38 void Impute(arma::Mat<T>& input,
39 const T& mappedValue,
40 const size_t dimension,
41 const bool columnMajor = true)
42 {
43 using PairType = std::pair<size_t, size_t>;
44 // dimensions and indexes are saved as pairs inside this vector.
45 std::vector<PairType> targets;
46 // good elements are kept inside this vector.
47 std::vector<double> elemsToKeep;
48
49 if (columnMajor)
50 {
51 for (size_t i = 0; i < input.n_cols; ++i)
52 {
53 if (input(dimension, i) == mappedValue ||
54 std::isnan(input(dimension, i)))
55 {
56 targets.emplace_back(dimension, i);
57 }
58 else
59 {
60 elemsToKeep.push_back(input(dimension, i));
61 }
62 }
63 }
64 else
65 {
66 for (size_t i = 0; i < input.n_rows; ++i)
67 {
68 if (input(i, dimension) == mappedValue ||
69 std::isnan(input(i, dimension)))
70 {
71 targets.emplace_back(i, dimension);
72 }
73 else
74 {
75 elemsToKeep.push_back(input(i, dimension));
76 }
77 }
78 }
79
80 // calculate median
81 const double median = arma::median(arma::vec(elemsToKeep));
82
83 for (const PairType& target : targets)
84 {
85 input(target.first, target.second) = median;
86 }
87 }
88}; // class MedianImputation
89
90} // namespace data
91} // namespace mlpack
92
93#endif
This is a class implementation of simple median imputation.
void Impute(arma::Mat< T > &input, const T &mappedValue, const size_t dimension, const bool columnMajor=true)
Impute function searches through the input looking for mappedValue and replaces it with the median of...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.