np.random.choice returns out of range index on masked array prob.
Open
Nobody has claimed this yet.
00 - Bug
component: numpy.ma
component: numpy.random
- Dominant language
- Python
- Stars
- 32.8k
- Forks
- 12.8k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 197
Description
import numpy as np
a = np.array([0.1, 0.2, 0.4, 0.3])
masked_a = np.ma.masked_array(a, [0, 0, 1, 1])
counter = np.zeros(len(a) + 1)
while True:
action = np.random.choice(len(a), p=masked_a)
counter[action] += 1
print(counter / counter.sum())
# [0.09931198 0.20076697 0. 0. 0.69992105]
It shouldn't returns an index > 3, but when the last element of masked array is true, it may return index = 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the np.random.choice entry point and reproduce the behavior with the masked_a example from the issue, including a mask on the final element. Trace how the masked probability array is handled and verify that repeated sampling never returns an index outside the input range.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100