Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XGBoosterGetNumFeature #5856

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demo/c-api/c-api-demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ int main(int argc, char** argv) {
printf("%s\n", eval_result);
}

bst_ulong num_feature = 0;
safe_xgboost(XGBoosterGetNumFeature(booster, &num_feature));
printf("num_feature: %llu\n", num_feature);

// predict
bst_ulong out_len = 0;
const float* out_result = NULL;
Expand Down
8 changes: 8 additions & 0 deletions include/xgboost/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ XGB_DLL int XGBoosterSetParam(BoosterHandle handle,
const char *name,
const char *value);

/*!
* \brief get number of features
* \param out number of features
* \return 0 when success, -1 when failure happens
*/
XGB_DLL int XGBoosterGetNumFeature(BoosterHandle handle,
bst_ulong *out);

/*!
* \brief update the model in one round using dtrain
* \param handle handle
Expand Down
6 changes: 6 additions & 0 deletions include/xgboost/learner.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ class Learner : public Model, public Configurable, public rabit::Serializable {
*/
virtual void SetParam(const std::string& key, const std::string& value) = 0;

/*!
* \brief Get the number of features of the booster.
* \return number of features
*/
virtual uint32_t GetNumFeature() = 0;

/*!
* \brief Set additional attribute to the Booster.
*
Expand Down
8 changes: 8 additions & 0 deletions src/c_api/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ XGB_DLL int XGBoosterSetParam(BoosterHandle handle,
API_END();
}

XGB_DLL int XGBoosterGetNumFeature(BoosterHandle handle,
xgboost::bst_ulong *out) {
API_BEGIN();
CHECK_HANDLE();
*out = static_cast<Learner*>(handle)->GetNumFeature();
API_END();
}

XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const* json_parameters) {
API_BEGIN();
CHECK_HANDLE();
Expand Down
4 changes: 4 additions & 0 deletions src/learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ class LearnerConfiguration : public Learner {
}
}

uint32_t GetNumFeature() override {
return learner_model_param_.num_feature;
}

void SetAttr(const std::string& key, const std::string& value) override {
attributes_[key] = value;
mparam_.contain_extra_attrs = 1;
Expand Down
7 changes: 6 additions & 1 deletion tests/cpp/c_api/test_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ TEST(CAPI, ConfigIO) {

TEST(CAPI, JsonModelIO) {
size_t constexpr kRows = 10;
size_t constexpr kCols = 10;
dmlc::TemporaryDirectory tempdir;

auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatrix();
auto p_dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatrix();
std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
std::vector<bst_float> labels(kRows);
for (size_t i = 0; i < labels.size(); ++i) {
Expand All @@ -132,6 +133,10 @@ TEST(CAPI, JsonModelIO) {
XGBoosterSaveModel(handle, modelfile_0.c_str());
XGBoosterLoadModel(handle, modelfile_0.c_str());

bst_ulong num_feature {0};
ASSERT_EQ(XGBoosterGetNumFeature(handle, &num_feature), 0);
ASSERT_EQ(num_feature, kCols);

std::string modelfile_1 = tempdir.path + "/model_1.json";
XGBoosterSaveModel(handle, modelfile_1.c_str());

Expand Down