xarray.Dataset.curvefit

Dataset.curvefit(coords, func, reduce_dims=None, skipna=True, p0=None, bounds=None, param_names=None, kwargs=None)[source]

Curve fitting optimization for arbitrary functions.

Wraps scipy.optimize.curve_fit with apply_ufunc.

Parameters
  • coords (DataArray, str or sequence of DataArray, str) – Independent coordinate(s) over which to perform the curve fitting. Must share at least one dimension with the calling object. When fitting multi-dimensional functions, supply coords as a sequence in the same order as arguments in func. To fit along existing dimensions of the calling object, coords can also be specified as a str or sequence of strs.

  • func (callable()) – User specified function in the form f(x, *params) which returns a numpy array of length len(x). params are the fittable parameters which are optimized by scipy curve_fit. x can also be specified as a sequence containing multiple coordinates, e.g. f((x0, x1), *params).

  • reduce_dims (str or sequence of str) – Additional dimension(s) over which to aggregate while fitting. For example, calling ds.curvefit(coords=’time’, reduce_dims=[‘lat’, ‘lon’], …) will aggregate all lat and lon points and fit the specified function along the time dimension.

  • skipna (bool, optional) – Whether to skip missing values when fitting. Default is True.

  • p0 (dictionary, optional) – Optional dictionary of parameter names to initial guesses passed to the curve_fit p0 arg. If none or only some parameters are passed, the rest will be assigned initial values following the default scipy behavior.

  • bounds (dictionary, optional) – Optional dictionary of parameter names to bounding values passed to the curve_fit bounds arg. If none or only some parameters are passed, the rest will be unbounded following the default scipy behavior.

  • param_names (seq, optional) – Sequence of names for the fittable parameters of func. If not supplied, this will be automatically determined by arguments of func. param_names should be manually supplied when fitting a function that takes a variable number of parameters.

  • kwargs (dictionary) – Additional keyword arguments to passed to scipy curve_fit.

Returns

curvefit_results (Dataset) – A single dataset which contains:

[var]_curvefit_coefficients

The coefficients of the best fit.

[var]_curvefit_covariance

The covariance matrix of the coefficient estimates.