tattybojangler
tattybojangler

Reputation: 1073

IndexError: only integers, slices (`:`), ellipsis (`...`), are valid indices

I'm trying to define the following function but I think i'm making an error somewhere -

import numpy as np
import math

def banana(A,B,C1,C2,N,keep,init): 

R = init*keep + N*keep
x1=x2=0
bimat = np.zeros((N,2))
for r  in range(1,R+1):
    x1=np.random.normal((B*x2+C1)/(A*(x2**2)+1),math.sqrt(1/(A*(x2**2)+1)))
    x2=np.random.normal((B*x1+C2)/(A*(x1**2)+1),math.sqrt(1/(A*(x1**2)+1)))
    if (r>init*keep and r%keep==0):
        mkeep=r/keep
        bimat[mkeep-init,:]=np.array([x1, x2])

return(bimat)

When I pass some values into the function, for example -

banana(0.5,0,3,3,1000,10,10)

I get the following error -

IndexError                                Traceback (most recent call last)
<ipython-input-288-9d3d5702a1a0> in <module>()
----> 1 banana(0.5,0,3,3,1000,10,10)

<ipython-input-287-ac0004b13e9f> in banana(A, B, C1, C2, N, keep, init)
      9         if (r>init*keep and r%keep==0):
     10             mkeep=r/keep
---> 11             bimat[mkeep-init,:]=np.array([x1, x2])
     12 
     13     return(bimat)

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

Does anyone know what might be causing it?

Thanks!

Upvotes: 1

Views: 2677

Answers (1)

perigon
perigon

Reputation: 2095

If you are using Python 3, then

    mkeep=r/keep

will be a float, which is not a valid index. Try casting it to int, or just using mkeep=r//keep to force integer division.

Upvotes: 1

Related Questions