In [13]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Create some data

In [14]:
x = [1.0, 1.5, 3.0, 4.0, 4.5, 5.0]
y = [1.0, 2.0, 1.5, 1.5, 3.0, 3.2]

x = np.array(x)
y = np.array(y)

Plot some data

In [15]:
plt.plot(x, y, 'o', ms=10)
plt.axis([0,6,0,4])
plt.xlabel('x')
plt.ylabel('y')
Out[15]:
<matplotlib.text.Text at 0x105af5f60>

Fit a Linear Line

In [42]:
xx = np.linspace(0,6,100)

A = np.vstack((x**0, x**1)).T
c = np.linalg.lstsq(A, y)[0]
yy = c[0]*xx**0 + c[1]*xx**1

plt.plot(x, y, 'o', ms=10)
plt.plot(xx, yy)
Out[42]:
[<matplotlib.lines.Line2D at 0x105867748>]

Fit with $p=1$, $p=2$, $p=3$, $p=4$, $p=5$

In [64]:
plt.figure(figsize=(20,20))
xx = np.linspace(0,6,100)

for k in range(1,6):
    A = np.vstack((x**i for i in range(k+1))).T

    c = np.linalg.lstsq(A, y)[0]
    
    yy = 0.0 * xx
    for i in range(0, k+1):
        yy += c[i] * xx**i

    plt.plot(xx, yy, label='p=%d' % k, lw=3)
    plt.plot(x, y, 'o', ms=20)
    plt.legend(frameon=False)

    plt.axis([0,6,0,4])
In [ ]: