0

I am trying to implement particle filter algorithm in python. I am getting this error:

x_P_update[i] = 0.5*x_P[i] + 25*x_P[i]/(1 + x_P[i]**2) + 8*math.cos(1.2*(t-1)) +     math.sqrt(x_N)*np.random.randn()
TypeError: 'float' object has no attribute '__getitem__'

My code:

import math
import numpy as np
import matplotlib.pyplot as plt

x = 0.1 #initial value
x_N = 1 #process noise covariance in state update
x_R = 1 #noise covariance in measurement
T = 75 #number of iterations
N = 10 #number of particles

V = 2
x_P = [None]*(N)

for i in xrange(0, N):
    x_P[i] = x + math.sqrt(V)*np.random.randn()

z_out = np.array([x**2 / 20 + math.sqrt(x_R) * np.random.randn()])  #the actual output vector for measurement values.
x_out = np.array([x])  #the actual output vector for measurement values.
x_est = np.array([x]); # time by time output of the particle filters estimate
x_est_out = np.array([x_est]) # the vector of particle filter estimates.

x_P_update = [None]*N
z_update = [None]*N
P_w = [None]*N

for t in xrange(1, T+1):
    x = 0.5*x + 25*x/(1 + x**2) + 8*math.cos(1.2*(t-1)) +  math.sqrt(x_N)*np.random.randn()
    z = x**2/20 + math.sqrt(x_R)*np.random.randn()
    for i in xrange(0, N):
        #each particle is updated with process eq
        x_P_update[i] = 0.5*x_P[i] + 25*x_P[i]/(1 + x_P[i]**2) + 8*math.cos(1.2*(t-1)) + math.sqrt(x_N)*np.random.randn()
        #observations are updated for each particle
        z_update[i] = x_P_update[i]**2/20
        #generate weights
        P_w[i] = (1/math.sqrt(2*math.pi*x_R)) * math.exp(-(z - z_update[i])**2/(2*x_R))

    P_w[:] = [ k / sum(P_w) for k in P_w]
#    print(np.where(np.cumsum(P_w, axis=0) >= np.random.rand()))

  #  print(index_tuple[0][1])
#    P_w_array = np.array(list(P_w))
#    indices = [i for i in range(len(P_w)) if np.cumsum(P_w_array) >= np.random.rand()]
    for i in xrange(0, N):
        index_tuple = np.where(np.random.rand() <= np.cumsum(P_w, axis=0))
        m = index_tuple[0][1]
        x_P = x_P_update[m]

    x_est = np.array([np.mean(x_P)])
    x_out = np.array([x_out, x])
    z_out = np.array([z_out, z])
    x_est_out = np.array([x_est_out, x_est])

I am using matlab code from here to learn how to implement this algorithm in python using scipy. http://studentdavestutorials.weebly.com/particle-filter-with-matlab-code.html

I just started learning python and can't get out of this problem, kindly help.

1 Answer 1

2

I'm not going to go through the video tutorial and fix your algorithm, but I can show you why you're getting this error.

In this line:

x_P = x_P_update[m]

You are assigning an array with a float value, which you then attempt to access as an array in the outer loop. Updating it instead will get rid of your error:

x_P[m] = x_P_update[m]
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but now I am getting some other error. m = index_tuple[0][1] IndexError: index 1 is out of bounds for axis 0 with size 1
@stoned_blasphemer that is a problem with your algorithm.
@stoned_blasphemer you should correct your code as shown above, post your new problem as a new question and except this answer since it answered your question.
I fixed it by changing m to index_tuple[0][0] now it works as expected! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.