Skip to content
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

Add optional weights parameter to random.sample() #84721

Closed
rhettinger opened this issue May 6, 2020 · 4 comments
Closed

Add optional weights parameter to random.sample() #84721

rhettinger opened this issue May 6, 2020 · 4 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 40541
Nosy @tim-one, @rhettinger, @mdickinson
PRs
  • bpo-40541: Add optional *counts* parameter to random.sample() #19970
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-08.14:53:54.309>
    created_at = <Date 2020-05-06.22:35:20.284>
    labels = ['type-feature', 'library', '3.9']
    title = 'Add optional weights parameter to random.sample()'
    updated_at = <Date 2020-05-08.14:53:54.309>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-05-08.14:53:54.309>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-08.14:53:54.309>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-05-06.22:35:20.284>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40541
    keywords = ['patch']
    message_count = 4.0
    messages = ['368307', '368333', '368374', '368445']
    nosy_count = 3.0
    nosy_names = ['tim.peters', 'rhettinger', 'mark.dickinson']
    pr_nums = ['19970']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40541'
    versions = ['Python 3.9']

    @rhettinger
    Copy link
    Contributor Author

    I've seen multiple requests for this and it isn't obvious how to do it with the existing tooling.

    The example currently given in the recipes section isn't scalable because it involves expanding the population into a big list with repeated elements:

          sample(['x', 'x', 'x', 'x', 'y', 'y', 'z'], k=5)

    Example task: Given an urn with 8 red balls, 2 white balls, and 3 green balls, choose ten without replacement:

        >>> population = ['red', 'blue', 'green']
        >>> weights =    [  8,      5,      3   ]
        >>> sample(population, weights=weights, k=10)
        ['red', 'green', 'blue', 'red', 'green', 'blue', 'red', 'blue', 'red', 'blue']

    I could also add *cum_weights* as an optional optimization but think it best to wait until someone asks for it ;-)

    @rhettinger rhettinger added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 6, 2020
    @mdickinson
    Copy link
    Member

    +1 for the functionality.

    How about "counts" instead of "weights"?

    I found the name "weights" misleading - my first thought was that this would be doing a weighted sampling without replacement (like NumPy's random.choice(..., replace=False, p=weights)).

    Of course, now I've read the docs, I know better.

    @rhettinger
    Copy link
    Contributor Author

    How about "counts" instead of "weights"?

    That makes sense.

    @rhettinger
    Copy link
    Contributor Author

    New changeset 81a5fc3 by Raymond Hettinger in branch 'master':
    bpo-40541: Add optional *counts* parameter to random.sample() (GH-19970)
    81a5fc3

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants