🍾 Xarray is now 10 years old! 🎉

xarray.Dataset.curvefit

xarray.Dataset.curvefit#

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

Curve fitting optimization for arbitrary functions.

Wraps scipy.optimize.curve_fit with apply_ufunc.

Parameters:
  • coords (hashable, DataArray, or sequence of hashable or DataArray) – 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, Iterable of Hashable or None, optional) – 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, default: True) – 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 the values are DataArrays, they will be appropriately broadcast to the coordinates of the array. 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 tuples of bounding values passed to the curve_fit bounds arg. If any of the bounds are DataArrays, they will be appropriately broadcast to the coordinates of the array. 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.

  • errors ({"raise", "ignore"}, default: "raise") – If ‘raise’, any errors from the scipy.optimize_curve_fit optimization will raise an exception. If ‘ignore’, the coefficients and covariances for the coordinates where the fitting failed will be NaN.

  • **kwargs (optional) – 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.