Python:TypeError:';浮动';对象没有属性'__获取项目';

Python:TypeError:';浮动';对象没有属性'__获取项目';,python,Python,我正在尝试用python实现粒子过滤器算法。我得到这个错误: 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__' 我的代码: import math import numpy as np import

我正在尝试用python实现粒子过滤器算法。我得到这个错误:

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__'
我的代码:

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])
导入数学
将numpy作为np导入
将matplotlib.pyplot作为plt导入
x=0.1#初始值
x_N=1#状态更新过程噪声协方差
x_R=1#测量中的噪声协方差
T=75#迭代次数
N=10#粒子数
V=2
x_P=[None]*(N)
对于x范围内的i(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())#测量值的实际输出向量。
x_out=np.数组([x])#测量值的实际输出向量。
x_est=np.数组([x]);#粒子滤波器的逐时输出估计
x_est_out=np.数组([x_est])#粒子滤波器估计的向量。
x_P_update=[None]*N
z_update=[None]*N
P_w=[无]*N
对于X范围内的t(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+数学sqrt(x_R)*np.random.randn()
对于x范围内的i(0,N):
#每个粒子都会使用过程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()
#更新每个粒子的观测值
z_update[i]=x_P_update[i]**2/20
#生成权重
P_w[i]=(1/math.sqrt(2*math.pi*x_R))*math.exp(-(z-z_更新[i])**2/(2*x_R))
P_w[:]=[k/P_w中k的和(P_w)]
#打印(np.where(np.cumsum(P_w,axis=0)>=np.random.rand())
#打印(索引组[0][1])
#P_w_数组=np.数组(列表(P_w))
#如果np.cumsum(P_w_数组)>=np.random.rand(),则索引=[i代表范围内的i(len(P_w))]
对于x范围内的i(0,N):

index\u tuple=np.where(np.random.rand()我不打算通过视频教程来修复您的算法,但我可以告诉您为什么会出现此错误

在这方面:

x_P = x_P_update[m]
您正在为数组分配浮点值,然后尝试将其作为外部循环中的数组进行访问。更新它将消除错误:

x_P[m] = x_P_update[m]

谢谢,但是现在我得到了一些其他错误。m=index\u tuple[0][1]索引器错误:索引1超出大小为的轴0的界限1@stoned_blasphemer这是你的算法的一个问题。@stoned_blasphemer你应该如上图所示更正你的代码,将你的新问题作为一个新问题发布,除了这个答案,因为它回答了你的问题。我通过将m改为index_tuple[0][0]修复了它,现在它可以正常工作了!:)