pythonpymc3best-fit-curve

IndexError: only integers, slices (`:`), ellipsis (`...`) . .


I am using pymc3 to find a best fit for a 3D surface. This is the code that I am using.

with Model() as model:
# specify glm and pass in data. The resulting linear model, its likelihood and                                                                                                   
# and all its parameters are automatically added to our model.                                                                                                                   
glm.glm('z ~ x**2 + y**2 + x + y + np.sin(x) + np.cos(y)' , flatimage)
start = find_MAP()
step = NUTS(scaling=start) # Instantiate MCMC sampling algorithm                                                                                                                 
trace = sample(2000, step, progressbar=False) # draw 2000 posterior samples using NUTS sampling                                                                                  

I got an error in this line:

glm.glm('z ~ x**2 + y**2 + x + y + np.sin(x) + np.cos(y)' , flatimage)

The error is :

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

I had tried to fix it by changing sin(x) and cos(y) to np.sin(x) and np.cos(y), but that didn't work, and I don't know what else to do.


Solution

  • I think the problem is related to your definition of flatimage. You need your data labeled for the glm module to work. Something like this:

    # synthetic data (just an example)
    x = np.random.normal(size=100)
    y = np.random.normal(size=100)
    z = x**2 + y**2 + x + y + np.sin(x) + np.cos(y)
    
    data = dict(x=x, y=y, z=z) # a pandas dataframe will also work
    
    with pm.Model() as model:
        pm.glm.glm('z ~ x**2 + y**2 + x + y + np.sin(x) + np.cos(y)' , data)
        start = pm.find_MAP()
        step = pm.NUTS(scaling=start)         
        trace = pm.sample(2000, step, start)
    

    Check this example for other details.