curvefit

UnitsAwareDataArray.curvefit(coords: Union[str, xarray.core.dataarray.DataArray, Iterable[Union[str, xarray.core.dataarray.DataArray]]], func: Callable[[...], Any], reduce_dims: Optional[Union[Hashable, Iterable[Hashable]]] = None, skipna: bool = True, p0: Optional[Dict[str, Any]] = None, bounds: Optional[Dict[str, Any]] = None, param_names: Optional[Sequence[str]] = None, kwargs: Optional[Dict[str, Any]] = None)

Curve fitting optimization for arbitrary functions.

Wraps scipy.optimize.curve_fit with apply_ufunc.

Parameters
  • coords (hashable, DataArray, or sequence of DataArray or hashable) – 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 (hashable or sequence of hashable) – 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 (dict-like, 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 (dict-like, 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 (sequence of hashable, 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 (optional) – Additional keyword arguments to passed to scipy curve_fit.

Returns

curvefit_results – A single dataset which contains:

[var]_curvefit_coefficients

The coefficients of the best fit.

[var]_curvefit_covariance

The covariance matrix of the coefficient estimates.

Return type

Dataset

See also

DataArray.polyfit, scipy.optimize.curve_fit