5#ifndef XGBOOST_COMMON_CATEGORICAL_H_
6#define XGBOOST_COMMON_CATEGORICAL_H_
13#include "xgboost/span.h"
18using CatBitField = LBitField32;
19using KCatBitField = CLBitField32;
34 return !ft.empty() && ft[fidx] == FeatureType::kCategorical;
37constexpr inline bst_cat_t OutOfRangeCat() {
43 constexpr auto kMaxCat = OutOfRangeCat();
44 static_assert(
static_cast<bst_cat_t>(
static_cast<float>(kMaxCat)) == kMaxCat);
45 static_assert(
static_cast<bst_cat_t>(
static_cast<float>(kMaxCat + 1)) != kMaxCat + 1);
46 static_assert(
static_cast<float>(kMaxCat + 1) == kMaxCat);
47 return cat < 0 || cat >= kMaxCat;
57 if (XGBOOST_EXPECT(InvalidCat(cat),
false)) {
61 auto pos = KCatBitField::ToBitPos(cat);
65 if (pos.int_pos >= cats.size()) {
68 return !s_cats.Check(AsCat(cat));
71inline void InvalidCategory() {
75 auto str = std::to_string(OutOfRangeCat());
76 LOG(FATAL) <<
"Invalid categorical value detected. Categorical value should be non-negative, "
77 "less than total number of categories in training data and less than " +
81inline void CheckMaxCat(
float max_cat,
size_t n_categories) {
82 CHECK_GE(max_cat + 1, n_categories)
83 <<
"Maximum cateogry should not be lesser than the total number of categories.";
90 bool use_one_hot = n_cats < max_cat_to_onehot;
95 XGBOOST_DEVICE bool operator()(FeatureType ft) {
return ft == FeatureType::kCategorical; }
Copyright 2019-2023, XGBoost Contributors.
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition span.h:424
Copyright 2015-2023 by XGBoost Contributors.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
Copyright 2015-2023 by XGBoost Contributors.
XGBOOST_DEVICE bool UseOneHot(uint32_t n_cats, uint32_t max_cat_to_onehot)
Whether should we use onehot encoding for categorical data.
Definition categorical.h:89
XGBOOST_DEVICE bool Decision(common::Span< CatBitField::value_type const > cats, float cat)
Whether should it traverse to left branch of a tree.
Definition categorical.h:55
namespace of xgboost
Definition base.h:90
uint32_t bst_feature_t
Type for data column (feature) index.
Definition base.h:101
int32_t bst_cat_t
Categorical value type.
Definition base.h:99
A non-owning type with auxiliary methods defined for manipulating bits.
Definition bitfield.h:61
Definition categorical.h:94