xgboost
feature_map.h
前往此文件的文档。
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  kCategorical = 4
31  };
36  inline void LoadText(std::istream& is) { // NOLINT(*)
37  int fid;
38  std::string fname, ftype;
39  while (is >> fid >> fname >> ftype) {
40  this->PushBack(fid, fname.c_str(), ftype.c_str());
41  }
42  }
49  inline void PushBack(int fid, const char *fname, const char *ftype) {
50  CHECK_EQ(fid, static_cast<int>(names_.size()));
51  names_.emplace_back(fname);
52  types_.push_back(GetType(ftype));
53  }
55  inline void Clear() {
56  names_.clear();
57  types_.clear();
58  }
60  inline size_t Size() const {
61  return names_.size();
62  }
64  inline const char* Name(size_t idx) const {
65  CHECK_LT(idx, names_.size()) << "特征映射的特征索引超出界限";
66  return names_[idx].c_str();
67  }
69  Type TypeOf(size_t idx) const {
70  CHECK_LT(idx, names_.size()) << "特征映射的特征索引超出界限";
71  return types_[idx];
72  }
73 
74  private
80  inline static Type GetType(const char* tname) {
81  using std::strcmp;
82  if (!strcmp("i", tname)) return kIndicator;
83  if (!strcmp("q", tname)) return kQuantitive;
84  if (!strcmp("int", tname)) return kInteger;
85  if (!strcmp("float", tname)) return kFloat;
86  if (!strcmp("c", tname)) return kCategorical;
87  LOG(FATAL) << "未知特征类型,请使用 i 表示指示器,q 表示数量";
88  return kIndicator;
89  }
91  std::vector<std::string> names_;
93  std::vector<Type> types_;
94 };
95 } // namespace xgboost
96 #endif // XGBOOST_FEATURE_MAP_H_
特征映射数据结构,用于帮助文本模型转储。TODO(tqchen) 考虑使其更轻量级...
定义: feature_map.h:22
void Clear()
清除特征映射
定义: feature_map.h:55
void LoadText(std::istream &is)
从输入流加载特征映射
定义: feature_map.h:36
Type
特征映射的类型
定义: feature_map.h:25
@ kQuantitive
定义: feature_map.h:27
@ kFloat
定义: feature_map.h:29
@ kIndicator
定义: feature_map.h:26
@ kInteger
定义: feature_map.h:28
@ kCategorical
定义: feature_map.h:30
void PushBack(int fid, const char *fname, const char *ftype)
添加特征映射。
定义: feature_map.h:49
const char * Name(size_t idx) const
定义: feature_map.h:64
Type TypeOf(size_t idx) const
定义: feature_map.h:69
size_t Size() const
定义: feature_map.h:60
用于多目标树的核心数据结构。
定义: base.h:89