AppDividend
Latest Code Tutorials

np.polyfit: How to Use Numpy polyfit() Method in Python

The Polynomial.fit class method is recommended for new code as it is numerically stable. It is a fit polynomial p(x) = p[0] * x**deg + … + p[deg] of degree deg to points (x, y). Let’s see how to use Python’s numpy polyfit() method.

Numpy polyfit

The Numpy polyfit() method fits our data inside a polynomial function. The np.polyfit() method takes a few parameters and returns a vector of coefficients p that minimizes the squared error in the order deg, deg-1, … 0.

It least squares the polynomial fit. It fits a polynomial p(X) of degree deg to points (X, Y).

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 polynomial should be made fit.

rcond: float. It is an optional parameter.

It describes the relative condition number of the fit. Those singular values that are smaller than this relative to the largest singular value will be ignored.

full: It’s an optional parameter of Boolean type

It acts as a switch and helps determine the nature of 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 Kth dataset are in p [:, K].

Programming Example:

Program to show the working of 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)

Output

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]

Explanation

In the above program, we have created a dataset (x,y) using the np.arange() method, and the various points in the dataset are displayed.

If we want to fit these data points into a polynomial of various degrees like linear (having degree 1), which is y=mx+c, we need to have two constant-coefficient values for m c, which is calculated using the numpy.polyfit() function.

Similarly, in the case of a quadratic equation (having degree 2), which is of the form y=ax**2+bx+c, we need to have three constant-coefficient values for a, b, and c is calculated using the numpy.polyfit() function.

The same is the case with a cubic polynomial of the form y=ax**3+bx**2+cx+d; we need to have four constant-coefficient values for a, b, c, and d is calculated using the numpy.polyfit() function.

Using np.polyfit() method to implement linear regression

See the following code.

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)

Output

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

Numpy polyfit() Method

Explanation

In the program polyfit2.py, we have created a dataset (x,y) method, and the various points in the dataset are displayed and are plotted onto a graph.

Now, we want to fit this dataset into a polynomial of degree 2, a quadratic polynomial of the form y=ax**2+bx+c, so we need to calculate three constant-coefficient values for a, b, and c, which is calculated using the numpy.polyfit() function.

These coefficient values signify the best fit that our polynomial function can have concerning the data points. We can predict our y values based on some given x_test values, which are also shown.

Finally, that is it for this tutorial.

See also

Numpy linalg det()

Numpy savetxt

Numpy ndarray flat()

Numpy floor()

Numpy ceil()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.