使用scipy\u optimiz进行优化

2024-09-29 23:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用曲线拟合来优化函数科学优化. 这是我的密码。你知道吗

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()。这是指函数列表ofoneminuskj(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))

Tags: inforreturnifdefasnprow
2条回答

Scipy Optimizes Curve Fitting Procedure尝试将扩展数据的完整向量赋给函数func。你把它传给listofOneMinusKj。你知道吗

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

"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的所有值是否都大于a。你知道吗

调试这类问题的一种方法是在回溯指向的行上方抛出import pdb; pdb.set_trace()咒语。然后运行您的代码,它将在断点处停止—您可以交互式地浏览各种对象并逐步完成代码。在这里,您可能会发现,当通过曲线拟合调用时,t或a是一个numpy数组,您不能只执行if array > another_array。你知道吗

相关问题 更多 >

    热门问题