参数¶
基础¶
这个包中的大多数模型都是“参数化”的,因为 Model
表示一个完整的模型族,每个模型的每个成员都由一组固定的参数来区分,这些参数使模型与某些因变量和自变量相适应(在整个软件包中也称为模型的输出和输入)。
在这个包中,参数在三种不同的上下文中使用:模型的基本评估、模型与数据的拟合以及向用户提供关于单个模型的信息(包括文档)。
的大多数子类 Model
--特别是那些实现特定物理或统计模型的人,有一组固定的参数,可以为该模型的实例指定这些参数。有几类模型(尤其是多项式),其中参数的数量取决于模型的某些其他属性(多项式情况下的阶数)。
模型维护一个参数名称列表, param_names
. 单个参数是 Parameter
它为实际参数值提供代理。可以用它们执行简单的数学运算,但它们也包含特定于模型参数的附加属性,例如对其值和文档的任何约束。
参数值可以是标量 or 数组值。某些参数因其本身的性质而被要求是数组(例如 AffineTransformation2D
). In most other cases, however, array-valued parameters have no meaning specific to the model, and are simply combined with input arrays during model evaluation according to the standard Numpy broadcasting rules .
参数约束¶
astropy.modeling
支持多种类型的参数约束。它们作为的属性实现 Parameter
或定义可在模型上设置的所有参数。
这个 astropy.modeling.Parameter.fixed
约束是布尔型的,指示参数在拟合期间是保持“固定”还是“冻结”。例如,修复 stddev
A的 Gaussian1D
模型意味着它将从拟合参数列表中排除:
>>> from astropy.modeling.models import Gaussian1D
>>> g = Gaussian1D(amplitude=10.2, mean=2.3, stddev=1.2)
>>> g.stddev.fixed
False
>>> g.stddev.fixed = True
>>> g.stddev.fixed
True
astropy.modeling.Parameter.bounds
是为参数设置最小值和最大值的一组数字。 (None, None)
指示参数值未绑定。 bounds
也可以使用 min
和 max
属性。分配 None
对相应属性移除参数上的绑定。例如,在 mean
A值 Gaussian1D
模型可以通过设置 min
和 max
::
>>> g.mean.bounds
(None, None)
>>> g.mean.min = 2.2
>>> g.mean.bounds
(2.2, None)
>>> g.mean.max = 2.4
>>> g.mean.bounds
(2.2, 2.4)
或使用 bounds
属性:
>>> g.mean.bounds = (2.2, 2.4)
astropy.modeling.Parameter.tied
是用户提供的可调用的,它获取模型实例并返回参数的值。它在设置化合物模型的约束时非常有用,例如两个参数之间的比率 (example )
也可以在初始化模型时设置约束。例如::
>>> g = Gaussian1D(amplitude=10.2, mean=2.3, stddev=1.2,
... fixed={'stddev': True},
... bounds={'mean': (2.2, 2.4)})
>>> g.stddev.fixed
True
>>> g.mean.bounds
(2.2, 2.4)
参数示例¶
模型类可以直接内省,以找出它们接受的参数:
>>> from astropy.modeling import models >>> models.Gaussian1D.param_names ('amplitude', 'mean', 'stddev')
中项目的顺序
param_names
列表是相关的——这与构造模型实例时传递这些参数值的顺序相同:>>> g = models.Gaussian1D(1.0, 0.0, 0.1) >>> g <Gaussian1D(amplitude=1.0, mean=0.0, stddev=0.1)>
但是,参数也可以作为关键字参数(以任何顺序)给定:
>>> g = models.Gaussian1D(mean=0.0, amplitude=2.0, stddev=0.2) >>> g <Gaussian1D(amplitude=2.0, mean=0.0, stddev=0.2)>
所以真正重要的是知道每个模型接受的参数的名称(和含义)。有关单个模型的更多信息也可以使用
help
内置:>>> help(models.Gaussian1D)
根据模型的其他属性,某些类型的模型可以具有不同数量的参数。具体而言,多项式模型的参数是其系数,其数量取决于多项式的次数:
>>> p1 = models.Polynomial1D(degree=3, c0=1.0, c1=0.0, c2=2.0, c3=3.0) >>> p1.param_names ('c0', 'c1', 'c2', 'c3') >>> p1 <Polynomial1D(3, c0=1., c1=0., c2=2., c3=3.)>
基本的
Polynomial1D
类参数命名c0
通过cN
在哪里?N
是多项式的次数。上面的例子表示多项式 \(3x^3 + 2x^2 + 1\) .有些模型还有一个或多个参数的默认值。例如,对于多项式模型,所有系数的默认值为零——这允许在不指定任何系数的情况下创建多项式实例:
>>> p2 = models.Polynomial1D(degree=4) >>> p2 <Polynomial1D(4, c0=0., c1=0., c2=0., c3=0., c4=0.)>
然后,可以通过访问模型上与参数同名的属性来设置/更新参数:
>>> p2.c4 = 1 >>> p2.c2 = 3.5 >>> p2.c0 = 2.0 >>> p2 <Polynomial1D(4, c0=2., c1=0., c2=3.5, c3=0., c4=1.)>
这个例子现在表示多项式 \(x^4 + 3.5x^2 + 2\) .
可以通过在字典中传递参数来设置多项式的系数,因为所有参数都可以作为关键字参数提供:
>>> ch2 = models.Chebyshev2D(x_degree=2, y_degree=3) >>> coeffs = dict((name, [idx, idx + 10]) ... for idx, name in enumerate(ch2.param_names)) >>> ch2 = models.Chebyshev2D(x_degree=2, y_degree=3, n_models=2, ... **coeffs) >>> ch2.param_sets array([[ 0., 10.], [ 1., 11.], [ 2., 12.], [ 3., 13.], [ 4., 14.], [ 5., 15.], [ 6., 16.], [ 7., 17.], [ 8., 18.], [ 9., 19.], [10., 20.], [11., 21.]])
或者直接使用关键字参数:
>>> ch2 = models.Chebyshev2D(x_degree=2, y_degree=3, ... c0_0=[0, 10], c0_1=[3, 13], ... c0_2=[6, 16], c0_3=[9, 19], ... c1_0=[1, 11], c1_1=[4, 14], ... c1_2=[7, 17], c1_3=[10, 20,], ... c2_0=[2, 12], c2_1=[5, 15], ... c2_2=[8, 18], c2_3=[11, 21])
单个参数值可以是不同大小和形状的数组:
>>> p3 = models.Polynomial1D(degree=2, c0=1.0, c1=[2.0, 3.0], ... c2=[[4.0, 5.0], [6.0, 7.0], [8.0, 9.0]]) >>> p3(2.0) array([[21., 27.], [29., 35.], [37., 43.]])
这相当于计算Numpy表达式:
>>> import numpy as np >>> c2 = np.array([[4.0, 5.0], ... [6.0, 7.0], ... [8.0, 9.0]]) >>> c1 = np.array([2.0, 3.0]) >>> c2 * 2.0**2 + c1 * 2.0 + 1.0 array([[21., 27.], [29., 35.], [37., 43.]])
注意,在大多数情况下,当使用数组值参数时,参数必须遵守Numpy数组之间的标准广播规则:
>>> models.Polynomial1D(degree=2, c0=1.0, c1=[2.0, 3.0], ... c2=[4.0, 5.0, 6.0]) Traceback (most recent call last): ... InputParameterError: Parameter u'c1' of shape (2,) cannot be broadcast with parameter u'c2' of shape (3,). All parameter arrays must have shapes that are mutually compatible according to the broadcasting rules.