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 <xgboost/logging.h>
11 
12 #include <vector>
13 #include <string>
14 #include <cstring>
15 #include <iostream>
16 
17 namespace xgboost {
22 class FeatureMap {
23  public:
25  enum Type {
28  kInteger = 2,
29  kFloat = 3
30  };
35  inline void LoadText(std::istream& is) { // NOLINT(*)
36  int fid;
37  std::string fname, ftype;
38  while (is >> fid >> fname >> ftype) {
39  this->PushBack(fid, fname.c_str(), ftype.c_str());
40  }
41  }
48  inline void PushBack(int fid, const char *fname, const char *ftype) {
49  CHECK_EQ(fid, static_cast<int>(names_.size()));
50  names_.emplace_back(fname);
51  types_.push_back(GetType(ftype));
52  }
54  inline void Clear() {
55  names_.clear();
56  types_.clear();
57  }
59  inline size_t Size() const {
60  return names_.size();
61  }
63  inline const char* Name(size_t idx) const {
64  CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
65  return names_[idx].c_str();
66  }
68  Type TypeOf(size_t idx) const {
69  CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
70  return types_[idx];
71  }
72 
73  private:
79  inline static Type GetType(const char* tname) {
80  using std::strcmp;
81  if (!strcmp("i", tname)) return kIndicator;
82  if (!strcmp("q", tname)) return kQuantitive;
83  if (!strcmp("int", tname)) return kInteger;
84  if (!strcmp("float", tname)) return kFloat;
85  LOG(FATAL) << "unknown feature type, use i for indicator and q for quantity";
86  return kIndicator;
87  }
89  std::vector<std::string> names_;
91  std::vector<Type> types_;
92 };
93 } // namespace xgboost
94 #endif // XGBOOST_FEATURE_MAP_H_
void Clear()
clear the feature map
Definition: feature_map.h:54
size_t Size() const
Definition: feature_map.h:59
void PushBack(int fid, const char *fname, const char *ftype)
push back feature map.
Definition: feature_map.h:48
Definition: feature_map.h:29
Type
type of feature maps
Definition: feature_map.h:25
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
Definition: feature_map.h:26
const char * Name(size_t idx) const
Definition: feature_map.h:63
namespace of xgboost
Definition: base.h:102
Definition: feature_map.h:27
void LoadText(std::istream &is)
load feature map from input stream
Definition: feature_map.h:35
Definition: feature_map.h:28
Type TypeOf(size_t idx) const
Definition: feature_map.h:68