限制curve_fit的值(剪刀。优化)

时间:2021-03-03 21:24:28

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

我正在尝试用curve_fit将逻辑斯蒂增长曲线拟合到我的数据中,使用下面的函数作为输入。

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.

正如您所看到的,我正在使用的函数对参数a和b可以接受的值有一些限制。输入函数应该引发异常还是返回一个虚拟值?提前谢谢。

1 个解决方案

#1


7  

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:

当参数超出允许范围时,返回一个非常大的数字(远离要拟合的数据)。这将(希望)对参数选择的惩罚如此之大,以至于curve_fit将把一些其他可接受的参数集确定为最优:

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

#1


7  

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:

当参数超出允许范围时,返回一个非常大的数字(远离要拟合的数据)。这将(希望)对参数选择的惩罚如此之大,以至于curve_fit将把一些其他可接受的参数集确定为最优:

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