已知
x
1
,
x
2
,
.
.
.
,
x
n
∈
R
N
,
y
1
,
y
2
,
.
.
.
,
y
N
∈
R
1
x_1, x_2, ..., x_n \in R^N,y_1, y_2,...,y_N\in R^1
x1,x2,...,xn∈RN,y1,y2,...,yN∈R1
{
y
1
=
x
11
a
1
+
x
12
a
2
+
.
.
.
+
x
1
n
a
n
y
2
=
x
21
a
1
+
x
22
a
2
+
.
.
.
+
x
2
n
a
n
y
N
=
x
n
1
a
1
+
x
n
2
a
2
+
.
.
.
+
x
n
n
a
n
\begin{cases}y^1=x_{11}a_1 + x_{12}a_2 + ... + x_{1n}a_n\\y^2=x_{21}a_1 + x_{22}a_2 + ... + x_{2n}a_n\\y^N=x_{n1}a_1 + x_{n2}a_2 + ... + x_{nn}a_n \end{cases}
⎩⎪⎨⎪⎧y1=x11a1+x12a2+...+x1nany2=x21a1+x22a2+...+x2nanyN=xn1a1+xn2a2+...+xnnan
则有:
X
N
×
n
a
n
×
1
=
Y
N
×
1
X_{N\times n}a_{n\times 1}=Y_{N\times 1}
XN×nan×1=YN×1
目标求
a
n
×
1
a_{n\times 1}
an×1
当 N = n N=n N=n,且 X N × n 可 逆 时 X_{N\times n}可逆时 XN×n可逆时
a = X − 1 Y a=X^{-1}Y a=X−1Y
当 N ≠ n N\neq n N=n$
求最小二乘解,
m
i
n
J
=
∣
∣
X
a
−
Y
∣
∣
2
minJ=||Xa-Y||^2
minJ=∣∣Xa−Y∣∣2
J
=
(
X
a
−
Y
)
T
(
X
a
−
Y
)
J=(Xa-Y)^T(Xa-Y)
J=(Xa−Y)T(Xa−Y)
∂ J ∂ a = X T ( X a − Y ) = 0 \frac{\partial J}{\partial a}=X^T(Xa-Y)=0 ∂a∂J=XT(Xa−Y)=0
X T X a = X T Y X^TXa = X^TY XTXa=XTY
当 X T X X^TX XTX可逆时,此时一般 N > > n N>>n N>>n(样本数远大于特征数)
a = ( X T X ) − 1 X T Y a = (X^TX)^{-1}X^TY a=(XTX)−1XTY(最小二乘解)
当 X T X X^TX XTX不可逆时,此时一般 N < n N<n N<n(样本数远小于特征数)
加入正则项,解决过拟合
J = ∣ ∣ X a − Y ∣ ∣ 2 + λ ∣ ∣ a ∣ ∣ 2 J=||Xa-Y||^2+\lambda ||a||^2 J=∣∣Xa−Y∣∣2+λ∣∣a∣∣2
∂ J ∂ a = X T ( X a − Y ) + λ a = 0 \frac{\partial J}{\partial a}=X^T(Xa-Y)+\lambda a=0 ∂a∂J=XT(Xa−Y)+λa=0
( X T X + λ I ) a = X T Y (X^TX+\lambda I)a=X^TY (XTX+λI)a=XTY
因为 X T X + λ I X^TX+\lambda I XTX+λI的特征值必然全大于0,则 X T X + λ I X^TX+\lambda I XTX+λI必然可逆
a = ( X T X + λ I ) − 1 X T Y a=(X^TX+\lambda I)^{-1}X^TY a=(XTX+λI)−1XTY(岭回归)