0%

白话机器学习的数学——回归——多项式回归的实现

预测函数

\[ f_\theta(x) = \theta_0 + \theta_1x + \theta_2x^2 \]

\[ X\cdot\theta = \begin{bmatrix} 1&x^{(1)}&x^{(1)^2}\\ 1&x^{(2)}&x^{(2)^2}\\ 1&x^{(3)}&x^{(3)^2}\\ \vdots&\vdots&\vdots\\ 1&x^{(n)}&x^{(n)^2} \end{bmatrix} \cdot \begin{bmatrix} \theta_0\\ \theta_1\\ \theta_2 \end{bmatrix} = \begin{bmatrix} \theta_0+\theta_1x^{(1)}+\theta_2x^{(1)^2}\\ \theta_0+\theta_1x^{(2)}+\theta_2x^{(2)^2}\\ \vdots\\ \theta_0+\theta_1x^{(n)}+\theta_2x^{(n)^2} \end{bmatrix} \]

更新表达式

\[ \theta_j := \theta_j - \eta\sum_{i=1}^n(f_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} \]

\[ f = \begin{bmatrix} f_\theta(x^{(1)}) - y^{(1)}\\ f_\theta(x^{(2)}) - y^{(2)}\\ \vdots\\ f_\theta(x^{(n)}) - y^{(n)} \end{bmatrix} \]

\[ X = \begin{bmatrix}x0&x1&x2\end{bmatrix} = \begin{bmatrix} 1&x^{(1)}&x^{(1)^2}\\ 1&x^{(2)}&x^{(2)^2}\\ 1&x^{(3)}&x^{(3)^2}\\ \vdots&\vdots&\vdots\\ 1&x^{(n)}&x^{(n)^2} \end{bmatrix} \]

\[ \sum_{i=1}^n(f_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} = f^TX \]

代码示例

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]

# 标准化
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):
return (x - mu) / sigma
train_z = standardize(train_x)

# 创建训练数据的矩阵
def to_matrix(x):
return np.vstack([np.ones(x.size), x, x ** 2]).T
X = to_matrix(train_z)

# 参数初始化
theta = np.random.rand(3)

# 预测函数
def f(x):
return np.dot(x, theta)

# 目标函数
def E(x, y):
return 0.5 * np.sum((y - f(x)) ** 2)

# 学习率
ETA = 1e-3
# 误差的差值
diff = 1
# 更新次数
count = 0
# 直到误差的差值小于 0.01 为止,重复参数更新
error = E(X, train_y)
while diff > 1e-2:
# 更新参数
theta = theta - ETA * np.dot(f(X) - train_y, X)
# 计算与上一次误差的差值
current_error = E(X, train_y)
diff = error - current_error
error = current_error
# 输出日志
count += 1
log = '第 {} 次 : theta = {}, 差值 = {:.4f}'
print(log.format(count, theta, diff))

# 绘图确认
x = np.linspace(-3, 3, 100)
plt.plot(train_z, train_y, 'o')
plt.plot(x, f(to_matrix(x)))
plt.show()