import numpy as np
x = np.arange(0.0, 21.0, 1.0)
x
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.])
m, c = 5.0, 10.0
y = (m * x + c) + np.random.normal(0.0, 2.0, len(x))
y
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])
((y - m*x - c)**2).sum()
103.22132467692614
import matplotlib.pyplot as plt
plt.plot(x, y, 'k.')
[<matplotlib.lines.Line2D at 0x23bad19b588>]
# 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)
1.0