mlpack  master
increment_policy.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DATA_MAP_POLICIES_INCREMENT_POLICY_HPP
13 #define MLPACK_CORE_DATA_MAP_POLICIES_INCREMENT_POLICY_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include <unordered_map>
17 #include <boost/bimap.hpp>
19 
20 namespace mlpack {
21 namespace data {
30 {
31  public:
32  // typedef of MappedType
33  using MappedType = size_t;
34 
48  template <typename MapType>
50  const size_t dimension,
51  MapType& maps,
52  std::vector<Datatype>& types)
53  {
54  // If this condition is true, either we have no mapping for the given string
55  // or we have no mappings for the given dimension at all. In either case,
56  // we create a mapping.
57  if (maps.count(dimension) == 0 ||
58  maps[dimension].first.left.count(string) == 0)
59  {
60  // This string does not exist yet.
61  size_t& numMappings = maps[dimension].second;
62 
63  // change type of the feature to categorical
64  if (numMappings == 0)
65  types[dimension] = Datatype::categorical;
66 
67  typedef boost::bimap<std::string, MappedType>::value_type PairType;
68  maps[dimension].first.insert(PairType(string, numMappings));
69  return numMappings++;
70  }
71  else
72  {
73  // This string already exists in the mapping.
74  return maps[dimension].first.left.at(string);
75  }
76  }
77 
93  template <typename eT, typename MapType>
94  void MapTokens(const std::vector<std::string>& tokens,
95  size_t& row,
96  arma::Mat<eT>& matrix,
97  MapType& maps,
98  std::vector<Datatype>& types)
99  {
100  auto notNumber = [](const std::string& str)
101  {
102  eT val(0);
103  std::stringstream token;
104  token.str(str);
105  token >> val;
106  return token.fail();
107  };
108 
109  const bool notNumeric = std::any_of(std::begin(tokens),
110  std::end(tokens), notNumber);
111  if (notNumeric)
112  {
113  for (size_t i = 0; i != tokens.size(); ++i)
114  {
115  const eT val = static_cast<eT>(this->MapString(tokens[i], row, maps,
116  types));
117  matrix.at(row, i) = val;
118  }
119  }
120  else
121  {
122  std::stringstream token;
123  for (size_t i = 0; i != tokens.size(); ++i)
124  {
125  token.str(tokens[i]);
126  token >> matrix.at(row, i);
127  token.clear();
128  }
129  }
130  }
131 }; // class IncrementPolicy
132 
133 } // namespace data
134 } // namespace mlpack
135 
136 #endif
IncrementPolicy is used as a helper class for DatasetMapper.
MappedType MapString(const std::string &string, const size_t dimension, MapType &maps, std::vector< Datatype > &types)
Given the string and the dimension to which the it belongs, and the maps and types given by the Datas...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
test cpp RESULT_VARIABLE MEX_RESULT_TRASH OUTPUT_VARIABLE MEX_OUTPUT ERROR_VARIABLE MEX_ERROR_TRASH string(REGEX MATCH"Warning: You are using"MEX_WARNING"${MEX_OUTPUT}") if(MEX_WARNING) string(REGEX REPLACE".*using [a-zA-Z]* version \"([0-9.]*)[^\"]*\".*""\\1"OTHER_COMPILER_VERSION"$
Definition: CMakeLists.txt:18
void MapTokens(const std::vector< std::string > &tokens, size_t &row, arma::Mat< eT > &matrix, MapType &maps, std::vector< Datatype > &types)
MapTokens turns vector of strings into numeric variables and puts them into a given matrix...