Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成1000次迭代_Python - Fatal编程技术网

Python 生成1000次迭代

Python 生成1000次迭代,python,Python,我正在尝试使用fake\u exp\u y\u值generator生成1000组数据点。我所要做的就是用不同的拟合模型计算所有1000次迭代的chi2值。但是我现在编码它的方式只给了我一个chi2值。我不明白怎么了 这是我的密码: true_mean = 5 #Assume that we know the perfect fitting model is gaussian #with mean value 5. And use try_mean with different mean val

我正在尝试使用
fake\u exp\u y\u值
generator生成1000组数据点。我所要做的就是用不同的拟合模型计算所有1000次迭代的
chi2
值。但是我现在编码它的方式只给了我一个
chi2
值。我不明白怎么了

这是我的密码:

true_mean = 5 #Assume that we know the perfect fitting model is gaussian 
#with mean value 5. And use try_mean with different mean values to see how 
#does the chi2 behave
true_amp = 100
true_wid = 2
true_offset =10
x_values =  np.array([i for i in np.arange(0,10,0.4)])
exact_y_values = np.array([true_offset + true_amp*
                               np.exp(-((i-true_mean)/true_wid)**2)
                               for i in x_values])
    
    
def func (x_values,offset,amp,mean,wid):
    return (offset + amp*np.exp(-((i-mean)/wid)**2))

try_mean=np.linspace(2,8,25)
y_values=func(x_values,true_offset,true_amp,try_mean,true_wid)

for i in range (1000):
    chi2=np.zeros(1000)
    fake_exp_y_values = np.array([np.random.poisson(y)
                                      for y in exact_y_values])
    residuals=fake_exp_y_values-y_values
    y_err=np.clip(np.sqrt(fake_exp_y_values),1,9999)
    pulls=residuals/y_err
    chi2[i]=np.sum(pulls**2)
然而,返回的
chi2
列表是:


此代码1000次创建一个包含1000个零的数组,并在每个数组中插入一个值,但只保留最后一个值:

您只需要创建一个数组,并在1000次迭代中的每一次中使用它:

chi2 = np.zeros(1000)
for i in range(1000):
    # [...]
    chi2[i] = np.sum(pulls**2)

是的,天哪。谢谢。我是在装傻
chi2 = np.zeros(1000)
for i in range(1000):
    # [...]
    chi2[i] = np.sum(pulls**2)