有没有办法确定scipy curve_fit的确定性?

时间:2019-01-15 14:57:20

标签: python scipy curve-fitting

尝试使用带有固定种子的numpy的curve_fit(scipy API,用于拟合Sigmoid),但结果仍然有所不同。 有什么方法可以使它完全具有确定性吗?

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

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))

似乎积分的顺序很重要。出于好奇,这背后的原因是什么?

1 个答案:

答案 0 :(得分:0)

如果在运行曲线拟合算法之前按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