-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Require sample weights to sum to less than 1 when replace = True #61582
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
Open
microslaw
wants to merge
6
commits into
pandas-dev:main
Choose a base branch
from
microslaw:add-df-sample-weight-constraints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+38
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a05beaa
implement the exception
microslaw 92122f3
remove impossible test scenario
microslaw 23926f6
add new tests
microslaw 11df613
update documentation
microslaw 9d2f7d5
Merge branch 'main' into add-df-sample-weight-constraints
microslaw 0b96d4a
Merge branch 'main' into add-df-sample-weight-constraints
microslaw 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
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 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -150,6 +150,14 @@ def sample( | |||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
raise ValueError("Invalid weights: weights sum to zero") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if weights is not None: | ||||||||||||||||||||||||||||
is_max_weight_dominating = size * weights.max() > 1 | ||||||||||||||||||||||||||||
if is_max_weight_dominating and not replace: | ||||||||||||||||||||||||||||
raise ValueError( | ||||||||||||||||||||||||||||
"Invalid weights: If `replace`=False, " | ||||||||||||||||||||||||||||
"total unit probabilities have to be less than 1" | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
Comment on lines
+153
to
+159
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.
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return random_state.choice(obj_len, size=size, replace=replace, p=weights).astype( | ||||||||||||||||||||||||||||
np.intp, copy=False | ||||||||||||||||||||||||||||
) |
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 |
---|---|---|
|
@@ -113,9 +113,6 @@ def test_sample_invalid_weight_lengths(self, obj): | |
with pytest.raises(ValueError, match=msg): | ||
obj.sample(n=3, weights=[0.5] * 11) | ||
|
||
with pytest.raises(ValueError, match="Fewer non-zero entries in p than size"): | ||
obj.sample(n=4, weights=Series([0, 0, 0.2])) | ||
|
||
def test_sample_negative_weights(self, obj): | ||
# Check won't accept negative weights | ||
bad_weights = [-0.1] * 10 | ||
|
@@ -137,6 +134,28 @@ def test_sample_inf_weights(self, obj): | |
with pytest.raises(ValueError, match=msg): | ||
obj.sample(n=3, weights=weights_with_ninf) | ||
|
||
def test_sample_unit_probabilities_raises(self, obj): | ||
# GH#61516 | ||
high_variance_weights = [1] * 10 | ||
high_variance_weights[0] = 100 | ||
msg = ( | ||
"Invalid weights: If `replace`=False, " | ||
"total unit probabilities have to be less than 1" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
obj.sample(n=2, weights=high_variance_weights, replace=False) | ||
|
||
# edge case, n*max(weights)/sum(weights) == 1 | ||
edge_variance_weights = [1] * 10 | ||
edge_variance_weights[0] = 9 | ||
# should not raise | ||
obj.sample(n=2, weights=edge_variance_weights, replace=False) | ||
|
||
low_variance_weights = [1] * 10 | ||
low_variance_weights[0] = 8 | ||
# should not raise | ||
obj.sample(n=2, weights=low_variance_weights, replace=False) | ||
Comment on lines
+148
to
+157
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. Can you make these two separate tests. |
||
|
||
def test_sample_zero_weights(self, obj): | ||
# All zeros raises errors | ||
|
||
|
Oops, something went wrong.
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.
Can you add "See the Notes below for more details." and then add a bit more detail in the notes section. I can give it a shot if you'd prefer.