xgboost
feature_map.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_FEATURE_MAP_H_
8 #define XGBOOST_FEATURE_MAP_H_
9 
10 #include <vector>
11 #include <string>
12 #include <cstring>
13 #include <iostream>
14 
15 namespace xgboost {
20 class FeatureMap {
21  public:
23  enum Type {
26  kInteger = 2,
27  kFloat = 3
28  };
33  inline void LoadText(std::istream& is) { // NOLINT(*)
34  int fid;
35  std::string fname, ftype;
36  while (is >> fid >> fname >> ftype) {
37  this->PushBack(fid, fname.c_str(), ftype.c_str());
38  }
39  }
46  inline void PushBack(int fid, const char *fname, const char *ftype) {
47  CHECK_EQ(fid, static_cast<int>(names_.size()));
48  names_.emplace_back(fname);
49  types_.push_back(GetType(ftype));
50  }
52  inline void Clear() {
53  names_.clear();
54  types_.clear();
55  }
57  inline size_t Size() const {
58  return names_.size();
59  }
61  inline const char* Name(size_t idx) const {
62  CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
63  return names_[idx].c_str();
64  }
66  Type type(size_t idx) const {
67  CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
68  return types_[idx];
69  }
70 
71  private:
77  inline static Type GetType(const char* tname) {
78  using std::strcmp;
79  if (!strcmp("i", tname)) return kIndicator;
80  if (!strcmp("q", tname)) return kQuantitive;
81  if (!strcmp("int", tname)) return kInteger;
82  if (!strcmp("float", tname)) return kFloat;
83  LOG(FATAL) << "unknown feature type, use i for indicator and q for quantity";
84  return kIndicator;
85  }
87  std::vector<std::string> names_;
89  std::vector<Type> types_;
90 };
91 } // namespace xgboost
92 #endif // XGBOOST_FEATURE_MAP_H_
void Clear()
clear the feature map
Definition: feature_map.h:52
size_t Size() const
Definition: feature_map.h:57
void PushBack(int fid, const char *fname, const char *ftype)
push back feature map.
Definition: feature_map.h:46
Definition: feature_map.h:27
Type
type of feature maps
Definition: feature_map.h:23
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:20
Definition: feature_map.h:24
const char * Name(size_t idx) const
Definition: feature_map.h:61
namespace of xgboost
Definition: base.h:79
Definition: feature_map.h:25
void LoadText(std::istream &is)
load feature map from input stream
Definition: feature_map.h:33
Type type(size_t idx) const
Definition: feature_map.h:66
Definition: feature_map.h:26