Ask Your Question
0

Problem calling np.random.multinomial from notebook

asked 2012-05-15 19:37:13 +0200

Shashank gravatar image

According to np.random.multinomial? this is what I should expect

np.random.multinomial(20, [1/6.]*6, size=2)
array([[3, 4, 3, 3, 4, 3],
       [2, 4, 3, 4, 0, 7]])

However when I type the same thing in notebook I get an error. The same command works fine in my system python. In sage notebook it works when I drop the argument size=2. This seems to be problem with sage notebook. Any suggestions?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_48.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW1wb3J0IG51bXB5IGFzIG5wCm5wLnJhbmRvbS5tdWx0aW5vbWlhbCgxMDAsIFsxLzIuMF0qMixzaXplPTMp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpARgtW7/___code___.py", line 4, in <module>
    exec compile(u'np.random.multinomial(_sage_const_100 , [_sage_const_1 /_sage_const_2p0 ]*_sage_const_2 ,size=_sage_const_3 )
  File "", line 1, in <module>

  File "mtrand.pyx", line 4050, in mtrand.RandomState.multinomial (numpy/random/mtrand/mtrand.c:17809)
  File "element.pyx", line 1305, in sage.structure.element.RingElement.__add__ (sage/structure/element.c:11569)
  File "coerce.pyx", line 797, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:7467)
TypeError: unsupported operand parent(s) for '+': 'Integer Ring' and '<type 'tuple'>'
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2012-05-15 22:39:31 +0200

Mike Hansen gravatar image

updated 2012-05-15 23:37:21 +0200

kcrisman gravatar image

The issue is that within the notebook or the command-line, Sage preparses 2 to Integer(2), and the numpy code thinks that since it isn't an int, that it should be a tuple. The following are work-arounds:

sage: np.random.multinomial(20, [1/6.]*6, size=2r)
array([[3, 1, 1, 2, 5, 8],
       [2, 5, 3, 6, 4, 0]])
sage: np.random.multinomial(20, [1/6.]*6, size=int(2))
array([[4, 4, 5, 3, 1, 3],
       [3, 4, 3, 4, 1, 5]])
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-05-15 19:37:13 +0200

Seen: 694 times

Last updated: May 15 '12