timclicks / choices Goto Github PK
View Code? Open in Web Editor NEWfor making random decisions
for making random decisions
choices is designed to make it easy to make it easy to make things happen in a probabilistic manner. Its main API is the Choice class. When called, a Choice object will return one of its keys according to the proability it has been assigned. Here's an example: >>> mood = Choice({'happy': 0.3, 'neutral': 0.6, 'sad': 0.1}) >>> mood() 'happy' >>> mood() 'neutral' >>> mood() 'neutral' Creating a Choice object ======================== There are three main ways for creating a Choice object: - a dict of keys and probabilities - a list of (key, probability) pairs - a list of (key, sample_count) pairs Each of those options is explained below. A dict of keys and probabilities -------------------------------- Perhaps the simplest way to create a Choice is to provide the keys and associated probability wrapped up as a dict: >>> mood = Choice({'happy': 0.3, 'neutral': 0.6, 'sad': 0.1}) >>> mood() 'happy' >>> mood() 'neutral' A list of (key, probability) pairs ---------------------------------- As well as a dict assigning a probability to each key, you can send in a list of (key,value) pairs as lists or tuples. This allows for lots of flexibility, including non-hashable types to be used. This means that you can provide objects such as functions to be used as keys. Let's say we wanted to create an NPC for a game and wanted some scripted responses. Why not encode moods as functions and then pass them togetheer as a Choice. >>> def grumpy(news): ... return ':/' >>> def happy(news): ... return ':)' >>> react = Choice([(grumpy, 0.7), (happy, 0.3)]) >>> reaction = react() >>> reaction("We're getting married!") ':/' A list of (key, sample_count) pairs ----------------------------------- You are not restricted to adding p to 1. If you only have samples, and wish to calculate a probablilty distribution, you can provide those too: >>> birds_spotted = {'geese': 0, 'ducks': 12, 'sparrows': 4, 'other': 39} >>> birds = Choice(birds_spotted) >>> birds.distribution [('geese', 0.0), ('sparrows', 0.07272727272727272), ('ducks', 0.21818181818181817), ('other', 0.7090909090909091)] Usage ===== Making decisions ---------------- To return a new value, simply call the Choice object. >>> where_to_go = Choice({"Paris": 0.4, "London": 0.4, "Copenhagen": 0.1, "Barcelona": 0.1 }) >>> where_to_go() 'Copenhagen' Finding probabilities --------------------- You can retrieve the distribution of how those values are applied by accessing the distribution attribute: >>> mood.distribution [('happy', 0.3), ('neutral', 0.6), ('sad', 0.1)] >>> where_to_go.distribution [('Paris': 0.4), ('London', 0.4), ('Copenhagen', 0.1), ('Barcelona', 0.1)] Retrieving the probability of a particular key is supported: >>> birds['geese'] 0.0
It would be sharp to provide a sorted distribution.. would enable bisection search & would nicer when printed.
>>> from choices import PDist
>>> from collections import Counter
>>> a = PDist([(x, 1) for x in 'abcd'])
>>> Counter(a() for x in range(1000))
Counter({'b': 493, 'c': 273, 'a': 234})
Oh noes! b
has gobbled up d
!
It would be great if we users could declare a tree, which would result in creating a conditional probability. I have no idea how this might work, and am very interested in ideas.
Reverse the order of PDist._cumulative_dist
, so that likely events are triggered first. This will lead to fewer lookups.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.