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

fix feature index in Dataset::AddFeaturesFrom (fixes #5410) #5650

Merged
merged 2 commits into from
Dec 29, 2022
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
2 changes: 1 addition & 1 deletion src/io/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ void Dataset::AddFeaturesFrom(Dataset* other) {
other->max_bin_by_feature_, other->num_total_features_, -1);
num_total_features_ += other->num_total_features_;
for (size_t i = 0; i < (other->numeric_feature_map_).size(); ++i) {
int feat_ind = numeric_feature_map_[i];
int feat_ind = other->numeric_feature_map_[i];
if (feat_ind > -1) {
numeric_feature_map_.push_back(feat_ind + num_numeric_features_);
} else {
Expand Down
23 changes: 23 additions & 0 deletions tests/python_package_test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,29 @@ def test_add_features_from_different_sources():
assert d1.feature_name == res_feature_names


def test_add_features_does_not_fail_if_initial_dataset_has_zero_informative_features(capsys):

arr_a = np.zeros((100, 1), dtype=np.float32)
arr_b = np.random.normal(size=(100, 5))

dataset_a = lgb.Dataset(arr_a).construct()
expected_msg = (
'[LightGBM] [Warning] There are no meaningful features which satisfy '
'the provided configuration. Decreasing Dataset parameters min_data_in_bin '
'or min_data_in_leaf and re-constructing Dataset might resolve this warning.\n'
)
log_lines = capsys.readouterr().out
assert expected_msg in log_lines

dataset_b = lgb.Dataset(arr_b).construct()

original_handle = dataset_a.handle.value
dataset_a.add_features_from(dataset_b)
assert dataset_a.num_feature() == 6
assert dataset_a.num_data() == 100
assert dataset_a.handle.value == original_handle


def test_cegb_affects_behavior(tmp_path):
X = np.random.random((100, 5))
X[:, [1, 3]] = 0
Expand Down