Regression
Regression is similar to interpolation. In this case, we assume that the data is imprecise, and we require an object of predetermined structure to fit the data as closely as possible. The most basic example is univariate polynomial regression to a sequence of points. We obtain that with the polyfit
command, which we discussed briefly in the Univariate polynomials section of this chapter. For instance, if we want to compute the regression line in the least-squares sense for a sequence of 10 uniformly spaced points in the interval (0, π/2) and their values under the sin
function, we will issue the following commands:
>>> import numpy >>> import scipy >>> import matplotlib.pyplot as plt >>> x=numpy.linspace(0,1,10) >>> y=numpy.sin(x*numpy.pi/2) >>> line=numpy.polyfit(x,y,deg=1) >>> plt.plot(x,y,'.',x,numpy.polyval(line,x),'r') >>> plt.show()
This gives the following plot that shows linear regression with polyfit...