Is `wsample` a proper name for a function like this?

Discussion in 'Python' started by Satoru Logic, Oct 11, 2012.

  1. Satoru Logic

    Satoru Logic Guest

    I came across a function named `wsample` in a `utils` package of my workplace recently.

    The "w" in `wsample` stands for `weighted`, and it randomly selects an element from a container according to relative weights of all the elements.

    In most articles and codes I saw online, a function like this is often named `weighted_random_choice`, which sounds *correct* to me.
    So when I saw this `wsample` function, I considered it a improper name.
    Because `wsample`makes me think of `random.sample`, which returns a list of randomly generated elements, not a element.
     
    Satoru Logic, Oct 11, 2012
    #1
    1. Advertising

  2. Satoru Logic

    Terry Reedy Guest

    On 10/10/2012 11:33 PM, Satoru Logic wrote:
    > I came across a function named `wsample` in a `utils` package of my
    > workplace recently.
    >
    > The "w" in `wsample` stands for `weighted`, and it randomly selects
    > an element from a container according to relative weights of all the
    > elements.


    I agree that wt_select for weighted select might be better for a sample
    size of 1 ;-).

    > In most articles and codes I saw online, a function like this is
    > often named `weighted_random_choice`, which sounds *correct* to me.


    Easier to understand, at least the first time, harder to write. Be glad
    the author did not use 'ws' as would have once been common.

    > So when I saw this `wsample` function, I considered it a improper
    > name. Because `wsample`makes me think of `random.sample`, which
    > returns a list of randomly generated elements, not a element.


    --
    Terry Jan Reedy
     
    Terry Reedy, Oct 11, 2012
    #2
    1. Advertising

  3. Satoru Logic

    alex23 Guest

    On Oct 11, 1:33 pm, Satoru Logic <> wrote:
    > I came across a function named `wsample` in a `utils` package
    > of my workplace recently.
    >
    > The "w" in `wsample` stands for `weighted`, and it randomly
    > selects an element from a container according to relative
    > weights of all the elements.
    >
    > So when I saw this `wsample` function, I considered it a improper name.


    I tend to agree. I like descriptive names for functions that don't
    assume familiarity. If you're unhappy with a function's name and
    you're unable to modify the module it belongs to, you can always
    rebind it yourself:

    from utils import wsample as weighted_random_choice

    If I'm then going to use that function heavily elsewhere, I _might_
    rebind it, but always within the scope that is going to use the
    abbreviated binding:

    wrc = weighted_random_choice
    <lots of lines of code using wrc>
     
    alex23, Oct 11, 2012
    #3
  4. On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:

    > I came across a function named `wsample` in a `utils` package of my
    > workplace recently.
    >
    > The "w" in `wsample` stands for `weighted`, and it randomly selects an
    > element from a container according to relative weights of all the
    > elements.
    >
    > In most articles and codes I saw online, a function like this is often
    > named `weighted_random_choice`, which sounds *correct* to me. So when I
    > saw this `wsample` function, I considered it a improper name. Because
    > `wsample`makes me think of `random.sample`, which returns a list of
    > randomly generated elements, not a element.


    You can have a sample size of one.

    wsample sounds fine to me. weighted_random_choice is okay too. It depends
    whether you value brevity over explicitness. Explicit is good, but some
    names are just too long.

    If this were my code base, I would probably go for weighted_sample
    without mentioning "random" in the name, the reasoning being that samples
    are almost always random so explicitly saying so doesn't help much.


    --
    Steven
     
    Steven D'Aprano, Oct 11, 2012
    #4
  5. Satoru Logic

    suzaku Guest

    On Thursday, October 11, 2012 2:29:37 PM UTC+8, Steven D'Aprano wrote:
    > On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:
    >
    >
    >
    > > I came across a function named `wsample` in a `utils` package of my

    >
    > > workplace recently.

    >
    > >

    >
    > > The "w" in `wsample` stands for `weighted`, and it randomly selects an

    >
    > > element from a container according to relative weights of all the

    >
    > > elements.

    >
    > >

    >
    > > In most articles and codes I saw online, a function like this is often

    >
    > > named `weighted_random_choice`, which sounds *correct* to me. So when I

    >
    > > saw this `wsample` function, I considered it a improper name. Because

    >
    > > `wsample`makes me think of `random.sample`, which returns a list of

    >
    > > randomly generated elements, not a element.

    >
    >
    >
    > You can have a sample size of one.
    >
    >
    >
    > wsample sounds fine to me. weighted_random_choice is okay too. It depends
    >
    > whether you value brevity over explicitness. Explicit is good, but some
    >
    > names are just too long.
    >
    >
    >
    > If this were my code base, I would probably go for weighted_sample
    >
    > without mentioning "random" in the name, the reasoning being that samples
    >
    > are almost always random so explicitly saying so doesn't help much.


    I think if a programmer has used the built-in `random` module before, he would expect a function with "sample" in its name to return a population sequence.

    If a function is to return scalar value instead of sequence, I would expect it to be named "choice".

    >
    >
    >
    >
    >
    > --
    >
    > Steven
     
    suzaku, Oct 11, 2012
    #5
  6. On Wed, 10 Oct 2012 23:44:42 -0700, suzaku wrote:

    > I think if a programmer has used the built-in `random` module before, he
    > would expect a function with "sample" in its name to return a population
    > sequence.


    I have used the random module for about fifteen years, and I still write
    random.sample when I need to use random.choice.

    In statistics, probability, and plain English, a sample can be a single
    item: that's why we can say "a sample" or "two samples".


    > If a function is to return scalar value instead of sequence, I would
    > expect it to be named "choice".


    And I wouldn't. But what do I care? I'm never going to use the code
    you're talking about, so call it "sasquatch" if you like, it's no skin
    off my nose.

    :)


    --
    Steven
     
    Steven D'Aprano, Oct 12, 2012
    #6
  7. Satoru Logic

    suzaku Guest

    On Friday, October 12, 2012 10:22:16 AM UTC+8, Steven D'Aprano wrote:
    > On Wed, 10 Oct 2012 23:44:42 -0700, suzaku wrote:
    >
    >
    >
    > > I think if a programmer has used the built-in `random` module before, he

    >
    > > would expect a function with "sample" in its name to return a population

    >
    > > sequence.

    >
    >
    >
    > I have used the random module for about fifteen years, and I still write
    >
    > random.sample when I need to use random.choice.
    >
    >
    >
    > In statistics, probability, and plain English, a sample can be a single
    >
    > item: that's why we can say "a sample" or "two samples".


    Thanks for sharing your experience.

    As I'm not a native speaker of English,
    when I learned that `random.choice` return single item,
    and `random.sample` return a sequence of items,
    I thought that the behaviour is determined by their definitions.

    >
    >
    >
    >
    >
    > > If a function is to return scalar value instead of sequence, I would

    >
    > > expect it to be named "choice".

    >
    >
    >
    > And I wouldn't. But what do I care? I'm never going to use the code
    >
    > you're talking about, so call it "sasquatch" if you like, it's no skin
    >
    > off my nose.
    >
    >
    >
    > :)
    >
    >
    >
    >
    >
    > --
    >
    > Steven
     
    suzaku, Oct 12, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cylin
    Replies:
    6
    Views:
    597
    Mike Wahler
    Aug 19, 2003
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    511
    Patrick Kowalzick
    Mar 14, 2006
  3. Replies:
    9
    Views:
    550
    P.J. Plauger
    May 10, 2006
  4. Guest
    Replies:
    2
    Views:
    1,970
    Guest
    Aug 18, 2007
  5. Bodi
    Replies:
    1
    Views:
    320
    Lasse Reichstein Nielsen
    Nov 18, 2003
Loading...

Share This Page