定义新模型类¶

基本定制型号¶

```import numpy as np
import matplotlib.pyplot as plt
from astropy.modeling.models import custom_model
from astropy.modeling.fitting import LevMarLSQFitter

# Define model
@custom_model
def sum_of_gaussians(x, amplitude1=1., mean1=-1., sigma1=1.,
amplitude2=1., mean2=1., sigma2=1.):
return (amplitude1 * np.exp(-0.5 * ((x - mean1) / sigma1)**2) +
amplitude2 * np.exp(-0.5 * ((x - mean2) / sigma2)**2))

# Generate fake data
np.random.seed(0)
x = np.linspace(-5., 5., 200)
m_ref = sum_of_gaussians(amplitude1=2., mean1=-0.5, sigma1=0.4,
amplitude2=0.5, mean2=2., sigma2=1.0)
y = m_ref(x) + np.random.normal(0., 0.1, x.shape)

# Fit model to data
m_init = sum_of_gaussians()
fit = LevMarLSQFitter()
m = fit(m_init, x, y)

# Plot the data and the best fit
plt.plot(x, y, 'o', color='k')
plt.plot(x, m(x))
```

(png _, svgpdf

一维高斯模型的逐步定义¶

```from astropy.modeling import Fittable1DModel, Parameter

class Gaussian1D(Fittable1DModel):
n_inputs = 1
n_outputs = 1

amplitude = Parameter()
mean = Parameter()
stddev = Parameter()
```

```@staticmethod
def evaluate(x, amplitude, mean, stddev):
return amplitude * np.exp((-(1 / (2. * stddev**2)) * (x - mean)**2))
```

```@staticmethod
def fit_deriv(x, amplitude, mean, stddev):
d_amplitude = np.exp(- 0.5 / stddev**2 * (x - mean)**2)
d_mean = (amplitude *
np.exp(- 0.5 / stddev**2 * (x - mean)**2) *
(x - mean) / stddev**2)
d_stddev = (2 * amplitude *
np.exp(- 0.5 / stddev**2 * (x - mean)**2) *
(x - mean)**2 / stddev**3)
return [d_amplitude, d_mean, d_stddev]
```

```def __init__(self, amplitude, x_mean, y_mean, x_stddev=None,
y_stddev=None, theta=None, cov_matrix=None, **kwargs):
# The **kwargs here should be understood as other keyword arguments
# accepted by the basic Model.__init__ (such as constraints)
if cov_matrix is not None:
# Set x/y_stddev and theta from the covariance matrix
x_stddev = ...
y_stddev = ...
theta = ...

# Don't pass on cov_matrix since it doesn't mean anything to the base
# class
super().__init__(amplitude, x_mean, y_mean, x_stddev, y_stddev, theta,
**kwargs)
```

完整例子¶

```import numpy as np
from astropy.modeling import Fittable1DModel, Parameter

class Gaussian1D(Fittable1DModel):
amplitude = Parameter()
mean = Parameter()
stddev = Parameter()

@staticmethod
def evaluate(x, amplitude, mean, stddev):
return amplitude * np.exp((-(1 / (2. * stddev**2)) * (x - mean)**2))

@staticmethod
def fit_deriv(x, amplitude, mean, stddev):
d_amplitude = np.exp((-(1 / (stddev**2)) * (x - mean)**2))
d_mean = (2 * amplitude *
np.exp((-(1 / (stddev**2)) * (x - mean)**2)) *
(x - mean) / (stddev**2))
d_stddev = (2 * amplitude *
np.exp((-(1 / (stddev**2)) * (x - mean)**2)) *
((x - mean)**2) / (stddev**3))
return [d_amplitude, d_mean, d_stddev]
```

线型模型的完整示例¶

```import numpy as np
from astropy.modeling import Fittable1DModel, Parameter

class LineModel(Fittable1DModel):
slope = Parameter()
intercept = Parameter()
linear = True

@staticmethod
def evaluate(x, slope, intercept):
return slope * x + intercept

@staticmethod
def fit_deriv(x, slope, intercept):
d_slope = x
d_intercept = np.ones_like(x)
return [d_slope, d_intercept]

@property
def inverse(self):
new_slope = self.slope ** -1
new_intercept = -self.intercept / self.slope
return LineModel(slope=new_slope, intercept=new_intercept)
```