for i, n in enumerate(p):
    poly += n * x ** i
return poly
x = numpy.linspace(0.0, numpy.pi)
y = numpy.cos(x) + 0.05 * numpy.random.normal(size=len(x))
p0 = numpy.ones(6)
coeff, var_matrix = curve_fit(_polynomial, x, y, p0=p0)
yfit = [_polynomial(xx, *tuple(coeff)) for xx in x]
plt.plot(x, y, label='Test data')
plt.plot(x, yfit, label='fitted data')
plt.show()
6th degree curve fitting with numpy/scipy