Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66507

Re: random.sample with large weighted sample-sets?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.066
X-Spam-Evidence '*H*': 0.87; '*S*': 0.00; '(instead': 0.09; 'answering': 0.09; 'compact': 0.09; 'item,': 0.09; 'item.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'semantic': 0.09; 'being,': 0.16; 'example).': 0.16; 'finney': 0.16; 'helps!': 0.16; 'in;': 0.16; 'missing?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sequence,': 0.16; 'subject:sample': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; 'things.': 0.19; 'header:User-Agent:1': 0.23; '(such': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'tim': 0.29; "i'm": 0.30; 'chase': 0.31; 'libraries': 0.31; 'probability': 0.31; 'writes:': 0.31; "i'd": 0.34; 'subject:with': 0.35; 'choosing': 0.36; 'received:com.au': 0.36; 'sequence': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'sometimes': 0.38; 'arrange': 0.38; 'ben': 0.38; 'to:addr:python- list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'hope': 0.61; 'matter': 0.61; 'simply': 0.61; "you're": 0.61; 'sum': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'world': 0.66; 'positions': 0.67; 'sample': 0.67; '8bit%:21': 0.69; 'money': 0.72; 'records': 0.73; 'special': 0.74; '\xe2\x80\x93': 0.77; '100': 0.79; '50),': 0.84; 'alone.': 0.84; 'of*': 0.84; 'received:125': 0.84; 'sweat': 0.84; "they'd": 0.84; '\xe2\x80\x9cthis': 0.84; 'genius': 0.91; 'hopes': 0.91; 'scientists,': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: random.sample with large weighted sample-sets?
Date Sun, 16 Feb 2014 16:08:27 +1100
References <20140215224145.68c82eb4@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host vmx15867.hosting24.com.au
X-Public-Key-ID 0xBD41714B
X-Public-Key-Fingerprint 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:cyNYCfIyCyd8HFQgcICGrkGWHnA=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7036.1392527326.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392527326 news.xs4all.nl 2857 [2001:888:2000:d::a6]:58412
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66507

Show key headers only | View raw


Tim Chase <python.list@tim.thechases.com> writes:

> I'm not coming up with the right keywords to find what I'm hunting.
> I'd like to randomly sample a modestly compact list with weighted
> distributions, so I might have
>
>   data = (
>     ("apple", 20),
>     ("orange", 50),
>     ("grape", 30),
>     )

That's not a list, it's a tuple. I think you want a list.

When you want a sequence where each position has a semantic meaning, use
a tuple (such as ‘("apple", 20)’). Each item has a meaning *because of*
the position it's in; if the items were in a different order, they'd
mean different things.

When you want a sequence where the positions don't have a special
meaning – each item means exactly the same no matter if you change the
order – that's sometimes called a “homogeneous” sequence, and you want a
list.

So a “record” should be represented as a tuple, and a “table” of records
should be represented as a list of tuples:

    records = [
            ("apple", 20),
            ("orange", 50),
            ("grape", 30),
            ]

> and I'd like to random.sample() it as if it was a 100-element list.

The implication being, I suppose, that you'd like the number in each
tuple to be a weighting for the probability of choosing that item.

For probability weightings, you should arrange for the weightings to sum
to 1 (instead of 100 in your example). Then each weighting is simply the
desired probability of that item, and those values will work with
various libraries that deal with probability.

> What am I missing? (links to relevant keywords/searches/algorithms
> welcome in lieu of actually answering in-line)

You're looking for a “probability distribution” and “weighted choice”.

Hope that helps!

-- 
 \     “This world in arms is not spending money alone. It is spending |
  `\      the sweat of its laborers, the genius of its scientists, the |
_o__)           hopes of its children.” —Dwight Eisenhower, 1953-04-16 |
Ben Finney

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: random.sample with large weighted sample-sets? Ben Finney <ben+python@benfinney.id.au> - 2014-02-16 16:08 +1100
  Re: random.sample with large weighted sample-sets? duncan smith <buzzard@invalid.invalid> - 2014-02-16 16:01 +0000
  Re: random.sample with large weighted sample-sets? Charles Allen <ca137tmp@earthlink.net> - 2014-02-16 10:35 -0600
    Re: random.sample with large weighted sample-sets? duncan smith <buzzard@invalid.invalid> - 2014-02-16 17:38 +0000

csiph-web