使用矩阵来简化算法
使用矩阵乘法来减少代码
训练集
size( f e e t 2 feet^2 feet2) | Number of badrooms | Number of floors | Age of home(years) | Price($1000) |
---|---|---|---|---|
2014 | 5 | 1 | 45 | 460 |
1416 | 3 | 2 | 40 | 232 |
1534 | 3 | 2 | 30 | 315 |
852 | 2 | 1 | 36 | 178 |
… | … | … | … | … |
预测函数
h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 + θ 3 x 3 + θ 4 x 4 h\theta(x)=\theta_{0}+\theta_{1}x_{1}+\theta_{2}x_{2}+\theta_{3}x_{3}+\theta_{4}x_{4} hθ(x)=θ0+θ1x1+θ2x2+θ3x3+θ4x4
使用矩阵
当特征数量为n时
h
θ
(
x
)
=
θ
0
+
θ
1
x
1
+
θ
2
x
2
+
θ
3
x
3
+
θ
4
x
4
+
.
.
.
.
.
.
+
θ
n
x
n
h\theta(x)=\theta_{0}+\theta_{1}x_{1}+\theta_{2}x_{2}+\theta_{3}x_{3}+\theta_{4}x_{4}+......+\theta_{n}x_{n}
hθ(x)=θ0+θ1x1+θ2x2+θ3x3+θ4x4+......+θnxn
假设一个 x 0 = 1 x_{0}=1 x0=1$
x = [ x 0 x 1 x 2 x 3 x 4 . . . x n ] x=\begin{gathered}\begin{bmatrix}x_{0}\\x_{1}\\x_{2}\\x_{3}\\x_{4}\\...\\x_{n}\end{bmatrix}\quad\end{gathered} x=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡x0x1x2x3x4...xn⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤ θ = [ θ 0 θ 1 θ 2 θ 3 θ 4 . . . θ n ] \theta=\begin{gathered}\begin{bmatrix}\theta_{0}\\\theta_{1}\\\theta_{2}\\\theta_{3}\\\theta_{4}\\...\\\theta_{n}\end{bmatrix}\quad\end{gathered} θ=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡θ0θ1θ2θ3θ4...θn⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤
预测函数为
h
θ
(
x
)
=
θ
⊤
x
h\theta(x)=\theta^\top x
hθ(x)=θ⊤x
各主流语言都有相关的库来优化矩阵计算
梯度下降算法为
θ
j
=
θ
j
−
a
1
m
∑
i
=
1
m
(
h
θ
(
x
(
i
)
)
−
y
(
i
)
)
x
j
(
i
)
\theta_{j}=\theta_{j}-a\frac{1}{m}\sum_{i=1}^{m}(h\theta(x^{(i)})-y^{(i)})x_{j}^{(i)}
θj=θj−am1i=1∑m(hθ(x(i))−y(i))xj(i)
其中 x j ( i ) x_{j}^{(i)} xj(i)表示第 i i i 组特征的第 j j j 个特征
特征缩放
特征缩放的目的是使特征保持在相似的取值范围,这样梯度下降算法能更快的收敛
x
1
=
s
i
z
e
(
0
−
2000
f
e
e
t
2
)
x_{1}=size(0-2000\space feet^2)
x1=size(0−2000 feet2)
x
2
=
n
u
m
b
e
r
o
f
b
a
d
r
o
o
m
s
(
1
−
5
)
x_{2}=number\space of\space badrooms(1-5)
x2=number of badrooms(1−5)
特征范围差距特别大时,轮廓图将呈现椭圆形
均值归一化
x
j
=
x
j
−
标
准
差
m
a
x
x_{j}=\frac{x_{j}-标准差}{max}
xj=maxxj−标准差
列如
x
1
=
s
i
z
e
−
1000
2000
x_{1}=\frac{size-1000}{2000}
x1=2000size−1000,
x
2
=
b
a
d
r
o
o
m
s
−
2
5
x_{2}=\frac{badrooms-2}{5}
x2=5badrooms−2
−
0.5
≤
x
1
≤
0.5
-0.5\le x_{1}\le 0.5
−0.5≤x1≤0.5,
−
0.5
≤
x
2
≤
0.5
-0.5\le x_{2}\le 0.5
−0.5≤x2≤0.5