scipy.optimize의 curve_fit를 사용하여 함수를 최적화하려고합니다. 여기 내 코드가있다. 나는 위의 코드를 실행하면scipy_optimize를 사용하여 최적화
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))
힌트 및 오프 주제 : 다음 코드를 사용하면 "파이썬"방식으로 더 잘 읽고 쓸 수 있습니다. df = pd.read_csv (...)'; 'xdata = np.asarray (df.iloc [:, 0])'등등 ... 일반적으로 나는 numpy 변환과리스트 이해를 혼합하지 말 것을 권한다. –
답변 중 하나가 도움이 되었습니까? 승인 된 답변으로 표시하고, 그렇지 않은 경우 불명 사항이 있으면 언제든지 물어보십시오. –