import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 30, 20)
y = x + 3*np.random.randn(20)
plt.figure(figsize=(10, 8))
plt.scatter(x, y)
<matplotlib.collections.PathCollection at 0x1115cc208>
from sklearn.linear_model import LinearRegression
model = LinearRegression()
X = x.reshape(-1, 1)
Y = y.reshape(-1, 1)
model.fit(X, Y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
model.predict([[40]])
array([[38.18890435]])
plt.figure(figsize=(12, 8))
plt.scatter(X, Y)
plt.plot(X, model.predict(X))
[<matplotlib.lines.Line2D at 0x1a1c284e48>]
Y_PRE = model.predict(X)
np.sum(np.square(Y_PRE, Y))
5760.457790652543
model.coef_, model.intercept_
(array([[0.94170908]]), array([0.52054117]))