BUG: fix Generator.choice ignoring the shuffle arg#31215
Open
Anarion-zuo wants to merge 2 commits intonumpy:mainfrom
Open
BUG: fix Generator.choice ignoring the shuffle arg#31215Anarion-zuo wants to merge 2 commits intonumpy:mainfrom
Anarion-zuo wants to merge 2 commits intonumpy:mainfrom
Conversation
Signed-off-by: aaronzuo <anarionzuo@outlook.com>
Member
|
Oh I missed the AI disclosure since it looked identical. The PR description still doesn't look human-written though... |
Contributor
Author
The written text can be reused. No need to rewrite things again. AI is great for us not fluent in Eng... |
Member
|
We prefer to talk to humans, and don't like wasting our time reading very wordy descriptions. I'm sure you understand. |
Signed-off-by: aaronzuo <anarionzuo@outlook.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR summary
Closes #31210.
This PR fixes
Generator.choice(..., replace=False, p=..., shuffle=...)so thatshuffleis respected in the weighted sampling path.Right now, when
replace=Falseandpis provided,shuffle=Trueandshuffle=Falseproduce the same ordering. That is different from the unweighted path, whereshuffledoes affect the output order. In practice, this means the draw order can reflect the internal weighted sampling order instead of being randomized, which is exactly the behavior reported in the issue.The code change is small: after the weighted no-replacement sampler finishes building the selected indices, it now applies the same final shuffle step that is already used in the unweighted no-replacement path when
shuffle=True.I also added a regression test to check that:
shuffle=Trueshuffle=FalseTests run in Docker:
spin test numpy/random/tests/test_generator_mt19937.py -- -q -k "test_choice_nonuniform_noreplace or test_choice_nonuniform_noreplace_shuffle or test_choice_uniform_noreplace"spin build --clean -- -Dallow-noblas=true -Dcpu-baseline=none -Dcpu-dispatch=nonespin test -- --durations=10 --timeout=600The focused random tests passed. The full test run completed except for the known environment-specific failure in
numpy/_core/tests/test_cpu_features.py::Test_X86_Features::test_features.I also reproduced the issue visually using the same kind of setup described in the issue report and confirmed that the fixed
shuffle=Truebehavior removes the order correlation, while preserving the sampled set.AI Disclosure
I used ChatGPT in preparing this pull request.
AI was used for grammar and wording fixes in PR text, and earlier I did not fully disclose that use. I’m sorry about that. It was not intentional.
For completeness: AI was also used to help inspect the code path and help draft text while I was working on the issue. The actual code changes, testing, and submission are being handled by me.