科学计算科研数据分析【免费下载链接】astropyAstronomy and astrophysics core library项目地址https://gitcode.com/gh_mirrors/as/astropy点击查看免费下载导读astropy 的建模与拟合模块astropy.modeling在拟合前默认会校验输入坐标数组与待拟合数据数组的形状是否一致形状不匹配会直接抛出ValueError。自 20369 号变更起Model类新增了verify_dims_in_fitting类属性默认True允许模型显式退出该校验从而拟合那些合法地返回与输入不同形状输出的模型——例如用仪器响应函数做卷积、把结果重采样到更粗网格的模型。读完本文你将掌握该属性的默认行为、在普通模型与复合模型上的正确设置方式、底层实现原理_verify_dims_in_fitting与_convert_input的调用链以及一个可直接复制的完整拟合示例。背景拟合前的形状校验及其局限在 astropy 中无论使用线性拟合器还是非线性拟合器拟合前都会先对输入数据做一次维度一致性检查输入坐标x二维模型还有y与数据y/z必须具有相同的形状shape。这一约束源于最小二乘等拟合算法对输入输出的结构性假设——坐标与数据理应一一对应。这一默认校验由 astropy/modeling/fitting.py 中的_convert_input(x, y, z, *, verify_dimsTrue)实现。当verify_dimsTrue时代码会执行如下检查见 fitting.pyif z is None: if verify_dims and data_shape ! x.shape: raise ValueError(x and y should have the same shape) farg (x, y) else: if verify_dims and not (x.shape y.shape data_shape): raise ValueError(x, y and z should have the same shape) farg (x, y, z)也就是说1D 拟合在x与y形状不一致时抛出x and y should have the same shape2D 拟合在x、y、z三者形状不一致时抛出x, y and z should have the same shape。但有些模型会合法地改变数据形状比如一个先与仪器响应函数做卷积、再输出到更粗网格上的模型其输出长度与输入坐标长度天然不同。这类模型过去无法直接参与拟合必须绕过形状检查。verify_dims_in_fitting正是为这类场景提供的显式退出通道。新属性Model.verify_dims_in_fitting变更记录 docs/changes/modeling/20369.api.rst 指出Modelnow has averify_dims_in_fittingattribute, defaulting toTrue. Setting it toFalseon a model, or on any component of a compound model, skips the check that the input coordinates and the data have the same shape when fitting.该属性在 astropy/modeling/core.py 的Model类中作为类属性定义# This class attribute allows users to skip the dimension check which ensures # that input coordinates are the same dimensions as the data. It is set as True # by default ensuring that the default setting is to perform the dimension check. verify_dims_in_fitting True关键语义默认值为True即默认行为与历史版本完全一致——所有拟合器仍然执行形状校验存量代码不受影响值取False时模型自身的形状校验被跳过拟合器会带着形状不同的x/y继续运行它是类属性推荐在自定义模型类体class body中声明例如verify_dims_in_fitting False所有实例自动继承。配套的功能变更记录 docs/changes/modeling/20369.feature.rst 补充说明了设计意图该能力允许拟合返回数组形状与输入不同的模型典型如与仪器响应卷积后在更粗网格上返回结果的模型同时特别强调——对复合模型compound model该属性必须设置在某个组件模型上而不是设置在复合模型本身。底层实现_verify_dims_in_fitting如何解析退出标志verify_dims_in_fitting并不会被直接读取拟合器统一通过辅助函数_verify_dims_in_fitting(model)解析定义于 astropy/modeling/fitting.pydef _verify_dims_in_fitting(model): Resolve the verify_dims_in_fitting opt-out flag for model. if model is None: return True if hasattr(model, traverse_postorder): # i.e. a CompoundModel nodes model.traverse_postorder() else: nodes [model] return not any( getattr(node, verify_dims_in_fitting, True) is False for node in nodes )这段实现包含了三个重要设计决策普通模型直接把模型本身放进节点列表读其verify_dims_in_fitting复合模型通过traverse_postorder()见 astropy/modeling/core.py后序遍历整棵复合模型树检查每一个叶子组件任一叶子退出即整体退出只要复合模型中任何一个组件设置了False整个复合模型的形状校验就被关闭——这正是属性必须设置在组件上的底层原因也对应测试test_verify_dims_in_fitting_leaf_winsAn opt-out on any leaf disables the check for the whole compound model验证的行为。拟合器中的调用点解析结果通过verify_dims_verify_dims_in_fitting(model_copy)传入_convert_input覆盖线性拟合与非线性拟合两条路径线性最小二乘类拟合器LinearLSQFitter等在 fitting.py 调用非线性拟合器TRFLSQFitter、LMLSQFitter、LevMarLSQFitter等在 fitting.py 以及 L1837、L1928 等多处调用。无论哪种拟合器只要模型设置了verify_dims_in_fitting False_convert_input就会跳过形状断言直接把坐标与数据送入后续参数求解流程。实战示例拟合重采样到粗网格的模型官方用户指南 docs/modeling/fitting.rst 的 Fitting models that change the shape of the data 一节给出了完整的可运行示例。核心是自定义一个可拟合的 1D 模型它在evaluate内部把结果重采样为 7 个点import numpy as np from astropy.modeling import Fittable1DModel, Parameter class RebinnedLinear1D(Fittable1DModel): verify_dims_in_fitting False intercept Parameter(default0.) slope Parameter(default1.) staticmethod def evaluate(x, intercept, slope): y intercept slope * x return np.interp(np.linspace(x.min(), x.max(), 7), x, y)生成数据在 200 点网格上求值模型把结果重采样到 7 个点坐标与数据形状显式不同x np.linspace(0., 10., 200) y RebinnedLinear1D(intercept3., slope2.)(x) x.shape, y.shape # ((200,), (7,))如果RebinnedLinear1D没有设置verify_dims_in_fitting False这组(200,)坐标与(7,)数据会让拟合直接抛出ValueError设置之后拟合可以正常完成并恢复出原始参数from astropy.modeling.fitting import TRFLSQFitter fit TRFLSQFitter() m fit(RebinnedLinear1D(), x, y) m.parameters # array([3., 2.])注该示例使用TRFLSQFitter需要环境已安装 SciPy。测试代码中同类模型还通过参数化覆盖了non_linear_fitters fitters的全部拟合器见下文测试章节。复合模型在组件上设置对复合模型如|串联、*并联的组合不要试图在复合模型对象上设置该属性而应直接设置到其中一个叶子组件上from astropy.modeling import models class OptOutGaussian1D(models.Gaussian1D): verify_dims_in_fitting False # 复合模型中任一组件退出即可关闭整体校验 compound OptOutGaussian1D(1.0, 5.0, 0.1) * models.Const1D(1.0)在 astropy/modeling/tests/test_fitters.py 的test_verify_dims_in_fitting_leaf_wins中对此有直接验证models.Linear1D() | models.Shift()无退出标志返回校验开启而RebinnedLinear1D() | models.Shift()含退出组件返回校验关闭。带单位数据的场景astropy 模型还支持带单位的输入。拟合时CompoundModel.without_units_for_data会重建复合模型节点退出标志之所以仍然生效正是因为解析逻辑是从叶子节点上读取的。测试 astropy/modeling/tests/test_quantities_fitting.pytest_verify_dims_in_fitting_survives_unit_stripping专门验证了这一点OptOutGaussian1D(1 * u.Jy, 1 * u.um, 0.1 * u.um) * models.Const1D(1.0)在构造复合模型及执行without_units_for_data剥离单位后_verify_dims_in_fitting均返回False退出标志不会丢失。测试验证默认行为与退出行为的双向保障围绕该特性astropy/modeling/tests/test_fitters.py 提供了成体系的测试可作为理解行为的权威参考测试用例验证内容test_verify_dims_in_fitting_defaultL1608-L1610_verify_dims_in_fitting(None)与普通Linear1D默认均返回True即默认执行校验test_verify_dims_in_fitting_leaf_winsL1613-L1616复合模型中任意叶子退出则整体关闭校验test_convert_input_verify_dims_1dL1619-L16271D 场景形状不一致默认抛x and y should have the same shapeverify_dimsFalse时正常返回test_convert_input_verify_dims_2dL1630-L16392D 场景形状不一致默认抛x, y and z should have the same shapeverify_dimsFalse时正常返回test_fitting_verifies_dims_by_defaultL1642-L1652各拟合器对形状不匹配的数据默认抛ValueErrortest_fitting_with_mismatched_dimsL1655-L1668各拟合器对RebinnedLinear1D退出校验可正常拟合且能恢复intercept3.0、slope2.0的真实参数其中测试用的RebinnedLinear1D定义于 test_fitters.py其evaluate通过np.interp(np.linspace(x.min(), x.max(), 7), x, y)将任意长度的输入重采样为 7 个点正是返回不同形状数组模型的真实写照。适用场景与注意事项适用场景仪器响应卷积后降采样、直方图/分箱类模型输出落在更粗的网格、任何输入输出形状合法不同的可拟合模型风险提示关闭形状校验意味着拟合器不再能保证坐标与数据一一对应优化器与协方差估计的语义由模型自身负责务必在evaluate中正确处理输入输出映射关系设置位置普通模型直接在类体中写verify_dims_in_fitting False复合模型必须在组件叶子上设置且任一组件退出即整体退出默认安全属性默认True未显式设置时所有现有拟合行为保持不变存量代码无需任何改动。参考文件索引变更记录docs/changes/modeling/20369.api.rst、docs/changes/modeling/20369.feature.rst用户指南docs/modeling/fitting.rstFitting models that change the shape of the data 章节属性定义astropy/modeling/core.py解析与校验实现astropy/modeling/fitting.py测试用例astropy/modeling/tests/test_fitters.py、astropy/modeling/tests/test_quantities_fitting.py赞分享科学计算科研数据分析【免费下载链接】astropyAstronomy and astrophysics core library项目地址https://gitcode.com/gh_mirrors/as/astropy点击查看免费下载相关推荐Astropy建模与拟合从简单函数到复杂天体模型的终极指南Astropy建模与拟合从简单函数到复杂天体模型的终极指南 Astropy作为天文学和天体物理学领域的核心库其强大的建模与拟合功能让数据分析变得简单高效。无科学计算科研数据分析Jesse MCP 蒙特卡洛模拟工具指南用重采样 K 线验证策略是否过拟合Jesse MCP 蒙特卡洛模拟工具指南用重采样 K 线验证策略是否过拟合 导读 Jesse 是一个用 Python 编写的开源加密货币交易机器人其 MCP金融科技数据分析后端AI 应用理解模型选择、欠拟合与过拟合D2L项目中的关键概念解析理解模型选择、欠拟合与过拟合D2L项目中的关键概念解析 引言机器学习中的根本挑战 在机器学习实践中我们经常面临一个核心困境如何在训练数据上表现优异的同时人工智能深度学习机器学习教程上一篇Podman 启动健康检查间隔--health-startup-interval完全指南参数语义、底层实现与 Quadlet 配置下一篇使用 Sublime Merge 完成首次开源贡献first-contributions 实战指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考