Models and Fitting (astropy.modeling)


astropy.modeling provides a framework for representing models and performing model evaluation and fitting. A number of predefined 1-D and 2-D models are provided and the capability for custom, user defined models is supported. Different fitting algorithms can be used with any model. For those fitters with the capabilities fitting can be done using uncertainties, parameters with bounds, and priors.

Getting started

Using a model is a matter of defining the model (instantiating) and then calling it with the input values for calculation. This is illustrated with the code and plot below for a 1-dimensional Gaussian.

import numpy as np
import matplotlib.pyplot as plt
from astropy.modeling import models

# define and evaluate the model
g = models.Gaussian1D(amplitude=1.2, mean=0.9, stddev=0.5)
x = np.linspace(-5., 5.0, 200)
y = g(x)

# plot the model
plt.plot(x, y, 'ko')




A number of significant changes have been made to the internals that have been documented in more detail in Changes to Modeling in v4.0. This summarizes the two biggest changes:

  • Expressions of model classes no longer is supported. (Expressions of model instances are still very much supported!)
  • Parameter values now are contained in the parameter instance. Previously they were contained in the model referencing the parameter. The previous behavior resulted in compound models parameters not sharing the same value as the constituent models, if one of them changed, the other didn’t. Now they see exactly the same value.