Restricting values for curve_fit (scipy.optimize)

拜拜、爱过 提交于 2019-12-09 11:17:22

问题


I'm trying to fit a logistic growth curve to my data using curve_fit using the following function as the input.

def logistic(x, y0, k, d, a, b):
    if b > 0 and a > 0:
        y = (k * pow(1 + np.exp(d - (a * b * x) ), (-1/b) )) + y0
    elif b >= -1 or b < 0 or a < 0:
        y = (k * pow(1 - np.exp(d - (a * b * x) ), (-1/b) )) + y0

    return y

As you can see the function i am using has some restrictions on the values it can accept for parameter a and b. Any guess on how to handle the incorrect values? Should the input function raise an exception or return a dummy value? Thanks in advance.


回答1:


When the parameters fall out of the admissible range, return a wildly huge number (far from the data to be fitted). This will (hopefully) penalize this choice of parameters so much that curve_fit will settle on some other admissible set of parameters as optimal:

def logistic(x, y0, k, d, a, b):
    if b > 0 and a > 0:
        y = (k * pow(1 + np.exp(d - (a * b * x) ), (-1/b) )) + y0
    elif b >= -1 or b < 0 or a < 0:
        y = (k * pow(1 - np.exp(d - (a * b * x) ), (-1/b) )) + y0
    else:
        y = 1e10
    return y


来源:https://stackoverflow.com/questions/9518290/restricting-values-for-curve-fit-scipy-optimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!