VOOZH about

URL: https://www.geeksforgeeks.org/data-analysis/numpys-polyfit-function-a-comprehensive-guide/

⇱ NumPy's polyfit Function : A Comprehensive Guide - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

NumPy's polyfit Function : A Comprehensive Guide

Last Updated : 23 Jul, 2025

NumPy is a fundamental package for scientific computing in Python, providing support for arrays, mathematical functions, and more. One of its powerful features is the ability to perform polynomial fitting using the polyfit function. This article delves into the technical aspects of numpy.polyfit, explaining its usage, parameters, and practical applications.

What is Polynomial Fitting?

Polynomial fitting is a form of regression analysis where the relationship between the independent variable xand the dependent variable y is modeled as an n-degree polynomial. The goal is to find the polynomial coefficients that minimize the difference between the observed data points and the values predicted by the polynomial.

Syntax and Parameters:

The numpy.polyfit function fits a polynomial of a specified degree to a set of data using the least squares method. The basic syntax is:

numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
  • x: array_like, shape (M,): x-coordinates of the M sample points (x[i], y[i]).
  • y: array_like, shape (M,) or (M, K): y-coordinates of the sample points. Multiple datasets with the same x-coordinates can be fitted at once by passing a 2D array.
  • deg: int: Degree of the fitting polynomial.
  • rcond: float, optional: Relative condition number of the fit. Default is len(x) * eps, where eps is the machine precision.
  • full: bool, optional: If True, return additional diagnostic information.
  • w: array_like, shape (M,), optional: Weights to apply to the y-coordinates.
  • cov: bool or str, optional: If True, return the covariance matrix of the polynomial coefficient estimates.

NumPy's polyfit Function : Practical Examples

Let's explore how to use numpy.polyfit with a practical example.

1. Linear Fit

A linear fit is a first-degree polynomial fit. Consider the following data points:

Output:

Linear Fit Coefficients: [-0.30285714 0.75714286]
👁 linearfit
Linear Fit

2. Higher-Degree Polynomial Fit

For higher-degree polynomials, the process is similar. Here is an example with a cubic polynomial:

Output:

Cubic Fit Coefficients: [ 0.08703704 -0.81349206 1.69312169 -0.03968254]
👁 cubic
Higher-Degree Polynomial Fit

Handling Multiple Datasets

numpy.polyfit can handle multiple datasets with the same x-coordinates by passing a 2D array for y. This is useful for fitting multiple curves simultaneously.

Output:

Linear Fit Coefficients for Multiple Datasets: [array([-0.30285714, 0.75714286]), array([-0.28285714, 0.69047619])]
👁 polyfit
Handling Multiple Datasets

Advanced Options With NumPy's polyfit Function

1. Weights

Weights can be applied to the y-coordinates to give different importance to different data points.

Output:

Weighted Fit Coefficients: [-0.36783784 0.84378378]

2. Covariance Matrix

The covariance matrix of the polynomial coefficient estimates can be returned by setting cov=True.

Output:

Fit Coefficients: [-0.30285714 0.75714286]
Covariance Matrix: [[ 0.0213551 -0.05338776]
[-0.05338776 0.1957551 ]]

Conclusion

NumPy's polyfit function is a versatile tool for polynomial fitting, offering various options to customize the fitting process. Whether you are performing a simple linear fit or a complex multi-dataset fit, numpy.polyfit provides the functionality needed to accurately model your data. With the introduction of the new polynomial API, working with polynomials in NumPy has become even more efficient and user-friendly.

Comment