# xarray.Dataset.polyfit

#### Dataset.polyfit(dim, deg, skipna=None, rcond=None, w=None, full=False, cov=False)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L9120-L9190)

Least squares polynomial fit.

This replicates the behaviour of numpy.polyfit but differs by skipping
invalid values when skipna = True.

* **Parameters:**
  * **dim** ([hashable](../user-guide/terminology.html.md#term-name)) – Coordinate along which to fit the polynomials.
  * **deg** ([`int`](https://docs.python.org/3/builtins/functions.html#int)) – Degree of the fitting polynomial.
  * **skipna** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – If True, removes all invalid values before fitting each 1D slices of the array.
    Default is True if data is stored in a dask.array or if there is any
    invalid values, False otherwise.
  * **rcond** ([`float`](https://docs.python.org/3/builtins/functions.html#float) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Relative condition number to the fit.
  * **w** ([hashable](../user-guide/terminology.html.md#term-name) or `Any`, *optional*) – Weights to apply to the y-coordinate of the sample points.
    Can be an array-like object or the name of a coordinate in the dataset.
  * **full** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – Whether to return the residuals, matrix rank and singular values in addition
    to the coefficients.
  * **cov** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or `"unscaled"`, *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – Whether to return to the covariance matrix in addition to the coefficients.
    The matrix is not scaled if cov=’unscaled’.
* **Returns:**
  **polyfit_results** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – A single dataset which contains (for each “var” in the input dataset):

  [var]_polyfit_coefficients
  : The coefficients of the best fit for each variable in this dataset.

  [var]_polyfit_residuals
  : The residuals of the least-square computation for each variable (only included if full=True)
    When the matrix rank is deficient, np.nan is returned.

  [dim]_matrix_rank
  : The effective rank of the scaled Vandermonde coefficient matrix (only included if full=True)
    The rank is computed ignoring the NaN values that might be skipped.

  [dim]_singular_values
  : The singular values of the scaled Vandermonde coefficient matrix (only included if full=True)

  [var]_polyfit_covariance
  : The covariance matrix of the polynomial coefficient estimates (only included if full=False and cov=True)
* **Warns:**
  **RankWarning** – The rank of the coefficient matrix in the least-squares fit is deficient.
  The warning is not raised with in-memory (not dask) data and full=True.

#### SEE ALSO
[`numpy.polyfit`](https://numpy.org/doc/stable/reference/generated/numpy.polyfit.html#numpy.polyfit), [`numpy.polyval`](https://numpy.org/doc/stable/reference/generated/numpy.polyval.html#numpy.polyval), [`xarray.polyval`](xarray.polyval.html.md#xarray.polyval)
