I'm currently trying to fit some parameters to an existing data file. After adding a fitting routine I keep getting the 'TypeError: '*numpy.float64' object is not iterable*'
error, which seems to have something to do with the Dl function that i defined. I haven't been able to solve this myself, so I'd be really grateful for any tips concerning this matter.
我目前正在尝试将一些参数与现有的数据文件相匹配。在添加一个合适的例程后,我一直得到“TypeError:”*numpy。float64'对象不是可迭代的*'错误,它似乎与我定义的Dl函数有关。我自己还没能解决这个问题,所以我非常感谢你对这件事的任何建议。
import pylab as p
import scipy as s
from scipy.integrate import odeint,quad
import numpy as np
import matplotlib.pyplot as plt
import math
z = np.arange(0.00, 1.5, 0.02)
z1, m1, sigma_m = np.loadtxt('data.txt', unpack=True, usecols=[0,1,2])
yerr = sigma_m
def H(z,omega_m,H0):
return H0*p.sqrt(omega_m*(1+z)**3+1-omega_m)
def Dl(z,omega_m,H0):
c = 3*10**5
y = []
for i in z:
y1 = c*(1+i)*quad(f,0.0,i, args=(omega_m,H0))[0]
y.append(y1)
return p.asarray(y)
def f(z,omega_m,H0):
return 1./H(z,omega_m,H0)
def m(z,omega_m,H0,M):
q = []
for j in Dl(z,omega_m,H0):
q1 = M+5*np.log10(j)+25.0
q.append(q1)
return p.asarray(q)
def chi2(omega_m, M):
return sum((m(z1,omega_m,70,M)-m1)/sigma_m)**2
chi2_min=1*10**30
o = np.arange(0.00, 1.5, 0.02)
Mrange = np.arange(-1.5, 1.5, 0.02)
for omega_m in o:
for M in Mrange:
if chi2(omega_m, M) < chi2_min:
omega_min=omega_m
M_min=M
chi2_min=m(omega_min, M_min, 70, M)
print(M_min)
print(chi2_min)
2 个解决方案
#1
1
In your routine Dl
, the iteration over z is invalid. z is a scalar at each invokation.
在常规Dl中,迭代z是无效的。z是每个调用的标量。
Transform your program so that either Dl is given an array or remove the loop in Dl.
转换您的程序,使Dl得到一个数组或删除Dl中的循环。
#2
1
Your problem seems to be here:
你的问题似乎在这里:
chi2_min=m(omega_min, M_min, 70, M)
chi2_min = m(omega_min M_min 70米)
omega_min
is a float, which gets passed to Dl()
in m()
here:
omega_min是一个浮点数,它在m()中被传递给Dl():
for j in Dl(z,omega_m,H0):
j在Dl(z,omega_m,H0):
and then Dl()
tries to iterate it:
然后Dl()试图迭代它:
for i in z:
因为我在z:
which raises your error
这提高了你的错误
To fix, I recommend you pass omega_min
as a list:
为了解决这个问题,我建议您将omega_min作为一个列表:
chi2_min=m([omega_min], M_min, 70, M)
chi2_min = m([omega_min],M_min 70米)
#1
1
In your routine Dl
, the iteration over z is invalid. z is a scalar at each invokation.
在常规Dl中,迭代z是无效的。z是每个调用的标量。
Transform your program so that either Dl is given an array or remove the loop in Dl.
转换您的程序,使Dl得到一个数组或删除Dl中的循环。
#2
1
Your problem seems to be here:
你的问题似乎在这里:
chi2_min=m(omega_min, M_min, 70, M)
chi2_min = m(omega_min M_min 70米)
omega_min
is a float, which gets passed to Dl()
in m()
here:
omega_min是一个浮点数,它在m()中被传递给Dl():
for j in Dl(z,omega_m,H0):
j在Dl(z,omega_m,H0):
and then Dl()
tries to iterate it:
然后Dl()试图迭代它:
for i in z:
因为我在z:
which raises your error
这提高了你的错误
To fix, I recommend you pass omega_min
as a list:
为了解决这个问题,我建议您将omega_min作为一个列表:
chi2_min=m([omega_min], M_min, 70, M)
chi2_min = m([omega_min],M_min 70米)