单位和数量支持¶

将参数设置为数量¶

```>>> from astropy import units as u
>>> from astropy.modeling.models import Gaussian1D
>>> g1 = Gaussian1D(mean=3 * u.m, stddev=2 * u.cm, amplitude=3 * u.Jy)
```

```>>> g1.mean
Parameter('mean', value=3.0, unit=m)
```

```>>> g1.mean.name
'mean'
>>> g1.mean.value
3.0
>>> g1.mean.unit
Unit("m")
```

```>>> g1.mean = 3 * u.s
>>> g1
<Gaussian1D(amplitude=3. Jy, mean=3. s, stddev=2. cm)>
```

```>>> g1.mean.value = 2
>>> g1
<Gaussian1D(amplitude=3. Jy, mean=2. s, stddev=2. cm)>
```

```>>> g1.mean = 2
Traceback (most recent call last):
...
UnitsError : The 'mean' parameter should be given as a Quantity because it
was originally initialized as a Quantity
```

```>>> g2 = Gaussian1D(mean=3)
>>> g2.mean = 3 * u.m
```

用数量评估模型¶

```>>> g3 = Gaussian1D(mean=3 * u.m, stddev=5 * u.cm)
>>> g3(2.9 * u.m)
<Quantity 0.1353352832366122>
>>> g3(2.9 * u.s)
Traceback (most recent call last):
...
UnitsError : Units of input 'x', s (time), could not be converted to
required input units of m (length)
```

```>>> g3(3)
Traceback (most recent call last):
...
UnitsError : Units of input 'x', (dimensionless), could not be converted to
required input units of m (length)
```

等价物¶

```>>> g4 = Gaussian1D(mean=3 * u.micron, stddev=1 * u.micron, amplitude=3 * u.Jy)
```

```>>> g4(1e2 * u.THz)
Traceback (most recent call last):
...
UnitsError : Units of input 'x', THz (frequency), could not be converted to
required input units of micron (length)
```

```>>> g4(110 * u.THz, equivalencies={'x': u.spectral()})
<Quantity 2.888986819525229 Jy>
```

```>>> g4.inputs
('x',)
```

```>>> g4.input_units_equivalencies = {'x': u.spectral()}
>>> g4(110 * u.THz)
<Quantity 2.888986819525229 Jy>
```

用单位拟合模型到数据¶

```import numpy as np
from astropy import units as u
import matplotlib.pyplot as plt

x = np.linspace(1, 5, 30) * u.micron
y = np.exp(-0.5 * (x - 2.5 * u.micron)**2 / (200 * u.nm)**2) * u.mJy
plt.plot(x, y, 'ko')
plt.xlabel('Wavelength (microns)')
plt.ylabel('Flux density (mJy)')
```

(png _, svgpdf

```from astropy.modeling import models, fitting

g5 = models.Gaussian1D(mean=3 * u.micron, stddev=1 * u.micron, amplitude=1 * u.Jy)

fitter = fitting.LevMarLSQFitter()

g5_fit = fitter(g5, x, y)

plt.plot(x, y, 'ko')
plt.plot(x, g5_fit(x), 'r-')
plt.xlabel('Wavelength (microns)')
plt.ylabel('Flux density (mJy)')
```

(png _, svgpdf

具有等效性¶

```g6 = models.Gaussian1D(mean=110 * u.THz, stddev=10 * u.THz, amplitude=1 * u.Jy)

g6_fit = fitter(g6, x, y, equivalencies={'x': u.spectral()})

plt.plot(x, g6_fit(x, equivalencies={'x': u.spectral()}), 'b-')
plt.xlabel('Wavelength (microns)')
plt.ylabel('Flux density (mJy)')
```

(png _, svgpdf

```g6.input_units_equivalencies = {'x': u.spectral()}
g6_fit = fitter(g6, x, y)
```

支持其他无单位模型中的单元¶

```>>> from astropy.modeling import models
>>> from astropy import units as u
>>> model = models.Polynomial1D(1, c0=1, c1=2)
>>> new_model = model.coerce_units(input_units={'x': u.Hz}, return_units={'y': u.s},
... input_units_equivalencies={'x':u.spectral()})
>>> new_model(10 * u.Hz)
<Quantity 21. s>
```