gpt4 book ai didi

python - 使用 scipy_optimize 进行优化

转载 作者:行者123 更新时间:2023-11-30 22:44:14 24 4
gpt4 key购买 nike

我正在尝试使用 scipy.optimize 的 curve_fit 来优化函数。这是我的代码。

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit

xdata = [row[0] for row in pd.read_excel("C:\\Users\\310967\\Desktop\\Scholar\\Wound Chelation Draft\\ChelationFiles.xlsx", sheetname="Case2Data",skiprows=0).as_matrix()]
ydata = [row[1] for row in pd.read_excel("C:\\Users\\310967\\Desktop\\Scholar\\Wound Chelation Draft\\ChelationFiles.xlsx", sheetname="Case2Data",skiprows=0).as_matrix()]
SF = [row[4] for row in pd.read_excel("C:\\Users\\310967\\Desktop\\Scholar\\Wound Chelation Draft\\ChelationFiles.xlsx", sheetname="Case2Data",skiprows=0).as_matrix()]
uncertainty = [(np.sqrt(np.exp(np.log(a)**2)-1))*b for a,b in zip(SF, ydata)]

Tau = [0,1,5,7]



def func(x, I, E, ic1, ic2, ih1, ih2):

def iu(t):
return ((0.01295*np.exp(-0.645974*t))+(4.3688e-4*np.exp(-0.04251*t))+(5.642452e-5*np.exp(-0.00160863*t)))

def ic(t,tj):
if t > tj:
return ic1*np.exp(-0.693/ih1*(t-tj))+ic1*np.exp(-0.693/ih1*(t-tj))
else:
return 0

def listofic(t):
list1 = []
for tj in Tau:
list1.append(ic(t,tj))
return list1

def Kj(tj):
return iu(tj+1)*(E-1)/(ic(1,0)-iu(tj+1))

def listofKj():
list2 = []
for tj in Tau:
list2.append(Kj(tj))
return list2

Kjs = listofKj()

def listofOneMinusKj(t):
list3 = []
for a in Tau:
if t > a:
value = 1-Kj(a)
else:
value = 1
list3.append(value)
return list3

return (iu(x)*np.prod(listofOneMinusKj(x))+sum([a*b for a,b in zip(Kjs,listofic(x))]))*I


popt, pcov = curve_fit(func, xdata, ydata, sigma=uncertainty)
print(popt)

当我运行上述代码时,我收到一条错误消息,指出“具有多个元素的数组的真值不明确。请使用 a.any() 或 a.all()”。这是指函数listofOneMinusKj(t)中的“if t>a”部分。

但是,如果我运行以下代码,尽管存在“if t>a”,但代码会按照我的预期运行。我想知道上面的代码有什么问题。

import numpy as np
Tau = [0,1,5,7]

def func(x, I, E, ic1, ic2, ih1, ih2):

def iu(t):
return ((0.01295*np.exp(-0.645974*t))+(4.3688e-4*np.exp(-0.04251*t))+(5.642452e-5*np.exp(-0.00160863*t)))

def ic(t,tj):
if t > tj:
return ic1*np.exp(-0.693/ih1*(t-tj))+ic1*np.exp(-0.693/ih1*(t-tj))
else:
return 0

def listofic(t):
list1 = []
for tj in Tau:
list1.append(ic(t,tj))
return list1

def Kj(tj):
return iu(tj+1)*(E-1)/(ic(1,0)-iu(tj+1))

def listofKj():
list2 = []
for tj in Tau:
list2.append(Kj(tj))
return list2

Kjs = listofKj()

def listofOneMinusKj(t):
list3 = []
for a in Tau:
if t > a:
value = 1-Kj(a)
else:
value = 1
list3.append(value)
return list3

return (iu(x)*np.prod(listofOneMinusKj(x))+sum([a*b for a,b in zip(Kjs,listofic(x))]))*I

print(func(1,400,12.5,0.99,0.01,0.55,10))

最佳答案

Scipy Optimizes Curve Fitting Procedure尝试将 xdata 的完整向量放入函数 func 中。您将其传递给listofOneMinusKj

因此t > a(或作为x > a传递)产生一个 bool 向量。然后会触发以下错误:

"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

出现此错误的原因是您无法检查length > 1 的向量是否为真。就像建议的那样,您可以使用 (t > a).any() 来检查 t 的任何值是否大于 a 或 (t > a).all() > 检查 t 的所有值是否都更大。

关于python - 使用 scipy_optimize 进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41579431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com