集成导致TypeError:“ numpy.float64”对象无法解释为整数

时间:2018-10-04 21:50:57

标签: python python-3.x numpy syntax-error

亲爱的朋友,我在运行Python后发送了错误代码,并编写了大量代码: 抱歉,这个问题是业余的。

dacdm = 1/(z+1) * cdm
TypeError: 'numpy.float64' object cannot be interpreted as an integer

这是错误的最后2个句子。 Python错误地提到了我的代码的以下部分,因此我确定错误来自此处:

def H_LCDM(z, H0, DM0):
        lcdm = 1/(H0 * sqrt(DM0 * (1 + z)**3 + (1 - DM0)))
        return lcdm

def DA_LCDM(z, DM0, H0):
         cdm = quad(H_LCDM, 0, z, args = (H0, DM0))
         dacdm = 1/(z+1) * cdm
         return dacdm


def fg(gg, DM0, H0, A, alpha, b, Omb):
        h = H0/100
        f_th = (0.824 /(1 + 0.19 * (h)**(1/2)))* (Omb/DM0) * (DA_LCDM(zgas[gg], H0, DM0)/DA(zgas[gg], DM0, H0, A, alpha, b))
        fgasth = ((fgas[gg] - f_th)/siggas[gg])**2
        return fgasth

def fgasmass(DM0, H0, A, alpha, b, Omb):
    h = H0/100
    num=0
    for gsgs in range(len(zgas)):
        num = num + fg(gsgs, DM0, H0, A, alpha, b, Omb)
        return num + ((Omb * h**2 - 0.0214)/0.002)**2 + ((h - 0.72)/0.08)**2

我找不到问题的根源。

有关更多信息,我将整个错误放在这里:

 Traceback (most recent call last):
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 348, in <module>
 L = np.exp(-0.5 * (TOTAL(DM0n[i], H0n[i], An[i], alphan[i], bn[i], Mn[i], Ombn[i]) - TOTAL(DM0o[i-1], H0o[i-1], Ao[i-1], alphao[i-1], bo[i-1], Mo[i-1], Ombo[i-1])))    # L = exp(-( x^2 )/2)
  File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 328, in TOTAL
 total =  SN(DM0, H0, A, alpha, b, M) + BAO(DM0, H0, A, alpha, b, Omb) +  CMB(Omb, H0, DM0, A, alpha, b) + CC(DM0, H0, A, alpha, b) + SGL(DM0, H0, A,  alpha, b) + fgas(DM0, H0, A, alpha, b, Omb)
  File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 322, in fgas
num = num + fg(gsgs, DM0, H0, A, alpha, b, Omb)
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 315, in fg
f_th = (0.824 /(1 + 0.19 * sqrt(h)))* (Omb/DM0) * (DA_LCDM(zgas[gg], H0, DM0)/DA(zgas[gg], DM0, H0, A, alpha, b))
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 163, in DA_LCDM
dacdm = 1/(z+1) * cdm
TypeError: 'numpy.float64' object cannot be interpreted as an integer

非常感谢您。

1 个答案:

答案 0 :(得分:1)

cdm是一个序列,而不是数字。也许quadscipy.integrate.quad,它返回的元组最多为5个元素,而不仅仅是整数。带有序列的*期望整数表示重复该序列的次数;例如,我们有

>>> 3 * (1, 2, 3)
(1, 2, 3, 1, 2, 3, 1, 2, 3)

如果quadscipy.integrate.quad,则积分是元组的第一个元素,因此,如果需要积分,请从结果中索引它:

#                                         vvv
cdm = quad(H_LCDM, 0, z, args = (H0, DM0))[0]

此外,将来,请阅读文档以了解您使用的功能。