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 {
26 kIndicator = 0,
27 kQuantitive = 1,
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_
xgboost::FeatureMap::kQuantitive
@ kQuantitive
Definition: feature_map.h:27
xgboost::FeatureMap::kInteger
@ kInteger
Definition: feature_map.h:28
xgboost::FeatureMap::kIndicator
@ kIndicator
Definition: feature_map.h:26
xgboost::FeatureMap::Clear
void Clear()
clear the feature map
Definition: feature_map.h:54
xgboost::FeatureMap::Type
Type
type of feature maps
Definition: feature_map.h:25
xgboost::FeatureMap
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
xgboost::FeatureMap::kFloat
@ kFloat
Definition: feature_map.h:29
xgboost::FeatureMap::LoadText
void LoadText(std::istream &is)
load feature map from input stream
Definition: feature_map.h:35
xgboost::FeatureMap::TypeOf
Type TypeOf(size_t idx) const
Definition: feature_map.h:68
xgboost::FeatureMap::Size
size_t Size() const
Definition: feature_map.h:59
xgboost::FeatureMap::Name
const char * Name(size_t idx) const
Definition: feature_map.h:63
xgboost::FeatureMap::PushBack
void PushBack(int fid, const char *fname, const char *ftype)
push back feature map.
Definition: feature_map.h:48
xgboost
namespace of xgboost
Definition: base.h:110