- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这些值(value)观:
T_values = (222, 284, 308.5, 333, 358, 411, 477, 518, 880, 1080, 1259) (x values)
C/(3Nk)_values = (0.1282, 0.2308, 0.2650, 0.3120 , 0.3547, 0.4530, 0.5556, 0.6154, 0.8932, 0.9103, 0.9316) (y values)
我知道他们遵循以下模型:
C/(3Nk)=(h*w/(k*T))**2*(exp(h*w/(k*T)))/(exp(h*w/(k*T)-1))**2
我也知道 k=1.38*10**(-23)
和 h=6.626*10**(-34)
。我必须找到最能描述测量数据的 w。我想在 python 中使用最小二乘法来解决这个问题,但是我真的不明白它是如何工作的。谁能帮帮我?
最佳答案
此答案提供了使用 Python 确定一般指数模式的拟合参数的演练。另请参阅 linearization techniques 上的相关帖子并使用 lmfit
图书馆。
首先,让我们将采样数据输入并组织为 numpy 数组,这将有助于稍后的计算和清晰度。
import matplotlib.pyplot as plt
import scipy.optimize as opt
import numpy as np
#% matplotlib inline
# DATA ------------------------------------------------------------------------
T_values = np.array([222, 284, 308.5, 333, 358, 411, 477, 518, 880, 1080, 1259])
C_values = np.array([0.1282, 0.2308, 0.2650, 0.3120 , 0.3547, 0.4530, 0.5556, 0.6154, 0.8932, 0.9103, 0.9316])
x_samp = T_values
y_samp = C_values
有很多curve fitting scipy 和 numpy 中的函数,每个函数的用法不同,例如scipy.optimize.leastsq
和 scipy.optimize.least_squares
.为简单起见,我们将使用 scipy.optimize.curve_fit
,但如果不选择合理的起始参数,很难找到优化的回归曲线。稍后将在选择起始参数时演示一种简单的技术。
首先,尽管 OP 提供了预期的拟合方程,但我们将通过回顾指数函数的一般方程来解决使用 Python 进行曲线拟合的问题:
现在我们构建这个通用函数,它会被使用几次:
# GENERAL EQUATION ------------------------------------------------------------
def func(x, A, c, d):
return A*np.exp(c*x) + d
趋势
A
给出小的振幅c
通过压平曲线的“拐点”来控制形状d
设置 y 截距A
在水平轴上翻转曲线;负的 c
翻转垂直轴上的曲线后者的趋势如下图所示,突出显示了对照(黑线)与具有不同参数的线(红线):
使用后一种趋势,让我们接下来查看数据并尝试通过调整这些参数来模拟曲线。为了演示,我们根据数据绘制了几个试验方程:
# SURVEY ----------------------------------------------------------------------
# Plotting Sampling Data
plt.plot(x_samp, y_samp, "ko", label="Data")
x_lin = np.linspace(0, x_samp.max(), 50) # a number line, 50 evenly spaced digits between 0 and max
# Trials
A, c, d = -1, -1e-2, 1
y_trial1 = func(x_lin, A, c, d)
y_trial2 = func(x_lin, -1, -1e-3, 1)
y_trial3 = func(x_lin, -1, -3e-3, 1)
plt.plot(x_lin, y_trial1, "--", label="Trial 1")
plt.plot(x_lin, y_trial2, "--", label="Trial 2")
plt.plot(x_lin, y_trial3, "--", label="Trial 3")
plt.legend()
通过简单的试错,我们可以更好地近似曲线的形状、幅度、位置和方向。例如,我们知道前两个参数(A
和 c
)必须为负数。我们对 c
的数量级也有一个合理的猜测。
我们现在将使用最佳试验的参数进行初始猜测:
# REGRESSION ------------------------------------------------------------------
p0 = [-1, -3e-3, 1] # guessed params
w, _ = opt.curve_fit(func, x_samp, y_samp, p0=p0)
print("Estimated Parameters", w)
# Model
y_model = func(x_lin, *w)
# PLOT ------------------------------------------------------------------------
# Visualize data and fitted curves
plt.plot(x_samp, y_samp, "ko", label="Data")
plt.plot(x_lin, y_model, "k--", label="Fit")
plt.title("Least squares regression")
plt.legend(loc="upper left")
# Estimated Parameters [-1.66301087 -0.0026884 1.00995394]
curve_fit
是众多 optimization functions 之一由 scipy 提供。给定一个初始值,得到的估计参数被迭代地细化,使得得到的曲线最小化残差,或拟合线和采样数据之间的差异。更好的猜测减少了迭代次数并加快了结果的速度。有了这些拟合曲线的估计参数,现在可以计算特定方程的特定系数(留给 OP 的最后练习)。
关于python - python中的最小二乘法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616993/
我是一名优秀的程序员,十分优秀!