有没有办法使 scipy curve_fit 具有确定性?

mbi*_*son 6 python curve-fitting scipy

尝试使用 curve_fit (scipy API,用于拟合 sigmoid)和 numpy 的固定种子,但结果仍然有所不同。有没有办法让它完全确定性?

根据评论中的要求,这是一个最小的工作示例:

from scipy.optimize import curve_fit
import numpy as np

def sigmoid(x, b, mu, max_kr):
    if isinstance(x, list) or isinstance(x, np.ndarray):
        return [sigmoid(xx, b, mu, max_kr) for xx in x]
    else:
        return max_kr/(1+10**(mu*(-x+b)))

def fit_sigmoid(points):
    xs, ys = list(zip(*points))
    err = None
    popt, pcov = curve_fit(sigmoid, xs, ys, bounds=([-np.inf, 0, 0],    [np.inf, np.inf, 1]), ftol=len(xs)*1e-6)
    b, mu, max_kr = popt
    return mu

np.random.seed = 12
points1 = [(4.0, 1.0), (1.0, 8.340850913002296e-05), (3.0, 0.9793319563421965), (0.0, 8.340850913002296e-05), (-1.0, 0.0), (2.0, 0.010306481917677357)]
points2 = [(4.0, 1.0), (-1.0, 0.0), (3.0, 0.9793319563421965), (0.0, 8.340850913002296e-05), (1.0, 8.340850913002296e-05), (2.0, 0.010306481917677357)]
print(fit_sigmoid(points1))
print(fit_sigmoid(points2))
Run Code Online (Sandbox Code Playgroud)

看来点的顺序很重要。出于好奇,这背后的原因是什么?

Nil*_*ner 1

如果在运行曲线拟合算法之前按 x 对数据进行排序,您将获得可重现的结果:

from scipy.optimize import curve_fit
import numpy as np

def sigmoid(x, b, mu, max_kr):
    if isinstance(x, list) or isinstance(x, np.ndarray):
        return [sigmoid(xx, b, mu, max_kr) for xx in x]
    else:
        return max_kr/(1+10**(mu*(-x+b)))

def fit_sigmoid(points):
    points = points[points[:, 0].argsort()]
    popt, pcov = curve_fit(sigmoid, points[:, 0], points[:, 1], bounds=([-np.inf, 0, 0],    [np.inf, np.inf, 1]), ftol=len(points)*1e-6)
    b, mu, max_kr = popt
    return mu

points1 = np.array([
    (4.0, 1.0),
    (1.0, 8.340850913002296e-05),
    (3.0, 0.9793319563421965),
    (0.0, 8.340850913002296e-05),
    (-1.0, 0.0),
    (2.0, 0.010306481917677357)
])
points2 = np.array([
    (4.0, 1.0),
    (-1.0, 0.0),
    (3.0, 0.9793319563421965),
    (0.0, 8.340850913002296e-05),
    (1.0, 8.340850913002296e-05),
    (2.0, 0.010306481917677357)
])
print(fit_sigmoid(points1))
print(fit_sigmoid(points2))
# 15.110203876634552
# 15.110203876634552
Run Code Online (Sandbox Code Playgroud)

  • 虽然这确实解决了问题,但它似乎没有回答发布者关于为什么顺序对 curve_fit 很重要的问题 (4认同)