PlasmaCalcs.tools.xarray_tools.xarray_sci.xarray_polyfit
- PlasmaCalcs.tools.xarray_tools.xarray_sci.xarray_polyfit(array, coord, degree, *, stddev=False, full=False, cov=False, eval=False, **kw_polyfit)
- returns array.polyfit(coord, degree, **kw_polyfit), after swapping coord to be a dimension, if needed.E.g. for an array with dimension ‘snap’ and associated non-dimension coordinate ‘t’,xarray_polyfit(array, ‘t’, 1) is equivalent to array.swap_dims(dict(snap=’t’)).polyfit(‘t’, 1).
- stddev: bool
- whether to also return the standard deviations of each coefficient in the fit.if True, assign the variable ‘polyfit_stddev’ = diagonal(polyfit_covariance)**0.5,mapping the diagonal (across ‘cov_i’, ‘cov_j’) to the dimension ‘degree’.if cov False when stddev True, do not keep_cov in the result.Not compatible with full=True.
- full: bool
- passed into polyfit; see below.
- cov: bool
- passed into polyfit; see below.Note: if stddev=True when cov=False, still use cov=True during array.polyfit,however then remove polyfit_covariance & polyfit_residuals from result.
- eval: bool
- whether to also return the fit, evaluated at array.coords[coord]if True, assign the variable ‘polyfit_eval’ = sum(coeff * coord**degree, ‘degree’).if stddev=True too, assign ‘polyfit_eval-stddev’ and ‘polyfit_eval+stddev’ too,using similar formula but using coeffs +/- stddev.
Docs for xr.DataArray.polyfit copied below:——————————————-Least squares polynomial fit.This replicates the behaviour ofnumpy.polyfitbut differs by skippinginvalid values whenskipna = True.Parameters———-- dimHashable
- Coordinate along which to fit the polynomials.
- degint
- Degree of the fitting polynomial.
- skipnabool or 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 anyinvalid values, False otherwise.
- rcondfloat or None, optional
- Relative condition number to the fit.
- wHashable, array-like or None, 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.
- fullbool, default: False
- Whether to return the residuals, matrix rank and singular values in additionto the coefficients.
- covbool or “unscaled”, default: False
- Whether to return to the covariance matrix in addition to the coefficients.The matrix is not scaled if
cov='unscaled'.
Returns——-- polyfit_resultsDataset
- A single dataset which contains:polyfit_coefficientsThe coefficients of the best fit.polyfit_residualsThe residuals of the least-square computation (only included if
full=True).When the matrix rank is deficient, np.nan is returned.[dim]_matrix_rankThe effective rank of the scaled Vandermonde coefficient matrix (only included iffull=True)[dim]_singular_valueThe singular values of the scaled Vandermonde coefficient matrix (only included iffull=True)polyfit_covarianceThe covariance matrix of the polynomial coefficient estimates (only included iffull=Falseandcov=True)
See Also——–numpy.polyfitnumpy.polyvalxarray.polyvalDataArray.curvefit