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)-------------------------------------------------- ---------------------------- TypeError跟踪(最近的呼叫 最后)在() 14点数字() 15 plt.plot(x,y,'ko',label =“原始噪声数据”) ---> 16个plt.plot(x,func(x,* popt),'r-',label =“ Fitted Curve”) 17 plt.legend() 18 plt.show()
在func(x,a,b,c)中 4 5 def func(x,a,b,c): ----> 6返回a * np.exp(-b * x)+ c 7 8 x = [333,500,1000,2000,5000,10000]
TypeError:“ numpy.float64”对象无法解释为整数
由于某种原因,我无法根据数据获得曲线拟合。我正在从此处跟踪指数示例:How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting
但是我使用的是两个数组,而不是随机数据。我是python新手!
答案 0 :(得分:1)
您的代码存在一些问题。
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()