I’ve a problem with numpy’s array constructor. I want to initialize an 2-D array with tuples, but it doesn’t work as with integers :
>>> A = array([[0, 0], [3, 5]])
>>> print(A)
[[0 0]
[3 5]]
>>> A[1, 1] = 7
>>> print(A)
[[0 0]
[3 7]]
>>> A = array([[(0, 0), (0, 1)], [(1, 0), None]], dtype=object)
>>> A[1, 1] = (2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: invalid index
>>> A.shape
(2,)
I really need to fill these matrix “by hand”. Any idea ?
Your code seems to work for me (using the explicit numpy namespace). I’m using numpy v1.6.1:
What version of numpy are you using?
Update This seems to be an issue related to the numpy version since I can reproduce the OP’s error using numpy v1.5.1 (the version that comes packaged with the base python install in OSX Lion). I’m not sure if this was a bug in numpy that was fixed or a change in the implementation. I would either update to a newer version of numpy or use this simple workaround:
Update #2 Here’s a general fix that hopefully you can adapt: