text_plain_output_1.png image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import numpy as np

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)
model = LinearRegression()
model.fit(x, y)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(123)
x = np.linspace(0, 10, 20)
y = 3 * x + 2 + np.random.normal(scale=2, size=len(x))
model = LinearRegression()
model.fit(x.reshape(-1, 1), y)
y_pred = model.predict(x.reshape(-1, 1))
residuals = y - y_pred
plt.scatter(x, y, s=10)
plt.plot(x, y_pred, color='red')
plt.legend(['Regression line', 'Data'])
plt.xlabel('X')
plt.ylabel('Y')
print('Residuals:')
for i in range(10):
    print('Data point {}: {:.2f}'.format(i + 1, residuals[i]))