“numpy。float64'对象不可调用

时间:2021-11-29 21:26:56

For some reason that I can't wrap my head around, it says TypeError: 'numpy.float64' object is not callable for the following code:

由于某种原因,我不能把头转过来,它说了TypeError:“numpy”。以下代码不能调用float64'对象:

import pylab as pl
import scipy as sp
import numpy as num

import scipy.integrate as spi
import matplotlib.pyplot as mat
import scipy.optimize as spo
from itertools import cycle
from matplotlib.font_manager import FontProperties

rs=.14
ra=0.0027
Mz=91.
ja=0.81
js=-.033
Gz=2.5
k=10**6
def sig_a(s,Gz):
    return (sp.pi)*((s*ra+(s-Mz**2)*ja)/((s-Mz**2)**2+Mz**2*Gz**2))
def sig_s(s,Gz):
    return (4*sp.pi/3)*(1/s+(s*rs+(s-Mz**2)*js)/((s-Mz**2)**2+Mz**2*Gz**2))

cos_theta=num.arange(-0.95,0.95,0.05)
E=num.arange(20,140,.1)
s=E**2


def f_theta(x,s):
    ans=k*(sig_s(s,Gz)*(1+(x)**2)+sig_a(s,Gz)*x)
    return and


d=num.arange(0.05,1.80,0.25)
x1=[]
for t in cos_theta:
    m=((t+t+0.05)/2)
    x1.append(m)
x01=num.array(x1)
def N_mu1(x0,sig_a,sig_s):     #<-------d=0.05
    n=(k*(sig_s*((x0**2.)*0.05+(0.05/4.)+(0.05**3.)/(12.))+sig_a(2.*x0*0.05)))
    return n

idealN=[]
randomN=[]
est_sig_a=[]
est_sig_s=[]
ratio_error=[]     
for i in s:
    for j in cos_theta:
        n=(spi.quad(f_theta,j,j+0.05,args=i))
        idealN.append(n[0])
    for k in idealN:
        r=num.random.poisson(k,1)
        randomN.append(r[0])
    siga=sig_a(i,Gz)
    sigs=sig_s(i,Gz)
    R=num.array(randomN)
    Error=(R**0.5)
    po,po_cov=spo.curve_fit(N_mu1,x01,R,[siga,sigs],Error)
    est_sig_a.append(po[0])
    est_sig_s.append(po[1])
    e=((((po_cov[0])/(po[0]))+((po_cov[1])/(po[1])))*((po[0])/(po[1])))
    ratio_error.append(e)
    idealN=[]
    randomN=[]

And the error show was:

错误显示为:

TypeError                                 Traceback (most recent call last)
    118     R=num.array(randomN)
    119     error=(R**0.5)
--> 120     po,po_cov=spo.curve_fit(N_mu1,x01,R,[siga,sigs],error)
    121     est_sig_a.append(po[0])
    122     est_sig_s.append(po[1])


TypeError: 'numpy.float64' object is not callable

I am struggling to find the mistake in the code.

我正在努力查找代码中的错误。

2 个解决方案

#1


4  

The problem is here:

这里的问题是:

def N_mu1(x0,sig_a,sig_s):     #<-------d=0.05
    n=(k*(sig_s*((x0**2.)*0.05+(0.05/4.)+(0.05**3.)/(12.))+sig_a(2.*x0*0.05)))
    return n                                              # ^ argument is an array not function 

sig_a is an argument to your function so it doesn't refer to the sig_a function you defined, and in this function it is being called as if it was.

sig_a是函数的参数,所以它不指向你定义的sig_a函数,在这个函数中,它被调用,就好像它是。

#2


1  

In f_theta Changing return ans Into return and.

在f_theta变化中返回,返回。

Also sig_a(2.*x0*0.05) should be changed into sig_a*(2.*x0*0.05) in N_mu1.

在N_mu1中,sig_a(2.*x0*0.05)也应该改为sig_a*(2.*x0*0.05)。

These changes will make your program executable.

这些更改将使您的程序可执行。

#1


4  

The problem is here:

这里的问题是:

def N_mu1(x0,sig_a,sig_s):     #<-------d=0.05
    n=(k*(sig_s*((x0**2.)*0.05+(0.05/4.)+(0.05**3.)/(12.))+sig_a(2.*x0*0.05)))
    return n                                              # ^ argument is an array not function 

sig_a is an argument to your function so it doesn't refer to the sig_a function you defined, and in this function it is being called as if it was.

sig_a是函数的参数,所以它不指向你定义的sig_a函数,在这个函数中,它被调用,就好像它是。

#2


1  

In f_theta Changing return ans Into return and.

在f_theta变化中返回,返回。

Also sig_a(2.*x0*0.05) should be changed into sig_a*(2.*x0*0.05) in N_mu1.

在N_mu1中,sig_a(2.*x0*0.05)也应该改为sig_a*(2.*x0*0.05)。

These changes will make your program executable.

这些更改将使您的程序可执行。