麻烦与scipy.optimize curve_fit

问题描述 投票:0回答:1

我有一些麻烦的东西用scipy优化包来拟合曲线。我的代码是:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def Function_EXD_2(x, d, e):
    return d*np.exp(-x/e)

x = np.array([135, 126, 120, 100, 90, 85, 80, 70, 65, 60])
y = np.array([207, 263, 401, 460, 531, 576, 1350, 2317, 2340, 2834])

popt, pcov = curve_fit(Function_EXD_2, x, y)
print(popt, pcov)

Imbing popt = [1,1],因此优化无效。我已经在R中完成了“相同”操作,并且正在执行popt = [44237.53,22.21] aprox。

有人可以帮我吗?

非常感谢!

Xevi

python scipy curve-fitting
1个回答
0
投票

功能定义

    x数组需要从0开始
  • 我已经为您翻转了数据值,并为拟合算法添加了边界
  • import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit def func(x, a, b, c): return a * np.exp(-b * x) + c x = np.array([135, 126, 120, 100, 90, 85, 80, 70, 65, 60]) y = np.array([207, 263, 401, 460, 531, 576, 1350, 2317, 2340, 2834]) # flip array values x = x[::-1] - np.amin(x) y = y[::-1] # fit function popt, pcov = curve_fit(func, x, y, bounds=(-10**6, 10**6)) # plot data x_data = np.linspace(1, 80, 100) plt.plot(x, y, '.') plt.plot(x_data, func(x_data, popt[0], popt[1], popt[2])) plt.show()

    输出:

    enter image description here

  • © www.soinside.com 2019 - 2024. All rights reserved.