amep.functions.Fit#
- class amep.functions.Fit(g, **kwargs)#
Bases:
BaseFunction
Fitting a 1d function.
- __init__(g, **kwargs)#
General fit class that allows to fit a 1d user-defined function to data.
- Parameters:
g (function) – The function that will be fitted to the data
**kwargs – Other keyword arguments are forwarded to the function g. These parameters will be kept constant in the fitting process.
- Return type:
None.
Examples
>>> import amep >>> import numpy as np >>> x = np.linspace(-np.pi, np.pi, 100) >>> y = (1+np.random.random((100,)))*np.sin(2*x) >>> def f(x, amp=1, omega=1, phi=0): ... return amp*np.sin(omega*x+phi) >>> fit = amep.functions.Fit(f) >>> fit.fit(x, y, p0=[1,2,3]) >>> print(fit.results) {'amp': (-1.6707345639977673, 0.03611365715752075), 'omega': (1.980453842406803, 0.018014280164672856), 'phi': (3.0950628128156383, 0.03345686981008855)}
>>> fit = amep.functions.Fit(f, omega=2) >>> fit.fit(x, y, p0=[1,2]) >>> print(fit.results) {'amp': (1.6786964141227352, 0.03291999452138418), 'phi': (0.07093556110237159, 0.03115340006227788)}
>>> fig, axs = amep.plot.new() >>> axs.plot(x, y, label="data", ls="") >>> axs.plot(x, fit.generate(x), marker="", label="fit") >>> axs.legend() >>> axs.set_xlabel(r"$x$") >>> axs.set_ylabel(r"$\sin(2x)$") >>> fig.savefig("./figures/functions/functions-Fit.png")
Methods
__init__
(g, **kwargs)General fit class that allows to fit a 1d user-defined function to data.
f
(p, x)fit
(xdata, ydata[, p0, sigma, maxit, verbose])Fits the function self.f to the given data by using ODR (orthogonal distance regression).
generate
(x[, p])Returns the y values for given x values.
Attributes
Returns the fit errors for each parameter as an array.
keys
name
nparams
output
Returns an array of the optimal fit parameters.
Returns the dictionary of fit results including parameter names, parameter values, and fit errors.
- property errors: ndarray#
Returns the fit errors for each parameter as an array.
- Returns:
Fit errors.
- Return type:
np.ndarray
- fit(xdata: ndarray, ydata: ndarray, p0: list | None = None, sigma: ndarray | None = None, maxit: int | None = None, verbose: bool = False) None #
Fits the function self.f to the given data by using ODR (orthogonal distance regression).
- Parameters:
xdata (np.ndarray) – x values.
ydata (np.ndarray) – y values.
p0 (list or None, optional) – List of initial values. The default is None.
sigma (np.ndarray or None, optional) – Absolute error for each data point. The default is None.
maxit (int, optional) – Maximum number of iterations. The default is 200.
verbose (bool, optional) – If True, the main results are printed. The default is False.
- Return type:
None.
- generate(x: ndarray, p: list | None = None) ndarray #
Returns the y values for given x values.
- Parameters:
x (np.ndarray) – x values.
p (list, optional) – List of parameters. The default is None.
- Returns:
y – f(x)
- Return type:
np.ndarray
- property params: ndarray#
Returns an array of the optimal fit parameters.
- Returns:
Fit parameter values.
- Return type:
np.ndarray
- property results: dict#
Returns the dictionary of fit results including parameter names, parameter values, and fit errors.
- Returns:
Fit results.
- Return type:
dict