# xarray.Dataset.curvefit

#### Dataset.curvefit(coords, func, reduce_dims=None, skipna=True, p0=None, bounds=None, param_names=None, errors='raise', kwargs=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L10030-L10122)

Curve fitting optimization for arbitrary functions.

Wraps [`scipy.optimize.curve_fit()`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy.optimize.curve_fit) with [`apply_ufunc()`](xarray.apply_ufunc.html.md#xarray.apply_ufunc).

* **Parameters:**
  * **coords** ([hashable](../user-guide/terminology.html.md#term-name), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), or [sequence](https://docs.python.org/3/glossary.html#term-sequence) of [hashable](../user-guide/terminology.html.md#term-name) or [`DataArray`](xarray.DataArray.html.md#xarray.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()`](https://docs.python.org/3/builtins/functions.html#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`](https://docs.python.org/3/builtins/stdtypes.html#str), `Iterable` of `Hashable` or [`None`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – Whether to skip missing values when fitting. Default is True.
  * **p0** ([dict-like](https://docs.python.org/3/glossary.html#term-mapping), *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](https://docs.python.org/3/glossary.html#term-mapping), *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](https://docs.python.org/3/glossary.html#term-sequence) of [hashable](../user-guide/terminology.html.md#term-name), *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** (`dict[str`, `Any]`, *optional*) – Additional keyword arguments to passed to scipy curve_fit.
* **Returns:**
  **curvefit_results** ([`Dataset`](xarray.Dataset.html.md#xarray.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.

#### SEE ALSO
[`Dataset.polyfit`](xarray.Dataset.polyfit.html.md#xarray.Dataset.polyfit), [`scipy.optimize.curve_fit`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy.optimize.curve_fit)

[`xarray.Dataset.xlm.modelfit`](https://xarray-lmfit.readthedocs.io/stable/accessors/xarray.Dataset.xlm.modelfit.html#xarray.Dataset.xlm.modelfit)
: External method from [xarray-lmfit](https://xarray-lmfit.readthedocs.io/) with more curve fitting functionality.
