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 = [333,500,1000,2000,5000,10000]
y = [195.3267, 233.0235, 264.5914,294.8728, 328.3523,345.4688]
popt, pcov = curve_fit(func, x, y)
plt.figure()
plt.plot(x, y, 'ko', label="Original Noised Data")
plt.plot(x, func(x, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show()错误: C:\Users\Aidan\Anaconda3\lib\site-packages\scipy\optimize\minpack.py:794: OptimizeWarning:无法估计参数的协方差 category=OptimizeWarning) 跟踪(最近一次调用) in () 14 plt.figure() 15 plt.plot(x,y,'ko',Label=“原始噪声数据”- 16 plt.plot(x,func(x,*popt),'r-',label=“拟合曲线”) 17 plt.legend() 18 plt.show() 在func(x,a,b,c) 4 5 def func(x,a,b,c):-返回a* np.exp(-b * x) +c78x= 333,500,1000,2000,5000,10000 TypeError:“numpy.float64”对象不能解释为整数
由于某些原因,我无法根据我的数据得到曲线拟合。我遵循这里的指数例子:How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting
但我使用的是两个数组,而不是随机数据。我对蟒蛇很陌生!
发布于 2018-07-11 14:25:20
您的代码有几个问题。
numpy.ndarray:numpy和scipy例程是用来处理numpy.ndarray的,它们在内部使用它们。你也应该用它们。np.exp(-1000)在Python3中已接近于零以下代码初步解决了所有这些问题:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a * (1 - np.exp(-b * x)) + c
x = np.array([333.0,500.0,1000.0,2000.0,5000.0,10000.0]) / 1000
y = np.array([195.3267, 233.0235, 264.5914,294.8728, 328.3523,345.4688]) / 10
popt, pcov = curve_fit(func, x, y)
print(popt)
plt.figure()
plt.plot(x, y, 'ko', label="Original Noised Data")
plt.plot(x, func(x, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show()https://stackoverflow.com/questions/51287713
复制相似问题