不幸的是,scipy的幂拟合不能返回良好的拟合。我尝试将p0用作具有接近值的输入参数,但没有帮助。

如果有人可以向我指出我的问题,我将感到非常高兴。

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

# Data
data = [[0.004408724185371062, 78.78011887652593], [0.005507091456466967, 65.01330508350753], [0.007073553026306459, 58.13364205119446], [0.009417452253958304, 50.12258366028477], [0.01315330108197482, 44.22980301062208], [0.019648758406406834, 35.436139354228956], [0.03248060063099905, 28.359815190205957], [0.06366197723675814, 21.54769216720596], [0.17683882565766149, 14.532777174472574], [1.5915494309189533, 6.156872080264581]]

# Fill lists to store x and y value
x_data,y_data = [], []
for i in data:
    x_data.append(i[0])
    y_data.append(i[1])

# Exponential Function
def func(x,m,c):
        return x**m * c

# Curve fit
coeff, _ = curve_fit(func, x_data, y_data)
m, c = coeff[0], coeff[1]

# Plot function
x_function = np.linspace(0, 1.5, 100)
y = x_function**m * c
a = plt.scatter(x_data, y_data, s=30, marker = "v")
yfunction = x_function**m * c
plt.plot(x_function, yfunction, '-')
plt.show()


拟合真的很差的另一个数据集是:

data = [[0.004408724185371062, 194.04075083542443], [0.005507091456466967, 146.09194314074864], [0.007073553026306459, 120.2115882821158], [0.009417452253958304, 74.04014371874908], [0.01315330108197482, 34.167114633194736], [0.019648758406406834, 12.775528348369871], [0.03248060063099905, 7.903195816871708], [0.06366197723675814, 5.186092050500438], [0.17683882565766149, 3.260540592404184], [1.5915494309189533, 2.006254812978579]]

最佳答案

我可能会错过一些东西,但我认为curve_fit可以正常工作。当我将curve_fit获得的残差与使用您在注释中提供的excel获得的参数所获得的残差进行比较时,python结果总是导致残差更低(下面提供了代码)。您说:“不幸的是,与scipy匹配的力量并没有返回良好的匹配。”但是,“合适”的衡量标准到底是什么?就残差而言,python拟合似乎总是比excel拟合更好。

不知道它是否必须确切地是此函数,如果不是,则还可以考虑在函数中添加第三个参数(以下称为“ d”),这将导致更好的结果。

这是修改后的代码。我更改了您的“功能”,还增加了情节的分辨率。然后,残渣也将被打印出来。对于第一个数据集,人们获得了大约79.35左右的excel和大约34.29的python。对于第二个数据集,使用excel时为15220.79,使用python时为601.08(假设我没有弄乱任何东西)。

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

# Data
data = [[0.004408724185371062, 78.78011887652593], [0.005507091456466967, 65.01330508350753], [0.007073553026306459, 58.13364205119446], [0.009417452253958304, 50.12258366028477], [0.01315330108197482, 44.22980301062208], [0.019648758406406834, 35.436139354228956], [0.03248060063099905, 28.359815190205957], [0.06366197723675814, 21.54769216720596], [0.17683882565766149, 14.532777174472574], [1.5915494309189533, 6.156872080264581]]
#data = [[0.004408724185371062, 194.04075083542443], [0.005507091456466967, 146.09194314074864], [0.007073553026306459, 120.2115882821158], [0.009417452253958304, 74.04014371874908], [0.01315330108197482, 34.167114633194736], [0.019648758406406834, 12.775528348369871], [0.03248060063099905, 7.903195816871708], [0.06366197723675814, 5.186092050500438], [0.17683882565766149, 3.260540592404184], [1.5915494309189533, 2.006254812978579]]
# Fill lists to store x and y value
x_data,y_data = [], []
for i in data:
    x_data.append(i[0])
    y_data.append(i[1])

# Exponential Function
def func(x,m,c):
    #slightly rewritten; you could also consider using a third parameter d
    return c*np.power(x,m) #  + d

# Curve fit
coeff, _ = curve_fit(func, x_data, y_data)
m, c = coeff[0], coeff[1] #, coeff[2]
print m, c #, d

# Plot function
a = plt.scatter(x_data, y_data, s=30, marker = "v")
x_function = np.linspace(0, 1.5, 1000)
yfunction = c*np.power(x_function,m) # + d
plt.plot(x_function, yfunction, '-')
plt.show()
print "residuals python:",((y_data - func(x_data, *coeff))**2).sum()
#compare to excel, first data set
print "residuals excel:",((y_data - func(x_data, -0.425,7.027))**2).sum()
#compare to excel, second data set
print "residuals excel:",((y_data - func(x_data, -0.841,1.0823))**2).sum()

关于python - 错误的指数幂图-如何改善曲线拟合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30996265/

10-14 08:21