限制curve_fit的值(scipy.optimize) [英] Restricting values for curve_fit (scipy.optimize)

查看:187
本文介绍了限制curve_fit的值(scipy.optimize)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curve_fit使用以下函数作为输入来拟合逻辑增长曲线到我的数据。

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

如您所见,我使用的函数对它可以接受的参数a和a的值有一些限制。 b。关于如何处理不正确值的任何猜测?输入函数应该引发异常还是返回虚拟值?
预先感谢。

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.

推荐答案

当参数超出允许范围时,返回一个巨大的数字(与要拟合的数据相去甚远)。 (希望)对这种参数选择造成不利影响,以至于 curve_fit 将以其他一些可接受的最佳参数集为基础:

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

这篇关于限制curve_fit的值(scipy.optimize)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆