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: boolwhether 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: boolpassed into polyfit; see below.cov: boolpassed 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: boolwhether 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———-dim : HashableCoordinate along which to fit the polynomials.deg : intDegree of the fitting polynomial.skipna : bool or None, optionalIf 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.rcond : float or None, optionalRelative condition number to the fit.w : Hashable, array-like or None, optionalWeights 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, default: FalseWhether to return the residuals, matrix rank and singular values in additionto the coefficients.cov : bool or “unscaled”, default: FalseWhether to return to the covariance matrix in addition to the coefficients.The matrix is not scaled ifcov='unscaled'.Returns——-polyfit_results : DatasetA single dataset which contains:polyfit_coefficientsThe coefficients of the best fit.polyfit_residualsThe residuals of the least-square computation (only included iffull=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