gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:59:34 26 4
gpt4 key购买 nike

尝试使用 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))

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

最佳答案

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

关于python - 有没有办法使 scipy curve_fit 具有确定性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54201403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com