In [7]:
import numpy as np
%matplotlib inline

In [ ]:
x = np.array([0, 1, 2, 3])

In [2]:
y = np.array([-1, 0.2, 0.9, 2.1])

In [12]:
A = np.vstack([x, np.ones(len(x)), np.ones(len(x))]).T

In [13]:
A.shape


Out[13]:
(4, 3)

In [14]:
A


Out[14]:
array([[ 0.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 2.,  1.,  1.],
       [ 3.,  1.,  1.]])

In [16]:
mcc = np.linalg.lstsq(A, y)[0]

In [17]:
mcc


Out[17]:
array([ 1.   , -0.475, -0.475])

In [8]:
import matplotlib.pyplot as plt

In [9]:
plt.plot(x, y, 'o', label='Original data', markersize=10)
plt.plot(x, m*x + c, 'r', label='Fitted line')
plt.legend()
plt.show()



In [ ]: