Загрузка данных


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

f = lambda x: (x - 3) ** 2
df = lambda x: 2 * (x - 3)

x, lr, path = 10.0, 0.1, []
for i in range(30):
    path.append(x)
    x = x - lr * df(x)

print(f"x = {x:.4f}")

xs = np.linspace(-2, 12, 100)
plt.figure(figsize=(6, 4))
plt.plot(xs, f(xs))
plt.scatter(path, [f(p) for p in path], c='r', zorder=5)
plt.show()

plt.figure(figsize=(15, 4))
lrs = [0.01, 0.1, 1.1]
for i, lr_test in enumerate(lrs):
    x_test, path_test = 10.0, []
    for _ in range(30):
        path_test.append(x_test)
        x_test = x_test - lr_test * df(x_test)
    
    plt.subplot(1, 3, i+1)
    plt.plot(xs, f(xs))
    plt.scatter(path_test, [f(p) for p in path_test], c='r', zorder=5)
    plt.title(f'lr = {lr_test}')
    plt.ylim(0, 50)
plt.tight_layout()
plt.show()

rng = np.random.default_rng(0)
X = rng.uniform(0, 5, 50)
y = 2 * X + 1 + rng.normal(0, 0.5, 50)

def loss(w, b): 
    return np.mean((w * X + b - y) ** 2)

def grads(w, b):
    p = w * X + b
    return np.mean(2 * (p - y) * X), np.mean(2 * (p - y))

w, b, lr_gd, hist = 0.0, 0.0, 0.05, []
for i in range(200):
    dw, db = grads(w, b)
    w -= lr_gd * dw
    b -= lr_gd * db
    hist.append(loss(w, b))

print(f"w = {w:.4f}, b = {b:.4f}")

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(hist)
plt.subplot(1, 2, 2)
plt.scatter(X, y)
plt.plot(X, w * X + b, 'r')
plt.show()

model = LinearRegression()
model.fit(X.reshape(-1, 1), y)
print(f"Sklearn: w = {model.coef_[0]:.4f}, b = {model.intercept_:.4f}")