- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试拟合看起来像这样的 x y 数据
x = np.linspace(-2, 2, 1000)
a = 0.5
yl = np.ones_like(x[x < a]) * -0.4 + np.random.normal(0, 0.05, x[x < a].shape[0])
yr = np.ones_like(x[x >= a]) * 0.4 + np.random.normal(0, 0.05, x[x >= a].shape[0])
y = np.concatenate((yl, yr))
plt.scatter(x, y, s=2, color='k')
我正在使用 Heaviside 阶跃函数的变体
def f(x, a, b): return 0.5 * b * (np.sign(x - a))
并拟合
popt, pcov = curve_fit(f, x, y, p0=p)
其中 p 是一些初始猜测。对于任何 p curve_fit 只适合 b 而不是 a例如:
popt, pcov = curve_fit(f, x, y, p0=[-1.0, 0])
我们得到 popt 是 [-1., 0.20117665]
popt, pcov = curve_fit(f, x, y, p0=[.5, 2])
我们得到 popt 是 [.5, 0.79902]
popt, pcov = curve_fit(f, x, y, p0=[1.5, -2])
我们得到 taht popt 是 [1.5, 0.40128229]
为什么 curve_fit 不拟合 a?
最佳答案
正如其他人所提到的,curve_fit
(以及scipy.optimize
中的所有其他求解器)可以很好地优化连续变量,但不能优化离散变量。它们的工作原理都是对参数值进行小的(例如,在 1.e-7 级别)更改,并查看结果中发生了什么(如果有)更改,并使用该更改来细化这些值,直到最小残差为成立。使用 np.sign
的模型函数:
def f(x, a, b): return 0.5 * b * (np.sign(x - a))
a
值的如此小的变化根本不会改变模型或拟合结果。也就是说,首先拟合将尝试起始值,例如 a=-1.0
或 a=0.5
,然后尝试 a=-0.999999995
或 a=0.500000005
。对于 np.sign(x-a)
,它们都会给出相同的结果。拟合不知道需要将 a
更改 1 才能对结果产生任何影响。它不可能知道这一点。 np.sign()
和 np.sin()
相差一个字母,但在这方面的行为却截然不同。
真实数据采取一步是很常见的,但采样足够精细,以便该一步不会完全在一步中发生。在这种情况下,您将能够使用各种函数形式(线性斜坡、误差函数、反正切、逻辑等)对步骤进行建模。 @JamesPhilipps 的彻底回答给出了一种方法。我可能会使用 lmfit (作为其主要作者之一)并愿意通过查看数据来猜测参数的起始值,也许:
import numpy as np
x = np.linspace(-2, 2, 1000)
a = 0.5
yl = np.ones_like(x[x < a]) * -0.4 + np.random.normal(0, 0.05, x[x < a].shape[0])
yr = np.ones_like(x[x >= a]) * 0.4 + np.random.normal(0, 0.05, x[x >= a].shape[0])
y = np.concatenate((yl, yr))
from lmfit.models import StepModel, ConstantModel
model = StepModel() + ConstantModel()
params = model.make_params(center=0, sigma=1, amplitude=1., c=-0.5)
result = model.fit(y, params, x=x)
print(result.fit_report())
import matplotlib.pyplot as plt
plt.scatter(x, y, label='data')
plt.plot(x, result.best_fit, marker='o', color='r', label='fit')
plt.show()
这将提供良好的拟合并打印出结果
[[Model]]
(Model(step, form='linear') + Model(constant))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 50
# data points = 1000
# variables = 4
chi-square = 2.32729556
reduced chi-square = 0.00233664
Akaike info crit = -6055.04839
Bayesian info crit = -6035.41737
## Warning: uncertainties could not be estimated:
[[Variables]]
amplitude: 0.80013762 (init = 1)
center: 0.50083312 (init = 0)
sigma: 4.6009e-04 (init = 1)
c: -0.40006255 (init = -0.5)
请注意,它会找到步骤的中心
,因为它假设步骤有一些有限的宽度(sigma
),但随后发现宽度更小比 x
中的步长大。但还要注意,它无法计算参数中的不确定性,因为如上所述,解附近的 center
(您的 a
)的微小变化不会改变最终的拟合。 FWIW StepModel 可以使用线性、误差函数、反正切或逻辑作为阶跃函数。
如果您构建的测试数据具有较小的步长宽度,请说类似的东西
from scipy.special import erf
y = 0.638 * erf((x-0.574)/0.005) + np.random.normal(0, 0.05, len(x))
然后拟合就能够找到最佳解决方案并评估不确定性。
我希望这能解释为什么与模型函数的拟合无法细化 a
的值,以及可以采取哪些措施。
关于python - 使用 scipy Optimize curve_fit 拟合步骤位置变化的步骤函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479443/
我在使用 cx_freeze 和 scipy 时无法编译 exe。特别是,我的脚本使用 from scipy.interpolate import griddata 构建过程似乎成功完成,但是当我尝试
是否可以通过函数在 scipy 中定义一个稀疏矩阵,而不是列出所有可能的值?在文档中,我看到可以通过以下方式创建稀疏矩阵 There are seven available sparse matrix
SciPy为非线性最小二乘问题提供了两种功能: optimize.leastsq()仅使用Levenberg-Marquardt算法。 optimize.least_squares()允许我们选择Le
SciPy 中的求解器能否处理复数值(即 x=x'+i*x")?我对使用 Nelder-Mead 类型的最小化函数特别感兴趣。我通常是 Matlab 用户,我知道 Matlab 没有复杂的求解器。如果
我有看起来像这样的数据集: position number_of_tag_at_this_position 3 4 8 6 13 25 23 12 我想对这个数据集应用三次样条插值来插值标签密度;为此
所以,我正在处理维基百科转储,以计算大约 5,700,000 个页面的页面排名。这些文件经过预处理,因此不是 XML 格式。 它们取自 http://haselgrove.id.au/wikipedi
Scipy 和 Numpy 返回归一化的特征向量。我正在尝试将这些向量用于物理应用程序,我需要它们不被标准化。 例如a = np.matrix('-3, 2; -1, 0') W,V = spl.ei
基于此处提供的解释 1 ,我正在尝试使用相同的想法来加速以下积分: import scipy.integrate as si from scipy.optimize import root, fsol
这很容易重新创建。 如果我的脚本 foo.py 是: import scipy 然后运行: python pyinstaller.py --onefile foo.py 当我启动 foo.exe 时,
我想在我的代码中使用 scipy.spatial.distance.cosine。如果我执行类似 import scipy.spatial 或 from scipy import spatial 的操
Numpy 有一个基本的 pxd,声明它的 c 接口(interface)到 cython。是否有用于 scipy 组件(尤其是 scipy.integrate.quadpack)的 pxd? 或者,
有人可以帮我处理 scipy.stats.chisquare 吗?我没有统计/数学背景,我正在使用来自 https://en.wikipedia.org/wiki/Chi-squared_test 的
我正在使用 scipy.odr 拟合数据与权重,但我不知道如何获得拟合优度或 R 平方的度量。有没有人对如何使用函数存储的输出获得此度量有建议? 最佳答案 res_var Output 的属性是所谓的
我刚刚下载了新的 python 3.8,我正在尝试使用以下方法安装 scipy 包: pip3.8 install scipy 但是构建失败并出现以下错误: **Failed to build sci
我有 my own triangulation algorithm它基于 Delaunay 条件和梯度创建三角剖分,使三角形与梯度对齐。 这是一个示例输出: 以上描述与问题无关,但对于上下文是必要的。
这是一个非常基本的问题,但我似乎找不到好的答案。 scipy 到底计算什么内容 scipy.stats.norm(50,10).pdf(45) 据我了解,平均值为 50、标准差为 10 的高斯中像 4
我正在使用 curve_fit 来拟合一阶动态系统的阶跃响应,以估计增益和时间常数。我使用两种方法。第一种方法是在时域中拟合从函数生成的曲线。 # define the first order dyn
让我们假设 x ~ Poisson(2.5);我想计算类似 E(x | x > 2) 的东西。 我认为这可以通过 .dist.expect 运算符来完成,即: D = stats.poisson(2.
我正在通过 OpenMDAO 使用 SLSQP 来解决优化问题。优化工作充分;最后的 SLSQP 输出如下: Optimization terminated successfully. (Exi
log( VA ) = gamma - (1/eta)log[alpha L ^(-eta) + 测试版 K ^(-eta)] 我试图用非线性最小二乘法估计上述函数。我为此使用了 3 个不同的包(Sc
我是一名优秀的程序员,十分优秀!