监督学习
回归问题(预测连续型数据)
分类问题(预测离散型数据)
线性回归
预测函数:h(x) =
θ
0
+
x
θ
1
\theta_0+x\theta_1
θ0+xθ1
减小误差即使误差函数
J
(
θ
)
=
1
2
m
∑
i
=
0
n
(
h
(
θ
)
i
+
y
i
)
2
J(\theta)= \frac{1}{2m}\sum\limits_{i=0}^{n}{(h(\theta)^i+y^i)}^2
J(θ)=2m1i=0∑n(h(θ)i+yi)2 最小。
m代表训练集数据地数量
举例说明:
梯度下降法
找出现在位置下降最快地方向
θ
j
:
=
θ
j
−
α
∂
J
(
θ
0
,
θ
1
)
∂
θ
j
\theta_j:=\theta_j-\alpha \frac{\partial J(\theta_0,\theta_1)}{\partial \theta_j }
θj:=θj−α∂θj∂J(θ0,θ1)
α
\alpha
α是学习率,即下降的步长
:=代表的是赋值地意思
θ 0 : = θ 0 − α ∂ J ( θ 0 , θ 1 ) ∂ θ 0 \theta_0:=\theta_0-\alpha \frac{\partial J(\theta_0,\theta_1)}{\partial \theta_0} θ0:=θ0−α∂θ0∂J(θ0,θ1)
这个方法的缺点是只能获取局部最优点。
但是线性回归的梯度下降法只有全局最优解。
矩阵向量乘法
h
θ
(
x
)
=
−
40
+
0.25
x
h_\theta(x)=-40+0.25x
hθ(x)=−40+0.25x
[
2104
1416
1534
852
]
×
[
−
40
0.25
]
\begin{gathered} \begin{bmatrix} 2104 \\ 1 416\\1534\\852\end{bmatrix} \quad \times \begin{bmatrix} -40 \\ 0.25 \end{bmatrix} \quad \end{gathered}
⎣⎢⎢⎡210414161534852⎦⎥⎥⎤×[−400.25]
多功能(多变量)
h
θ
(
x
)
=
θ
0
+
θ
1
x
1
+
θ
2
x
2
+
…
…
h_\theta(x)=\theta_0+\theta_1x_1+\theta_2x_2+……
hθ(x)=θ0+θ1x1+θ2x2+……
x
=
[
x
0
x
1
x
2
…
…
x
n
]
θ
=
[
θ
0
θ
1
θ
2
…
…
θ
n
]
\begin{gathered} x= \begin{bmatrix} x_0 \\ x_1\\x_2\\……\\x_n\end{bmatrix} \quad \theta= \begin{bmatrix} \theta_0 \\ \theta_1 \\ \theta_2\\……\\ \theta_n\end{bmatrix} \quad \end{gathered}
x=⎣⎢⎢⎢⎢⎡x0x1x2……xn⎦⎥⎥⎥⎥⎤θ=⎣⎢⎢⎢⎢⎡θ0θ1θ2……θn⎦⎥⎥⎥⎥⎤
h
0
(
x
)
=
θ
T
x
h_0(x)=\theta^Tx
h0(x)=θTx
梯度下降
θ
j
:
=
θ
j
−
α
1
m
∑
i
=
1
m
(
h
θ
(
x
)
i
−
y
i
)
x
j
i
\theta_j:=\theta_j-\alpha \frac{1}{m}\sum\limits_{i=1}^{m}{(h_\theta(x)^i-y^i)}x_j^i
θj:=θj−αm1i=1∑m(hθ(x)i−yi)xji
变量x需要归一化
x
=
x
−
E
(
x
)
c
o
v
(
x
)
x=\frac{x-E(x)}{cov(x)}
x=cov(x)x−E(x)
这样可以使
−
1
≤
x
i
≤
1
-1\leq x^i\leq1
−1≤xi≤1
特别注意不要使步长
α
\alpha
α太大,不然损失函数无法收敛
α
\alpha
α最好取值:0.001,0.003,0.01,0.03,0.1,0.3,1……