The numpy.polyfit() method fits a polynomial of a specified degree to a set of data using the least squares method. This method is used to model relationships between two sets of data, typically x and y.
Syntax
numpy.polyfit (x, y, deg, rcond=None, full=False, w=None, cov=False)
Parameters
Name | Description |
x | Array or sequence of x coordinates of the data points. |
y | Array or sequence of y coordinates of the data points. |
deg | Degree of the fitting polynomial. |
rcond (optional) | Relative condition number of the fit. |
full (optional) | If True, extra information is returned. |
w (optional) | Weights applied to the y-coordinates. |
cov (optional) | If True, the covariance matrix of the polynomial coefficient estimates is returned. |
Return Value
This method returns a vector of polynomial coefficients that minimizes the squared error.
- If full is True, additional diagnostic information is also returned.
- If cov is True, the covariance matrix is returned.
Important points
- This method assumes that the data points are equally spaced along the x-axis.
- If the data points are not equally spaced, this method with the w (weights) argument, where the weights are inversely proportional to the distances between the x-coordinates, is recommended.
- The degree of the fitted polynomial should be chosen carefully. Higher-degree polynomials can lead to overfitting.
- Data should be pre-processed (e.g., normalized) for better results, especially for high degrees.
- The rcond parameter can be adjusted to improve the numerical stability of the fit if the data points are nearly collinear or if the polynomial degree is high.
- If the cov=True option is used, the covariance matrix provides information about the uncertainties and correlations between the estimated polynomial coefficients.
Example 1: Fitting a Linear Polynomial
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
coeffs = np.polyfit(x, y, 1)
poly = np.poly1d(coeffs)
# Generate x values for plotting
x_plot = np.linspace(0, 10, 100)
# Plot the data and the fitted line
plt.scatter(x, y, label='Data')
plt.plot(x_plot, poly(x_plot), 'r-', label='Fitted Line')
plt.legend()
plt.show()
Output
This example fits a linear polynomial (degree 1) to some sample data. The result is plotted with the original data points and the fitted line.
Example 2: Fitting a Quadratic Polynomial
import numpy as np
import matplotlib.pyplot as plt
# Generate some sample data
x = np.linspace(-5, 5, 20)
y = x**2 + np.random.normal(0, 3, 20) # Add some noise
# Fitting a quadratic polynomial (degree 2)
coeffs = np.polyfit(x, y, 2)
poly = np.poly1d(coeffs)
# Generate x values for plotting
x_plot = np.linspace(-5, 5, 100)
# Plot the data and the fitted curve
plt.scatter(x, y, label='Data')
plt.plot(x_plot, poly(x_plot), 'r-', label='Fitted Curve')
plt.legend()
plt.show()
Output
It fits a second-degree polynomial, suitable for parabolic data.
Example 3: Fitting a Higher-Degree Polynomial
import numpy as np
import matplotlib.pyplot as plt
# Generate some sample data
x = np.linspace(0, 10, 25)
y = np.sin(x) + np.random.normal(0, 0.2, 25) # Add some noise
# Fit a 5th-degree polynomial
coefficients = np.polyfit(x, y, 5)
poly = np.poly1d(coefficients)
# Generate x values for plotting
x_plot = np.linspace(0, 10, 100)
# Plot the data and the fitted curve
plt.scatter(x, y, label='Data')
plt.plot(x_plot, poly(x_plot), 'r-', label='Fitted Curve')
plt.legend()
plt.show()
Output
In this example, we fit a 5th-degree polynomial to some sample data with added noise. The result is plotted with the original data points and the fitted curve.
Example 4: Polynomial Fit with Weights
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
# Fitting a polynomial with weights
weights = np.array([1, 2, 1, 1])
coeffs = np.polyfit(x, y, 1, w=weights)
print(coeffs)
Output
[ 0.97631579 -0.85526316]
Weights allow for emphasis on certain data points.
Example 5: Returning full diagnostic information
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
coeffs, residuals, rank, singular_values, rcond = np.polyfit(x, y, 1, full=True)
print(coeffs)
print(residuals)
print(rank)
print(singular_values)
print(rcond)
Output
[ 1. -0.95]
[0.05]
2
[1.34230538 0.44521486]
8.881784197001252e-16
We got comprehensive information about the polynomial fit.
Example 6: Covariance Matrix output
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
# Fitting a polynomial and obtaining the covariance matrix
coeffs, cov = np.polyfit(x, y, 1, cov=True)
print(cov)
Output
[[ 0.005 -0.0075]
[-0.0075 0.0175]]
That’s all!
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.