MCPcopy Create free account
hub / github.com/numpy/numpy / polyfit

Function polyfit

numpy/lib/polynomial.py:454–698  ·  view source on GitHub ↗

Least squares polynomial fit. .. note:: This forms part of the old polynomial API. Since version 1.4, the new polynomial API defined in `numpy.polynomial` is preferred. A summary of the differences can be found in the :doc:`transition guide </reference/routines.

(x, y, deg, rcond=None, full=False, w=None, cov=False)

Source from the content-addressed store, hash-verified

452
453@array_function_dispatch(_polyfit_dispatcher)
454def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):
455 """
456 Least squares polynomial fit.
457
458 .. note::
459 This forms part of the old polynomial API. Since version 1.4, the
460 new polynomial API defined in `numpy.polynomial` is preferred.
461 A summary of the differences can be found in the
462 :doc:`transition guide </reference/routines.polynomials>`.
463
464 Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
465 to points `(x, y)`. Returns a vector of coefficients `p` that minimises
466 the squared error in the order `deg`, `deg-1`, ... `0`.
467
468 The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class
469 method is recommended for new code as it is more stable numerically. See
470 the documentation of the method for more information.
471
472 Parameters
473 ----------
474 x : array_like, shape (M,)
475 x-coordinates of the M sample points ``(x[i], y[i])``.
476 y : array_like, shape (M,) or (M, K)
477 y-coordinates of the sample points. Several data sets of sample
478 points sharing the same x-coordinates can be fitted at once by
479 passing in a 2D-array that contains one dataset per column.
480 deg : int
481 Degree of the fitting polynomial
482 rcond : float, optional
483 Relative condition number of the fit. Singular values smaller than
484 this relative to the largest singular value will be ignored. The
485 default value is len(x)*eps, where eps is the relative precision of
486 the float type, about 2e-16 in most cases.
487 full : bool, optional
488 Switch determining nature of return value. When it is False (the
489 default) just the coefficients are returned, when True diagnostic
490 information from the singular value decomposition is also returned.
491 w : array_like, shape (M,), optional
492 Weights. If not None, the weight ``w[i]`` applies to the unsquared
493 residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
494 chosen so that the errors of the products ``w[i]*y[i]`` all have the
495 same variance. When using inverse-variance weighting, use
496 ``w[i] = 1/sigma(y[i])``. The default value is None.
497 cov : bool or str, optional
498 If given and not `False`, return not just the estimate but also its
499 covariance matrix. By default, the covariance are scaled by
500 chi2/dof, where dof = M - (deg + 1), i.e., the weights are presumed
501 to be unreliable except in a relative sense and everything is scaled
502 such that the reduced chi2 is unity. This scaling is omitted if
503 ``cov='unscaled'``, as is relevant for the case that the weights are
504 w = 1/sigma, with sigma known to be a reliable estimate of the
505 uncertainty.
506
507 Returns
508 -------
509 p : ndarray, shape (deg + 1,) or (deg + 1, K)
510 Polynomial coefficients, highest power first. If `y` was 2-D, the
511 coefficients for `k`-th data set are in ``p[:,k]``.

Callers

nothing calls this directly

Calls 8

finfoClass · 0.90
vanderFunction · 0.90
lstsqFunction · 0.90
invFunction · 0.90
dotFunction · 0.90
warnMethod · 0.80
sumMethod · 0.45
outerMethod · 0.45

Tested by

no test coverage detected