I use Scipy below, but the same functions (polyval and polyfit) are also in NumPy; NumPy is a Matplotlib dependency, so you can import these two functions from there if you don't have SciPy.
import numpy as NP
from scipy import polyval, polyfit
from matplotlib import pyplot as PLT
n=10
x = NP.linspace(0, 1, n)
y = 7*x**2 - 5*x + 3
noise = NP.random.normal(.5, .3, 10)
y += noise
a, b, c = polyfit(x, y, 2)
y_pred = polyval([a, b, c], x)
MSE = NP.sqrt( NP.sum((y_pred-y)**2)/10 )
x_out = NP.linspace(0, 2, 20)
y_pred = polyval([a, b, c], x_out)
fig = PLT.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y, 'g.', x_out, y_pred, 'b-' )
PLT.show()

source
share