In [1]:
import numpy as np

In [2]:
x = np.arange(0.0, 21.0, 1.0)
x

Out[2]:
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20.])
In [3]:
m, c = 5.0, 10.0

In [27]:
y = (m * x + c) + np.random.normal(0.0, 2.0, len(x))
y

Out[27]:
array([  7.27789599,  13.4346095 ,  17.01236296,  22.5994958 ,
31.12553181,  33.92652203,  43.92327199,  42.43949886,
50.9527582 ,  54.08260115,  60.43629577,  64.7320538 ,
73.43535143,  77.38816257,  78.3791766 ,  82.03490764,
88.19020573,  93.89723844, 101.47512203, 107.96066546,
107.18274193])
In [34]:
((y - m*x - c)**2).sum()

Out[34]:
103.22132467692614
In [29]:
import matplotlib.pyplot as plt

In [30]:
plt.plot(x, y, 'k.')

Out[30]:
[<matplotlib.lines.Line2D at 0x23bad19b588>]
In [3]:
# Import packages.
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.0, 10.1, 0.5)
y = 5.0 * x + 2.0

# Import linear_model from sklearn.
import sklearn.linear_model as lm

# Create a linear regression model instance.
m = lm.LinearRegression()

# Ask our model to fit the data.
m.fit(x.reshape(-1, 1), y)

# See how good our fit is.
m.score(x.reshape(-1, 1), y)

Out[3]:
1.0
In [ ]: