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.
Note
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. The main change is that
combining model classes no longer is supported. (Combining model
instances is still very much supported!)
This simple example illustrates defining a model,
calculating values based on input x values, and using fitting data with a model.
importnumpyasnpimportmatplotlib.pyplotaspltfromastropy.modelingimportmodels,fitting# define a model for a lineline_orig=models.Linear1D(slope=1.0,intercept=0.5)# generate x, y data non-uniformly spaced in x# add noise to y measurementsnpts=30np.random.seed(10)x=np.random.uniform(0.0,10.0,npts)y=line_orig(x)y+=np.random.normal(0.0,1.5,npts)# initialize a linear fitterfit=fitting.LinearLSQFitter()# initialize a linear modelline_init=models.Linear1D()# fit the data with the fitterfitted_line=fit(line_init,x,y)# plot the modelplt.figure()plt.plot(x,y,'ko',label='Data')plt.plot(x,fitted_line(x),'k-',label='Fitted Model')plt.xlabel('x')plt.ylabel('y')plt.legend()