-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
ENH add support for sample_weight in KBinsDiscretizer(strategy="quantile") #22048
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
Changes from all commits
3522d2d
4366bdc
17e429b
fe6076d
e7d5003
85fe2b7
83a0bba
524cb73
dbe2eb0
6bff3a3
ea5f446
e7e9eee
e78c263
1a417f4
b5677e3
b16321c
0d3919b
30edabc
b600a0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,31 @@ | |
|
||
|
||
@pytest.mark.parametrize( | ||
"strategy, expected", | ||
"strategy, expected, sample_weight", | ||
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. The test will need to be updated with the change that I asked earlier. 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. I updated the test cases that I previously pushed. Unfortunately, I'm doubting on their relevance. Would they be acceptable as is, or should I find more specific cases ? 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. I would like to have 2 specific checks that check the real behaviour of passing weights:
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. Unfortunately, I fail to understand why, the case where The behaviors of 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. Indeed, I just checked: >>> import numpy as np
>>> from sklearn.utils.stats import _weighted_percentile
>>> data = np.random.randn(100)
>>> np.percentile(data, [25, 50, 75])
array([-0.76701739, 0.0604021 , 0.79485777])
>>> [_weighted_percentile(data, np.ones_like(data), p) for p in [25, 50, 75]]
[-0.7855144180141034, 0.05197426163774039, 0.7586492302901591] It seems that this problem has been known for a while but not yet fixed:
Old attempts to fix this problem or related problems:
So for now, we can just comment out this case with a TODO comment with a link to #17370 for explain why this test is commented out. 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. I commented out the faulty test case in 30edabc (l. 139-151) |
||
[ | ||
("uniform", [[0, 0, 0, 0], [1, 1, 1, 0], [2, 2, 2, 1], [2, 2, 2, 2]]), | ||
("kmeans", [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2]]), | ||
("quantile", [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]]), | ||
("uniform", [[0, 0, 0, 0], [1, 1, 1, 0], [2, 2, 2, 1], [2, 2, 2, 2]], None), | ||
("kmeans", [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2]], None), | ||
("quantile", [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]], None), | ||
( | ||
"quantile", | ||
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]], | ||
[1, 1, 2, 1], | ||
), | ||
( | ||
"quantile", | ||
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]], | ||
[1, 1, 1, 1], | ||
), | ||
( | ||
"quantile", | ||
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]], | ||
[0, 1, 1, 1], | ||
), | ||
], | ||
) | ||
def test_fit_transform(strategy, expected): | ||
def test_fit_transform(strategy, expected, sample_weight): | ||
est = KBinsDiscretizer(n_bins=3, encode="ordinal", strategy=strategy) | ||
est.fit(X) | ||
est.fit(X, sample_weight=sample_weight) | ||
assert_array_equal(expected, est.transform(X)) | ||
|
||
|
||
|
@@ -53,6 +68,20 @@ def test_invalid_n_bins(): | |
est.fit_transform(X) | ||
|
||
|
||
def test_invalid_sample_weight(): | ||
# sample_weight parameter is used with wrong strategy (other than quantile) | ||
strategy = ["uniform", "kmeans"] | ||
sample_weight = [1, 1, 1, 1] | ||
for s in strategy: | ||
est = KBinsDiscretizer(n_bins=3, strategy=s) | ||
err_msg = ( | ||
"`sample_weight` was provided but it can be only used with" | ||
f"strategy='quantile'. Got strategy={s!r} instead." | ||
) | ||
with pytest.raises(ValueError, match=err_msg): | ||
est.fit_transform(X, sample_weight=sample_weight) | ||
|
||
|
||
def test_invalid_n_bins_array(): | ||
# Bad shape | ||
n_bins = np.full((2, 4), 2.0) | ||
|
@@ -92,17 +121,40 @@ def test_invalid_n_bins_array(): | |
|
||
|
||
@pytest.mark.parametrize( | ||
"strategy, expected", | ||
"strategy, expected, sample_weight", | ||
[ | ||
("uniform", [[0, 0, 0, 0], [0, 1, 1, 0], [1, 2, 2, 1], [1, 2, 2, 2]]), | ||
("kmeans", [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 2, 2, 2]]), | ||
("quantile", [[0, 0, 0, 0], [0, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]]), | ||
("uniform", [[0, 0, 0, 0], [0, 1, 1, 0], [1, 2, 2, 1], [1, 2, 2, 2]], None), | ||
("kmeans", [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 2, 2, 2]], None), | ||
("quantile", [[0, 0, 0, 0], [0, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]], None), | ||
( | ||
"quantile", | ||
[[0, 0, 0, 0], [0, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]], | ||
[1, 1, 3, 1], | ||
), | ||
( | ||
"quantile", | ||
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]], | ||
[0, 1, 3, 1], | ||
), | ||
# ( | ||
# "quantile", | ||
# [[0, 0, 0, 0], [0, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]], | ||
# [1, 1, 1, 1], | ||
# ), | ||
# | ||
# TODO: This test case above aims to test if the case where an array of | ||
# ones passed in sample_weight parameter is equal to the case when | ||
# sample_weight is None. | ||
# Unfortunately, the behavior of `_weighted_percentile` when | ||
# `sample_weight = [1, 1, 1, 1]` are currently not equivalent. | ||
# This problem has been adressed in issue : | ||
# https://github.com/scikit-learn/scikit-learn/issues/17370 | ||
], | ||
) | ||
def test_fit_transform_n_bins_array(strategy, expected): | ||
def test_fit_transform_n_bins_array(strategy, expected, sample_weight): | ||
est = KBinsDiscretizer( | ||
n_bins=[2, 3, 3, 3], encode="ordinal", strategy=strategy | ||
).fit(X) | ||
).fit(X, sample_weight=sample_weight) | ||
assert_array_equal(expected, est.transform(X)) | ||
|
||
# test the shape of bin_edges_ | ||
|
Uh oh!
There was an error while loading. Please reload this page.