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