python - Generating Discrete random variables with specified weights using SciPy or NumPy -


i looking simple function can generate array of specified random values based on corresponding (also specified) probabilities. need generate float values, don't see why shouldn't able generate scalar. can think of many ways of building existing functions, think missed obvious scipy or numpy function.

e.g.:

>>> values = [1.1, 2.2, 3.3] >>> probabilities = [0.2, 0.5, 0.3] >>> print some_function(values, probabilities, size=10) (2.2, 1.1, 3.3, 3.3, 2.2, 2.2, 1.1, 2.2, 3.3, 2.2) 

note: found scipy.stats.rv_discrete don't understand how works. specifically, not understand (below) means nor should do:

numargs = generic.numargs [ <shape(s)> ] = ['replace resonable value', ]*numargs 

if rv_discrete should using, please provide me simple example , explanation of above "shape" statement?

drawing discrete distribution directly build numpy. function called random.choice (difficult find without reference discrete distributions in numpy docs).

elements = [1.1, 2.2, 3.3] probabilites = [0.2, 0.5, 0.3] np.random.choice(elements, 10, p=probabilities) 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -