dataset_mapper.hpp
Go to the documentation of this file.
1 
15 #ifndef MLPACK_CORE_DATA_DATASET_INFO_HPP
16 #define MLPACK_CORE_DATA_DATASET_INFO_HPP
17 
18 #include <mlpack/prereqs.hpp>
19 #include <unordered_map>
20 
22 
23 namespace mlpack {
24 namespace data {
25 
40 template<typename PolicyType, typename InputType = std::string>
42 {
43  public:
49  explicit DatasetMapper(const size_t dimensionality = 0);
50 
56  explicit DatasetMapper(PolicyType& policy, const size_t dimensionality = 0);
57 
65  template<typename T>
66  void MapFirstPass(const InputType& input, const size_t dimension);
67 
78  template<typename T>
79  T MapString(const InputType& input,
80  const size_t dimension);
81 
100  template<typename T>
101  const InputType& UnmapString(const T value,
102  const size_t dimension,
103  const size_t unmappingIndex = 0) const;
104 
108  template<typename T>
109  size_t NumUnmappings(const T value, const size_t dimension) const;
110 
120  typename PolicyType::MappedType UnmapValue(const InputType& input,
121  const size_t dimension);
122 
124  Datatype Type(const size_t dimension) const;
126  Datatype& Type(const size_t dimension);
127 
132  size_t NumMappings(const size_t dimension) const;
133 
140  size_t Dimensionality() const;
141 
145  template<typename Archive>
146  void serialize(Archive& ar, const unsigned int /* version */)
147  {
148  ar & BOOST_SERIALIZATION_NVP(types);
149  ar & BOOST_SERIALIZATION_NVP(maps);
150  }
151 
153  const PolicyType& Policy() const;
154 
156  PolicyType& Policy();
158  void Policy(PolicyType&& policy);
159 
160  private:
162  std::vector<Datatype> types;
163 
164  // Forward mapping type.
165  using ForwardMapType = typename std::unordered_map<InputType, typename
166  PolicyType::MappedType>;
167 
168  // Reverse mapping type. Multiple inputs may map to a single output, hence
169  // the need for std::vector.
170  using ReverseMapType = std::unordered_map<typename PolicyType::MappedType,
171  std::vector<InputType>>;
172 
173  // Mappings from strings to integers.
174  // Map entries will only exist for dimensions that are categorical.
175  // MapType = map<dimension, pair<bimap<string, MappedType>, numMappings>>
176  using MapType = std::unordered_map<size_t, std::pair<ForwardMapType,
177  ReverseMapType>>;
178 
180  MapType maps;
181 
183  // mapped to the maps object. It is used in MapString() and MapTokens().
184  PolicyType policy;
185 };
186 
187 // Use typedef to provide backward compatibility
189 
190 } // namespace data
191 } // namespace mlpack
192 
193 #include "dataset_mapper_impl.hpp"
194 
195 #endif
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
DatasetMapper(const size_t dimensionality=0)
Create the DatasetMapper object with the given dimensionality.
void serialize(Archive &ar, const unsigned int)
Serialize the dataset information.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
const InputType & UnmapString(const T value, const size_t dimension, const size_t unmappingIndex=0) const
Return the input that corresponds to a given value in a given dimension.
PolicyType::MappedType UnmapValue(const InputType &input, const size_t dimension)
Return the value that corresponds to a given input in a given dimension.
Datatype Type(const size_t dimension) const
Return the type of a given dimension (numeric or categorical).
size_t NumMappings(const size_t dimension) const
Get the number of mappings for a particular dimension.
void MapFirstPass(const InputType &input, const size_t dimension)
Preprocessing: during a first pass of the data, pass the input on to the MapPolicy if they are needed...
size_t NumUnmappings(const T value, const size_t dimension) const
Get the number of possible unmappings for a string in a given dimension.
Datatype
The Datatype enum specifies the types of data mlpack algorithms can use.
Definition: datatype.hpp:24
size_t Dimensionality() const
Get the dimensionality of the DatasetMapper object (that is, how many dimensions it has information f...
const PolicyType & Policy() const
Return the policy of the mapper.