我無法正確地將對數和指數衰減曲線擬合到我的實驗資料點,就好像建議的曲線擬合與我的資料中的模式不相似,甚至不是很遠。
我有以下示例資料:
data = {'X':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
],
'Y':[55, 55, 55, 54, 54, 54, 54, 53, 53, 50, 45, 37, 27, 16, 0
]}
df = pd.DataFrame(data)
df = pd.DataFrame(data,columns=['X','Y'])
df.plot(x ='X', y='Y', kind = 'scatter')
plt.show()
這輸出:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/d112cb3f972e4fb5b747d2284b78f267.png)
然后,我嘗試使用此代碼將指數衰減和對數衰減曲線擬合到這些資料點,并輸出每條曲線的均方根誤差:
# load the dataset
data = df.values
# choose the input and output variables
x, y = data[:, 0], data[:, 1]
def func1(x, a, b, c):
return a*exp(b*x) c
def func2(x, a, b):
return a * np.log(x) b
params, _ = curve_fit(func1, x, y)
a, b, c = params[0], params[1], params[2]
yfit1 = a*exp(x*b) c
rmse = np.sqrt(np.mean((yfit1 - y) ** 2))
print('Exponential decay fit:')
print('y = %.5f * exp(x*%.5f) %.5f' % (a, b, c))
print('RMSE:')
print(rmse)
print('')
params, _ = curve_fit(func2, x, y)
a, b = params[0], params[1]
yfit2 = a * np.log(x) b
rmse = np.sqrt(np.mean((yfit2 - y) ** 2))
print('Logarithmic decay fit:')
print('y = %.5f * ln(x) %.5f' % (a, b))
print('RMSE:')
print(rmse)
print('')
plt.plot(x, y, 'bo', label="y-original")
plt.plot(x, yfit1, label="y=a*exp(x*b) c")
plt.plot(x, yfit2, label="y=a * np.log(x) b")
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='best', fancybox=True, shadow=True)
plt.grid(True)
plt.show()
我收到這個輸出:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/366f5a56dffa4ca0be608f1d84bb30de.png)
然后我嘗試使用我的實驗資料,嘗試這些新資料點:
data = {'X':[0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480
],
'Y':[2.011399983,1.994139959,1.932761226,1.866343728,1.709889128,1.442674671,1.380548494,1.145193671,0.820646118,
0.582299012, 0.488162766, 0.264390575, 0.139457758, 0, 0, 0, 0
]}
df = pd.DataFrame(data)
df = pd.DataFrame(data,columns=['X','Y'])
df.plot(x ='X', y='Y', kind = 'scatter')
plt.show()
由此可見:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/5279d31514f94f94b043362532af6813.png)
然后我嘗試使用前面的代碼將指數衰減曲線和對數衰減曲線擬合到這些新資料點:
import pandas as pd
import numpy as np
from numpy import array, exp
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# load the dataset
data = df.values
# choose the input and output variables
x, y = data[:, 0], data[:, 1]
def func1(x, a, b, c):
return a*exp(b*x) c
def func2(x, a, b):
return a * np.log(x) b
params, _ = curve_fit(func1, x, y)
a, b, c = params[0], params[1], params[2]
yfit1 = a*exp(x*b) c
rmse = np.sqrt(np.mean((yfit1 - y) ** 2))
print('Exponential decay fit:')
print('y = %.5f * exp(x*%.5f) %.5f' % (a, b, c))
print('RMSE:')
print(rmse)
print('')
params, _ = curve_fit(func2, x, y)
a, b = params[0], params[1]
yfit2 = a * np.log(x) b
rmse = np.sqrt(np.mean((yfit2 - y) ** 2))
print('Logarithmic decay fit:')
print('y = %.5f * ln(x) %.5f' % (a, b))
print('RMSE:')
print(rmse)
print('')
plt.plot(x, y, 'bo', label="y-original")
plt.plot(x, yfit1, label="y=a*exp(x*b) c")
plt.plot(x, yfit2, label="y=a * np.log(x) b")
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='best', fancybox=True, shadow=True)
plt.grid(True)
plt.show()
我收到了這個看起來完全錯誤的輸出:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/9897364051ce4393af1a292cddbd1694.png)
And then I receive this plotted output which looks very far off from my experimental data points:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/9878002ed27840e094f01d7204fc3d2d.png)
I do not understand why my first curve fitting attempt worked so well and smoothly, while my second attempt seems to have turned into a huge incoherent mess that just broke the curve_fit function. I do not understand why I see the graph going into the negative y-axis if I do not have any negative y-axis values in my experimental data. I am confused because I can clearly see my experimental data plotted fine as just points, so I am not sure what is so wrong about it that I cannot simply fit my curves to the points. How can I address my code so that I can properly use curve_fit() to fit an exponential decay curve and a logarithmic decay curve to my experimental data points?
uj5u.com熱心網友回復:
正如評論中已經指出的那樣,該模型似乎是邏輯型別的。
與常用軟體擬合的主要困難是選擇引數的初始值來啟動迭代演算。![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/9d80f73e2424496a89d30e4c91c0f29f.jpg)
使用您的第二個資料:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/4789055e60db418b8b48e494d27e1e8c.gif)
使用您的第一個資料:
![由于“找不到最佳引數”錯誤[python],無法使用來自 scipy 的 curve_fit() 將曲線擬合到資料點](https://img.uj5u.com/2022/03/12/f7acff238d8f4036813532940e6272d5.gif)
如果您想要根據某些指定的擬合標準(MSE、MSRE、MAE 或其他)進行更準確的擬合,您可以將上述引數值作為非線性回歸軟體中的起始值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/441547.html
