预测函数
\[
f_\theta(x) = \theta_0 + \theta_1x
\]
目标函数
\[
E(\theta) = \frac 12\sum_{i=1}^n(y^{(i)} - f_\theta(x^{(i)}))^2
\]
标准化
\(\mu\) 是训练数据的平均值,\(\sigma\) 是标准差。
\[
z^{(i)} = \frac{x^{(i)} - \mu}{\sigma}
\]
更新表达式
\[
\theta_0 := \theta_0 - \eta\sum_{i=1}^n(f_\theta(x^{(i)}) - y^{(i)})
\]
\[
\theta_1 := \theta_1 - \eta\sum_{i=1}^n(f_\theta(x^{(i)}) -
y^{(i)})x^{(i)}
\]
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import numpy as np import matplotlib.pyplot as plt
train = np.loadtxt('click.csv', delimiter=',', dtype='int', skiprows=1) train_x = train[:, 0] train_y = train[:, 1]
theta0 = np.random.rand() theta1 = np.random.rand()
def f(x): return theta0 + theta1 * x
def E(x, y): return 0.5 * np.sum((y - f(x)) ** 2)
mu = train_x.mean() sigma = train_x.std() def standardize(x): return (x - mu) / sigma train_z = standardize(train_x)
ETA = 1e-3
diff = 1
count = 0
error = E(train_z, train_y) while diff > 1e-2: tmp_theta0 = theta0 - ETA * np.sum((f(train_z) - train_y)) tmp_theta1 = theta1 - ETA * np.sum((f(train_z) - train_y) * train_z) theta0 = tmp_theta0 theta1 = tmp_theta1 current_error = E(train_z, train_y) diff = error - current_error error = current_error count += 1 log = '第 {} 次 : theta0 = {:.3f}, theta1 = {:.3f}, 差值 = {:.4f}' print(log.format(count, theta0, theta1, diff))
x = np.linspace(-3, 3, 100) plt.plot(train_z, train_y, 'o') plt.plot(x, f(x)) plt.show()
|