Debian Codegraph @

src/include/xgboost/feature_map.h

raw symbols SCIP
1 /*!
2 * Copyright 2014-2021 by Contributors
3 * \file feature_map.h
4 * \brief Feature map data structure to help visualization and model dump.
5 * \author Tianqi Chen
6 */
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 {
18 /*!
19 * \brief Feature map data structure to help text model dump.
20 * TODO(tqchen) consider make it even more lightweight.
21 */
22 class FeatureMap {
23 public:
24 /*! \brief type of feature maps */
25 enum Type {
26 kIndicator = 0,
27 kQuantitive = 1,
28 kInteger = 2,
29 kFloat = 3,
30 kCategorical = 4
31 };
32 /*!
33 * \brief load feature map from input stream
34 * \param is Input text stream
35 */
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 }
43 /*!
44 * \brief push back feature map.
45 * \param fid The feature index.
46 * \param fname The feature name.
47 * \param ftype The feature type.
48 */
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 }
54 /*! \brief clear the feature map */
55 inline void Clear() {
56 names_.clear();
57 types_.clear();
58 }
59 /*! \return number of known features */
60 inline size_t Size() const {
61 return names_.size();
62 }
63 /*! \return name of specific feature */
64 inline const char* Name(size_t idx) const {
65 CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
66 return names_[idx].c_str();
67 }
68 /*! \return type of specific feature */
69 Type TypeOf(size_t idx) const {
70 CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound";
71 return types_[idx];
72 }
73
74 private:
75 /*!
76 * \return feature type enum given name.
77 * \param tname The type name.
78 * \return The translated type.
79 */
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) << "unknown feature type, use i for indicator and q for quantity";
88 return kIndicator;
89 }
90 /*! \brief name of the feature */
91 std::vector<std::string> names_;
92 /*! \brief type of the feature */
93 std::vector<Type> types_;
94 };
95 } // namespace xgboost
96 #endif // XGBOOST_FEATURE_MAP_H_