实现 Simple Linear Regression
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1, 6, dtype=float)
y = np.array([1., 3., 2., 3., 5.])
plt.scatter(x, y)
plt.axis([0, 6, 0, 6])
x_mean = np.mean(x)
y_mean = np.mean(y)
num = 0.0
d = 0.0
for x_i, y_i in zip(x, y):
num += (x_i - x_mean) * (y_i - y_mean)
d += (x_i - x_mean)**2
a = num / d
b = y_mean -a * x_mean
求 a b 的向量化方式
# 求出数据集和标签的均值
x_mean = np.mean(x)
y_mean = np.mean(y)
# 向量化求解a b
num = (x_train - x_mean).dot(y_train - y_mean)
d = (x_train - x_mean).dot(x_train - x_mean)
a = num / d
b = y_mean -a * x_mean
y_hat = a * x + b
y_hat
array([1.2, 2. , 2.8, 3.6, 4.4])
plt.scatter(x, y)
plt.plot(x, y_hat, color="r")
plt.axis([0, 6, 0, 6])
x_predict = 6
y_predict = a * x_predict + b
y_predict
5.2