简体   繁体   English

scipy.optimize curve_fit 的麻烦

[英]Troubels with scipy.optimize curve_fit

I'm having some troubels fitting a curve with the scipy optimize package.我有一些 troubels 用 scipy 优化包拟合曲线。 My code is:我的代码是:

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)

I amb getting popt = [1,1], so the optimitzation is not working.我得到 popt = [1,1],所以优化不起作用。 I've done the "same" in R and I'm exepcting popt = [44237.53, 22.21] aprox.我已经在 R 中完成了“相同的”,并且我正在执行 popt = [44237.53, 22.21] aprox。

Could anyone help me with that please?有人可以帮我吗?

Many thanks!非常感谢!

Xevi西维

There are two problems:有两个问题:

  1. definition of function功能定义
  2. x array need to by starting from 0 x 数组需要从0开始

I have flipped you data values and add bounds to the fit algorithm我已经翻转了您的数据值并为拟合算法添加了界限

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

Output:输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 限制curve_fit的值(scipy.optimize) - Restricting values for curve_fit (scipy.optimize) 使用scipy.optimize的curve_fit - Using scipy.optimize's curve_fit 如何引用scipy.optimize的curve_fit()(方法,函数?) - How to refer to the curve_fit() (method, function?) of scipy.optimize 使用 scipy.optimize curve_fit 找到曲线的参数并得到“无法估计参数的协方差” - using scipy.optimize curve_fit to find parameters of a curve and getting 'Covariance of the parameters could not be estimated' 如何使用 scipy.optimize 中的 curve_fit 和跨多个数据集的共享拟合参数? - How to use curve_fit from scipy.optimize with a shared fit parameter across multiple datasets? scipy.optimize curve_fit返回错误的值(取决于机器) - scipy.optimize curve_fit returns wrong value (depends on the machine) Python - R平方和可通过scipy.optimize curve_fit获得的绝对平方和? - Python - R square and absolute sum of squares obtainable by scipy.optimize curve_fit? 未能将初始参数元组传递到 scipy.optimize curve_fit - Failing to pass a tuple of initial parameters into scipy.optimize curve_fit scipy.optimize中python中curve_fit和leastsq之间的区别 - Difference between curve_fit and leastsq in python from scipy.optimize 如何修复 Scipy.optimize curve_fit 的“OptimizeWarning:无法估计参数的协方差”? - How to fix the "OptimizeWarning: Covariance of the parameters could not be estimated" for Scipy.optimize curve_fit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM