[Numpy-discussion] numpy.random.multinomial() cannot handle zero's
Michael Nandris
mnandris at btinternet.com
Sun Aug 26 08:45:55 EDT 2007
Hi,
Is there an easy way around this problem, that does not involve fixing the API (like using NaN instead of 0.0)?
>>> from numpy.random import multinomial
>>> multinomial(100,[ 0.2, 0.4, 0.1, 0.3 ])
array([19, 45, 10, 26])
>>> multinomial( 100, [0.2, 0.0, 0.8, 0.0] )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mtrand.pyx", line 1173, in mtrand.RandomState.multinomial
TypeError: exceptions must be strings, classes, or instances, not exceptions.ValueError
I found a similar problem in scipy.stats.rv_discrete() which was fixed by adding sort to a dictionary handler:
"""
def reverse_dict(dict):
newdict = {}
for key in dict.keys(): # DUFF
newdict[dict[key]] = key
return newdict
"""
def reverse_dict(dict):
newdict = {}
for key in dict.keys():
sorted_keys = copy(dict.keys())
sorted_keys.sort()
for key in sorted_keys[::-1]: # NEW
newdict[dict[key]] = key
return newdict
Obviously this cannot be done with numpy since it runs in C or something which I don't understand. Can anyone help? Numpy is great and the simulation I want to code requires speed.
Thanks for any advice given
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070826/92d087b5/attachment.html>
More information about the NumPy-Discussion
mailing list