- What is numpy.polyfit() Method in Python
- Syntax
- Parameters
- X: array_like,
- Y: array_like,
- deg: int,
- rcond: float. It is an optional parameter.
- full: It’s an optional parameter of the Boolean type
- w: array_like, shape(M,), optional
- cov: bool or str, optional parameter
- Return Value
- Example 1: How to Use numpy.polyfit() Method
- Example 2: Using np.polyfit() method to implement linear regression
- Example 3: How to fit a polynomial function with numpy.polyfit()
- numpy.polyfit#
What is numpy.polyfit() Method in Python
The numpy.polyfit() is “used to fit the data within a polynomial function.” It takes a few parameters and returns a vector of coefficients p that minimizes the squared error in the order deg, deg-1, … 0.
Syntax
numpy.polyfit (X, Y, deg, rcond=None, full=False, w=None, cov=False)
Parameters
This function takes at most seven parameters:
X: array_like,
Suppose it has a shape of size (M). In a sample of M examples, it represents the x-coordinates (X[i],Y[i])
Y: array_like,
it has a shape of (M,) or (M, K). It represents the y-coordinates (X[i], Y[i]) of sample points.
deg: int,
It should be an integer value and specify the degree to which the polynomial should be made fit.
rcond: float. It is an optional parameter.
It describes the relative condition number of the fit. Those singular values smaller than this relative to the largest singular value will be ignored.
full: It’s an optional parameter of the Boolean type
It acts as a switch and helps determine the nature of the return value. For example, when the value is set to false (the default), only the coefficients are returned; when the value is set to true diagnostic info from the singular value decomposition is additionally returned.
w: array_like, shape(M,), optional
These are weights that are applied to the Y-coordinates of the sample points. For gaussian uncertainties, we should use 1/sigma (not 1/sigma**2).
cov: bool or str, optional parameter
If provided and is not set to False , it returns the estimate and its covariance matrix. The covariance is, by default, scaled by chi**2/sqrt(N-dof), i.e., the weights are presumed to be unreliable except in a relative sense, and everything is scaled such that the reduced chi2 is unity.
If cov=’ unscaled’, then the scaling is omitted as it is relevant because the weights are 1/sigma**2, with sigma being known to be a reliable estimate of the uncertainty.
Return Value
It returns a ndarray, shape (deg+1,) or (deg+1, K)
This method returns an n-dimensional array of shape (deg+1) when the Y array has the shape of (M,) or in case the Y array has the shape of (M, K), then an n-dimensional array of shape (deg+1, K) is returned.
If Y is 2-Dimensional, the coefficients for the K th dataset are in p [:, K].
Example 1: How to Use numpy.polyfit() Method
# importing the numpy module import numpy as np # Creating a dataset x = np.arange(-5, 5) print("X values in the dataset are:\n", x) y = np.arange(-30, 30, 6) print("Y values in the dataset are:\n", y) # calculating value of coefficients in case of linear polynomial z = np.polyfit(x, y, 1) print("\ncoefficient value in case of linear polynomial:\n", z) # calculating value of coefficient in case of quadratic polynomial z1 = np.polyfit(x, y, 2) print("\ncoefficient value in case of quadratic polynomial:\n", z1) # calculating value of coefficient in case of cubic polynomial z2 = np.polyfit(x, y, 3) print("\ncoefficient value in case of cubic polynomial:\n", z2)
X values in the dataset are: [-5 -4 -3 -2 -1 0 1 2 3 4] Y values in the dataset are: [-30 -24 -18 -12 -6 0 6 12 18 24] coefficient value in case of linear polynomial: [6.00000000e+00 4.62412528e-16] coefficient value in case of quadratic polynomial: [ 1.36058326e-16 6.00000000e+00 -2.24693342e-15] coefficient value in case of cubic polynomial: [ 1.10709805e-16 0.00000000e+00 6.00000000e+00 -8.42600032e-16]
Example 2: Using np.polyfit() method to implement linear regression
import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 30, 3) print("X points are: \n", x) y = x**2 print("Y points are: \n", y) plt.scatter(x, y) plt.xlabel("X-values") plt.ylabel("Y-values") plt.plot(x, y) p = np.polyfit(x, y, 2) # Last argument is degree of polynomial print("Coeeficient values:\n", p) predict = np.poly1d(p) x_test = 15 print("\nGiven x_test value is: ", x_test) y_pred = predict(x_test) print("\nPredicted value of y_pred for given x_test is: ", y_pred)
X points are: [ 0 3 6 9 12 15 18 21 24 27] Y points are: [ 0 9 36 81 144 225 324 441 576 729] Coeeficient values: [ 1.00000000e+00 -2.02043144e-14 2.31687975e-13] Given x_test value is: 15 Predicted value of y_pred for given x_test is: 225.00000000000009
Example 3: How to fit a polynomial function with numpy.polyfit()
import numpy as np import matplotlib.pyplot as plt # Define some data points x = np.array([0, 1, 2, 3, 4, 5]) y = np.array([0, 0.8, 0.9, 0.1, -0.8, -1]) # Fit a polynomial of degree 3 to the data coefficients = np.polyfit(x, y, 3) # Create a polynomial function that can be # used for plotting or further calculations p = np.poly1d(coefficients) # Generate x values x_fit = np.linspace(x.min(), x.max(), 500) # Use the polynomial function to calculate the y values y_fit = p(x_fit) # Plot the original data (in blue) and the fitted data (in red) plt.scatter(x, y, label='Original data', color='b') plt.plot(x_fit, y_fit, label='Fitted data', color='r') plt.legend() plt.show()
numpy.polyfit#
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 transition guide .
Fit a polynomial p(x) = p[0] * x**deg + . + p[deg] of degree deg to points (x, y). Returns a vector of coefficients p that minimises the squared error in the order deg, deg-1, … 0.
The Polynomial.fit class method is recommended for new code as it is more stable numerically. See the documentation of the method for more information.
Parameters : 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. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column.
Degree of the fitting polynomial
rcond float, optional
Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases.
full bool, optional
Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned.
w array_like, shape (M,), optional
Weights. If not None, the weight w[i] applies to the unsquared residual y[i] — y_hat[i] at x[i] . Ideally the weights are chosen so that the errors of the products w[i]*y[i] all have the same variance. When using inverse-variance weighting, use w[i] = 1/sigma(y[i]) . The default value is None.
cov bool or str, optional
If given and not False, return not just the estimate but also its covariance matrix. By default, the covariance are scaled by chi2/dof, where dof = M — (deg + 1), i.e., the weights are presumed to be unreliable except in a relative sense and everything is scaled such that the reduced chi2 is unity. This scaling is omitted if cov=’unscaled’ , as is relevant for the case that the weights are w = 1/sigma, with sigma known to be a reliable estimate of the uncertainty.
Returns : p ndarray, shape (deg + 1,) or (deg + 1, K)
Polynomial coefficients, highest power first. If y was 2-D, the coefficients for k-th data set are in p[:,k] .
residuals, rank, singular_values, rcond
These values are only returned if full == True
- residuals – sum of squared residuals of the least squares fit
- rank – the effective rank of the scaled Vandermonde coefficient matrix
- singular_values – singular values of the scaled Vandermonde coefficient matrix
- rcond – value of rcond.
V ndarray, shape (M,M) or (M,M,K)
Present only if full == False and cov == True . The covariance matrix of the polynomial coefficient estimates. The diagonal of this matrix are the variance estimates for each coefficient. If y is a 2-D array, then the covariance matrix for the k-th data set are in V[. k]
The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if full == False .
The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
Compute polynomial values.
Computes a least-squares fit.