-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
BUG Fixes sample weights when there are missing values in DecisionTrees #26376
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
Merged
ogrisel
merged 7 commits into
scikit-learn:main
from
thomasjpfan:missing_values_regression_sample_weights
May 16, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef48585
BUG Fixes sample weights when there are missing values in Regression …
thomasjpfan b385c45
DOC Adds PR number
thomasjpfan c0365a7
REV Less diffs
thomasjpfan 32f0030
TST Adds test for non-uniform sample weights
thomasjpfan d595565
TST Stronger test
thomasjpfan 3b4dbc1
TST Speed up test
thomasjpfan a32d7bb
DOC Adjust comments
thomasjpfan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2549,7 +2549,8 @@ def test_missing_values_poisson(): | |
(datasets.make_classification, DecisionTreeClassifier), | ||
], | ||
) | ||
def test_missing_values_is_resilience(make_data, Tree): | ||
@pytest.mark.parametrize("sample_weight_train", [None, "ones"]) | ||
def test_missing_values_is_resilience(make_data, Tree, sample_weight_train): | ||
"""Check that trees can deal with missing values and have decent performance.""" | ||
|
||
rng = np.random.RandomState(0) | ||
|
@@ -2563,15 +2564,18 @@ def test_missing_values_is_resilience(make_data, Tree): | |
X_missing, y, random_state=0 | ||
) | ||
|
||
if sample_weight_train == "ones": | ||
sample_weight_train = np.ones(X_missing_train.shape[0]) | ||
|
||
# Train tree with missing values | ||
tree_with_missing = Tree(random_state=rng) | ||
tree_with_missing.fit(X_missing_train, y_train) | ||
tree_with_missing.fit(X_missing_train, y_train, sample_weight=sample_weight_train) | ||
score_with_missing = tree_with_missing.score(X_missing_test, y_test) | ||
|
||
# Train tree without missing values | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) | ||
tree = Tree(random_state=rng) | ||
tree.fit(X_train, y_train) | ||
tree.fit(X_train, y_train, sample_weight=sample_weight_train) | ||
score_without_missing = tree.score(X_test, y_test) | ||
|
||
# Score is still 90 percent of the tree's score that had no missing values | ||
|
@@ -2601,3 +2605,32 @@ def test_missing_value_is_predictive(): | |
|
||
assert tree.score(X_train, y_train) >= 0.85 | ||
assert tree.score(X_test, y_test) >= 0.85 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"make_data, Tree", | ||
[ | ||
(datasets.make_regression, DecisionTreeRegressor), | ||
(datasets.make_classification, DecisionTreeClassifier), | ||
], | ||
) | ||
def test_sample_weight_non_uniform(make_data, Tree): | ||
"""Check sample weight is correctly handled with missing values.""" | ||
rng = np.random.RandomState(0) | ||
n_samples, n_features = 1000, 10 | ||
X, y = make_data(n_samples=n_samples, n_features=n_features, random_state=rng) | ||
|
||
# Create dataset with missing values | ||
X[rng.choice([False, True], size=X.shape, p=[0.9, 0.1])] = np.nan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat idiom :) |
||
|
||
# Zero sample weight is the same as removing the sample | ||
sample_weight = np.ones(X.shape[0]) | ||
sample_weight[::2] = 0.0 | ||
|
||
tree_with_sw = Tree(random_state=0) | ||
tree_with_sw.fit(X, y, sample_weight=sample_weight) | ||
|
||
tree_samples_removed = Tree(random_state=0) | ||
tree_samples_removed.fit(X[1::2, :], y[1::2]) | ||
|
||
assert_allclose(tree_samples_removed.predict(X), tree_with_sw.predict(X)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also test the behavior when using non-uniform weights?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, an easier test than non-uniform weight is to assign 0-weight to some specific samples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth having a separated test for checking an equivalence.