-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
FIX force node values outside of [0, 1] range for monotonically constraints classification trees #27639
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
glemaitre
merged 18 commits into
scikit-learn:main
from
samronsin:fix-monotonic-trees-classification-clipping
Dec 7, 2023
Merged
FIX force node values outside of [0, 1] range for monotonically constraints classification trees #27639
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dd5ae50
Fix node values outside of [0, 1] range
samronsin 283a94e
fix linter
glemaitre d74b012
Fix scale in ClassificationCriterion clip_node_value
samronsin c2e3f59
Remove some numerical noise in clipped values and subsequent predicte…
samronsin 8ec1ca3
Add test on the sum of predicted probabilities
samronsin 2f72328
Fix linter
samronsin 3448e1d
Code format
samronsin b52776a
Use assert_allclose
samronsin 137d249
Fix linting
samronsin 0e82410
Fix format
samronsin 0d28f8f
Make node_value method in ClassificationCriterion compute probabiliti…
samronsin aa88ff4
Fix classification trees export tests
samronsin f4f6616
Clean up
samronsin 37ad0a9
Inline clip_node_value
samronsin 381b106
Fix handling of proportion attribute in _BaseTreeExporter
samronsin 3e1fdfb
Fix export_text
samronsin 5029a16
Black
samronsin 924066f
Remove obsolete predict_proba normalisation in DecisionTreeClassifier…
samronsin 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
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
ExtraTreeClassifier, | ||
ExtraTreeRegressor, | ||
) | ||
from sklearn.utils._testing import assert_allclose | ||
from sklearn.utils.fixes import CSC_CONTAINERS | ||
|
||
TREE_CLASSIFIER_CLASSES = [DecisionTreeClassifier, ExtraTreeClassifier] | ||
|
@@ -77,15 +78,20 @@ def test_monotonic_constraints_classifications( | |
if sparse_splitter: | ||
X_train = csc_container(X_train) | ||
est.fit(X_train, y_train) | ||
y = est.predict_proba(X_test)[:, 1] | ||
proba_test = est.predict_proba(X_test) | ||
|
||
assert np.logical_and( | ||
proba_test >= 0.0, proba_test <= 1.0 | ||
).all(), "Probability should always be in [0, 1] range." | ||
assert_allclose(proba_test.sum(axis=1), 1.0) | ||
Comment on lines
+83
to
+86
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. Do those tests fail without this PR? (They should!) 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. assert np.logical_and(
proba_test >= 0.0, proba_test <= 1.0
).all(), "Probability should always be in [0, 1] range." definitely fails. assert_allclose(proba_test.sum(axis=1), 1.0) doesn't though as we made sure the sum is |
||
|
||
# Monotonic increase constraint, it applies to the positive class | ||
assert np.all(est.predict_proba(X_test_0incr)[:, 1] >= y) | ||
assert np.all(est.predict_proba(X_test_0decr)[:, 1] <= y) | ||
assert np.all(est.predict_proba(X_test_0incr)[:, 1] >= proba_test[:, 1]) | ||
assert np.all(est.predict_proba(X_test_0decr)[:, 1] <= proba_test[:, 1]) | ||
|
||
# Monotonic decrease constraint, it applies to the positive class | ||
assert np.all(est.predict_proba(X_test_1incr)[:, 1] <= y) | ||
assert np.all(est.predict_proba(X_test_1decr)[:, 1] >= y) | ||
assert np.all(est.predict_proba(X_test_1incr)[:, 1] <= proba_test[:, 1]) | ||
assert np.all(est.predict_proba(X_test_1decr)[:, 1] >= proba_test[:, 1]) | ||
|
||
|
||
@pytest.mark.parametrize("TreeRegressor", TREE_BASED_REGRESSOR_CLASSES) | ||
|
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.
This is indeed much cleaner.