How can I fix “IndexError: only integers” in a numpy array?

How can I fix “IndexError: only integers” in a numpy array?



(Solved. See answer below) I am teaching myself Python in preparation for a school project. I ran into the following error that I cannot figure out how to fix:



IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices


:


...


None



Here is the code I am trying to run (edited it some based on comments)


from sympy import *
from sympy.abc import i
from mpmath import *
import numpy as np
np.set_printoptions(suppress=True)

N = 3
M = np.zeros(shape=(N, N))
for n in range(0, N):
M[0, n] = fac(n) # mpmath.fac
for m in range(1, N):
for n in range(0, N):
M[m, n] = (Sum(M[m - 1, i], (i, 0, n + 1))) / (n+1)
print(M)



Essentially, I want to specify a dimension for the array and a first row for the array. Then I want construct the remaining entries in the array in a certain way (Holder summability if you know about that summation technique).



I have looked over various other questions in stack overflow with this particular error but the questions were beyond my current knowledge or did not exactly address my issue. I have fiddled with many of the parameters and have isolated the issue to line 13. In particular, as soon as I put "i" inside M[m-1,i], I receive the error. My code runs if I put any number less than the dimension instead of "i" but this is not the construction I need.



For example, the entry M[2,1] should be constructed as:


M[2,1]



M[2,1] = (M[1,0] + M[1,1]) / 2


M[2,1] = (M[1,0] + M[1,1]) / 2



Which I thought corresponded to the sum I had written. How did my indexes become something other than a positive integer?



Thanks!






The "i" in M[m - 1, i] is suspect: it does not appear correctly defined/assigned elsewhere

– user2864740
Sep 13 '18 at 2:17



M[m - 1, i]






On Sympy.org where I was following along I saw the example: >>> from sympy.abc import k, a, b >>> from sympy import Sum >>> Sum(1/k, (k, 2, 5)).doit().evalf() Here, they refer to k in the sum. I though it was standard practice to utilize your index in a sum. Sorry for the formatting. I don't know how to press "enter" to generate space in a comment.

– Robert Monahan
Sep 13 '18 at 2:24







Stop star-importing everything. You wouldn't have had this problem if you hadn't star-imported everything out of sympy.abc.

– user2357112
Sep 13 '18 at 2:33


sympy.abc






Thanks for the suggestion. I saw that syntax in a getting started guide so I used it. I switched to "import numpy as np" and "from sympy.abc import i." Unfortunately, the error persists.

– Robert Monahan
Sep 13 '18 at 2:40






...wait, you imported i deliberately? Okay, looking closer, you seem to be using i as a summation symbol for a sympy Sum, so that was probably deliberate. Mixing sympy and numpy like that doesn't work; they don't understand each other, and M doesn't know how to build an expression object for M[m - 1, i].

– user2357112
Sep 13 '18 at 2:47



i


i


Sum


M


M[m - 1, i]




2 Answers
2



You are mixing numpy and sympy, and it's giving problems:


numpy


sympy



Your sympy import defines i as a symbol (that's not obvious to numpy users):


sympy


i


numpy


In [1]: from sympy.abc import *
In [2]: i
Out[2]: i



then you define a numpy array. (import numpy as np is better than *):


numpy


import numpy as np


*


In [3]: M=np.zeros((3,3))



trying to use this i as index raises your error:


i


In [4]: M[1,i]
------------------------------------------------------------------
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices



A symbol may work in a sympy matrix, but it does not work when indexing a numpy array.


sympy


Ms = sy.zeros(3,3) # sympy matrix
for m in range(1,3):
for n in range(3):
Ms[m,n] = (sy.Sum(Ms[m-1,i],(i,0,n+1)))/(n+1)



This runs, though the resulting Ms doesn't make much sense.


Ms



So the root of the problem is that you are mixing numpy and sympy, possibly through ignorance, and possibly because of the * imports.


*






Thanks for the explanation. I switched to "import numpy as np" and "from sympy.abc import i" rather than star importing. Unfortunately, I still have the error. How do i receive an honest working index in this case?

– Robert Monahan
Sep 13 '18 at 2:44






Use a sympy matrix rather than a numpy array.

– hpaulj
Sep 13 '18 at 2:47






I see. I was able to get it to run but with some odd print in the log. Strange and sporadic spacing.

– Robert Monahan
Sep 13 '18 at 2:53



I was able to fix the error based on everyone's help here. Thanks! I see that I should not star-import everything and that I should stick with either sympy or numpy so they aren't fighting each other. I was successfully able to rewrite my program to correctly build the Holder sums. Here is my running code:


import sympy as sy
import mpmath as mp
import numpy as np
np.set_printoptions(suppress=True)
sy.init_printing(use_unicode=False, wrap_line=False, no_global=True)
N = 5
M = np.zeros((N, N))
for n in range(0, N):
M[0, n] = mp.fac(n)
for n in range(0, N):
for i in range(0, n + 1):
M[1, n] += (M[0, i])
for m in range(2, N):
for n in range(0, N):
for i in range(0, n + 1):
M[m, n] += (M[m - 1, i])
M[m, n] = M[m, n] / (n + 1)
print(M)



This correctly (and beautifully) prints:


[[ 1. 1. 2. 6. 24. ]
[ 1. 2. 4. 10. 34. ]
[ 1. 1.5 2.33333333 4.25 10.2 ]
[ 1. 1.25 1.61111111 2.27083333 3.85666667]
[ 1. 1.125 1.28703704 1.53298611 1.99772222]]



Thanks again for the help!



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)