使用scipy.optimize.minimize会给我标题中的类型错误,我不完全确定我的代码有什么问题。
我已经检查了https://emcee.readthedocs.io/en/v2.2.1/user/line/处的示例代码,并将其与我自己的示例代码进行了比较,看是否对我有帮助。我仔细检查了一下是否偶然地为列表和函数命名,然后再重新启动内核以确保它们没有起作用,然后重新运行了我的代码。我尝试过的最新操作是检查我输入到minimum函数中的所有内容是否都是iterale,除了应调用的函数。
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy.stats import norm
from scipy.optimize import curve_fit
from scipy.optimize import minimize
import scipy.integrate as integrate
import scipy.special as special
%matplotlib inline
data = np.loadtxt('final_06.xy', skiprows = 4)
energy = data[:,0]
print(energy)
e = energy[energy < 250]
Hist = np.histogram(e, bins = 1500)
x = (Hist[1][1:]+Hist[1][:-1])/2
y = Hist[0]
def gaussian(x, a, b, c, d, e):
pi = np.pi
exp = np.exp(-(((x-a)/b)**2)/2)
bot = b*np.sqrt(2*pi)
return((e/bot)*exp+c*x+d)
sela = (x>0)&(x<10)
popta, pcova = curve_fit(gaussian, x[sela], y[sela], [2.6, 0.2, 0.,25., 250])
plt.figure()
plt.plot(x[sela], y[sela])
plt.plot(x[sela], gaussian(x[sela], *popta))
print(popta, np.diag(pcova))
print(popta[1]*2.35482, popta[1]*4.29193)
def Fsi(x, a, b, c):
pi = np.pi
exp = np.exp(-(((x-a)/b)**2)/2)
bot = b*np.sqrt(2*pi)
return((c/bot)*exp)
def Fbi(x, a, b):
return a*x+b
def lgLike(events, n, x, pars):
S, B = events
a, b, c, d, e = pars
L = S*Fsi(x, a, b, e)+B*Fbi(x, c, d)
fac = np.math.factorial(n)
lnl = np.math.log(L)
lnn = np.math.log(fac)
return -1*(np.sum(n*lnl-lnn-L))
LnLike = np.vectorize(lgLike)
S_guess = [0.0]
B_guess = [len(y[sela])]
guesses = [S_guess, B_guess]
print(iter(guesses))
para = minimize(LnLike, guesses, args=(y[sela], x[sela], popta))
我应该得到S和B的一些结果,这将是我的实际起始参数,但是我不断收到错误“ TypeError:'numpy.float64'对象不可迭代”。