Python curve_fit 带有测量数据点
Python curve_fit with measured data points
我测量了数据点,我想根据一个公式来确定两个实体。但是我收到错误:
TypeError:输入类型不支持 ufunc 'bitwise_xor',根据转换规则“'safe'”
,无法将输入安全地强制转换为任何受支持的类型
由以下 python 代码生成(我使用版本 3):
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(T, fun, Tc):
return fun*np.sqrt(np.cos(np.pi/2*(T/Tc)^2))
xdata=(4.61, 4.89, 4.92, 4.95, 5.06, 5.10, 5.21, 5.38, 5.41, 5.57, 5.80, 6.14, 6.61, 7.27, 7.66, 7.90, 8.91, 8.29, 8.23, 7.30, 7.86,
8.30, 8.89, 8.99, 9.24, 9.35, 9.50, 8.77, 8.27, 8.37, 7.72, 7.57, 7.99, 8.13) # these are temperature values <-> T
ydata=(2.85, 2.84, 2.83, 2.825, 2.82, 2.81, 2.80, 2.765, 2.76, 2.74, 2.695, 2.62, 2.50, 2.265, 2.105, 1.975, 1.23, 1.75, 1.81, 2.26,
2.005, 1.75, 1.31, 1.14, 1.015, 1.045, 1.06, 1.40, 1.75, 1.69, 2.075, 2.15, 1.93, 1.855) # these are energy values <-> func
popt, pcov = curve_fit(func, xdata, ydata)
popt #display these optimized values
出现上面的错误!!!
我看到了一种方法,如果你有一个固定的公式并添加一些噪音,但我已经测量了数据点(它们必须是离散的)。
谢谢!
卡斯滕
我认为您看到的错误的很大一部分是您没有为拟合变量 fun
和 Tc
提供初始值。不幸的是 scipy 的 curve_fit()
允许这样做并默默地将值分配为 1.0,这会鼓励不良做法并且是非常糟糕的 "feature"。不要使用它。
请允许我推荐 lmfit (https://lmfit.github.io/lmfit-py),它提供了更高级的曲线拟合接口,更易于使用,更善于避免不良行为,并且具有许多 [=14= 不具备的有用功能].
使用 lmfit,您的拟合问题将如下所示(为清楚起见,我更改了几个变量名称):
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
def func(t, scale, tc):
return scale*np.sqrt(np.cos(np.pi/2*(t/tc)**2))
tdata = np.array([4.61, 4.89, 4.92, 4.95, 5.06, 5.10, 5.21, 5.38, 5.41, 5.57, 5.80, 6.14, 6.61, 7.27, 7.66, 7.90, 8.91, 8.29, 8.23, 7.30, 7.86,
8.30, 8.89, 8.99, 9.24, 9.35, 9.50, 8.77, 8.27, 8.37, 7.72, 7.57, 7.99, 8.13]) # these are temperature values <-> T
energy = np.array([2.85, 2.84, 2.83, 2.825, 2.82, 2.81, 2.80, 2.765, 2.76, 2.74, 2.695, 2.62, 2.50, 2.265, 2.105, 1.975, 1.23, 1.75, 1.81, 2.26,
2.005, 1.75, 1.31, 1.14, 1.015, 1.045, 1.06, 1.40, 1.75, 1.69, 2.075, 2.15, 1.93, 1.855]) # these are energy values <-> func
fmodel = Model(func)
params = fmodel.make_params(scale=5, tc=10)
result = fmodel.fit(energy, params, t=tdata)
print(result.fit_report())
plt.plot(tdata, energy, 'o', label='data')
plt.plot(tdata, result.best_fit, '+', label='fit')
plt.legend()
plt.show()
这将打印出
的报告
[[Model]]
Model(func)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 24
# data points = 34
# variables = 2
chi-square = 0.34988407
reduced chi-square = 0.01093388
Akaike info crit = -151.601474
Bayesian info crit = -148.548753
[[Variables]]
scale: 2.87776739 +/- 0.02737439 (0.95%) (init = 5)
tc: 9.68051725 +/- 0.03597889 (0.37%) (init = 10)
[[Correlations]] (unreported correlations are < 0.100)
C(scale, tc) = -0.506
并生成类似
的图表
我测量了数据点,我想根据一个公式来确定两个实体。但是我收到错误:
TypeError:输入类型不支持 ufunc 'bitwise_xor',根据转换规则“'safe'”
,无法将输入安全地强制转换为任何受支持的类型由以下 python 代码生成(我使用版本 3):
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(T, fun, Tc):
return fun*np.sqrt(np.cos(np.pi/2*(T/Tc)^2))
xdata=(4.61, 4.89, 4.92, 4.95, 5.06, 5.10, 5.21, 5.38, 5.41, 5.57, 5.80, 6.14, 6.61, 7.27, 7.66, 7.90, 8.91, 8.29, 8.23, 7.30, 7.86,
8.30, 8.89, 8.99, 9.24, 9.35, 9.50, 8.77, 8.27, 8.37, 7.72, 7.57, 7.99, 8.13) # these are temperature values <-> T
ydata=(2.85, 2.84, 2.83, 2.825, 2.82, 2.81, 2.80, 2.765, 2.76, 2.74, 2.695, 2.62, 2.50, 2.265, 2.105, 1.975, 1.23, 1.75, 1.81, 2.26,
2.005, 1.75, 1.31, 1.14, 1.015, 1.045, 1.06, 1.40, 1.75, 1.69, 2.075, 2.15, 1.93, 1.855) # these are energy values <-> func
popt, pcov = curve_fit(func, xdata, ydata)
popt #display these optimized values
出现上面的错误!!!
我看到了一种方法,如果你有一个固定的公式并添加一些噪音,但我已经测量了数据点(它们必须是离散的)。
谢谢! 卡斯滕
我认为您看到的错误的很大一部分是您没有为拟合变量 fun
和 Tc
提供初始值。不幸的是 scipy 的 curve_fit()
允许这样做并默默地将值分配为 1.0,这会鼓励不良做法并且是非常糟糕的 "feature"。不要使用它。
请允许我推荐 lmfit (https://lmfit.github.io/lmfit-py),它提供了更高级的曲线拟合接口,更易于使用,更善于避免不良行为,并且具有许多 [=14= 不具备的有用功能].
使用 lmfit,您的拟合问题将如下所示(为清楚起见,我更改了几个变量名称):
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
def func(t, scale, tc):
return scale*np.sqrt(np.cos(np.pi/2*(t/tc)**2))
tdata = np.array([4.61, 4.89, 4.92, 4.95, 5.06, 5.10, 5.21, 5.38, 5.41, 5.57, 5.80, 6.14, 6.61, 7.27, 7.66, 7.90, 8.91, 8.29, 8.23, 7.30, 7.86,
8.30, 8.89, 8.99, 9.24, 9.35, 9.50, 8.77, 8.27, 8.37, 7.72, 7.57, 7.99, 8.13]) # these are temperature values <-> T
energy = np.array([2.85, 2.84, 2.83, 2.825, 2.82, 2.81, 2.80, 2.765, 2.76, 2.74, 2.695, 2.62, 2.50, 2.265, 2.105, 1.975, 1.23, 1.75, 1.81, 2.26,
2.005, 1.75, 1.31, 1.14, 1.015, 1.045, 1.06, 1.40, 1.75, 1.69, 2.075, 2.15, 1.93, 1.855]) # these are energy values <-> func
fmodel = Model(func)
params = fmodel.make_params(scale=5, tc=10)
result = fmodel.fit(energy, params, t=tdata)
print(result.fit_report())
plt.plot(tdata, energy, 'o', label='data')
plt.plot(tdata, result.best_fit, '+', label='fit')
plt.legend()
plt.show()
这将打印出
的报告[[Model]]
Model(func)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 24
# data points = 34
# variables = 2
chi-square = 0.34988407
reduced chi-square = 0.01093388
Akaike info crit = -151.601474
Bayesian info crit = -148.548753
[[Variables]]
scale: 2.87776739 +/- 0.02737439 (0.95%) (init = 5)
tc: 9.68051725 +/- 0.03597889 (0.37%) (init = 10)
[[Correlations]] (unreported correlations are < 0.100)
C(scale, tc) = -0.506
并生成类似
的图表