子空间投影
从
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.style.use("seaborn-dark-palette")
fig = plt.figure()
plt.axis('equal')
plt.axis([-7, 7, -6, 6])
plt.arrow(-4, -1, 8, 2, head_width=0.3, head_length=0.5, color='r', length_includes_head=True)
plt.arrow(0, 0, 2, 4, head_width=0.3, head_length=0.5, color='b', length_includes_head=True)
plt.arrow(0, 0, 48/17, 12/17, head_width=0.3, head_length=0.5, color='gray', length_includes_head=True)
plt.arrow(48/17, 12/17, 2-48/17, 4-12/17, head_width=0.3, head_length=0.5, color='g', length_includes_head=True)
# plt.plot([48/17], [12/17], 'o')
# y=1/4x
# y=-4x+12
# x=48/17
# y=12/17
plt.annotate('b', xy=(1, 2), xytext=(-30, 15), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('a', xy=(-1, -0.25), xytext=(15, -30), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('e=b-p', xy=(2.5, 2), xytext=(30, 0), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('p=xa', xy=(2, 0.5), xytext=(-20, -40), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.grid()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
plt.close(fig)
从图中我们知道,向量
所以有
从上面的式子可以看出,如果将
设投影矩阵为
易看出
观察投影矩阵
投影矩阵的性质:
,投影矩阵是一个对称矩阵。 - 如果对一个向量做两次投影,即
,则其结果仍然与 相同,也就是 。
为什么我们需要投影?因为就像上一讲中提到的,有些时候
现在来看
现在问题的关键在于找
比较该方程与
再化简方程得
- 第一个问题:
; - 第二个问题:
,回忆在 中的情形,下划线部分就是原来的 ; - 第三个问题:易看出投影矩阵就是下划线部分
。
这里还需要注意一个问题,
再来看投影矩阵
:有 ,而 是对称的,所以其逆也是对称的,所以有 ,得证。 :有 ,得证。
最小二乘法
接下看看投影的经典应用案例:最小二乘法拟合直线(least squares fitting by a line)。
我们需要找到距离图中三个点
plt.style.use("seaborn-dark-palette")
fig = plt.figure()
plt.axis('equal')
plt.axis([-1, 4, -1, 3])
plt.axhline(y=0, c='black', lw='2')
plt.axvline(x=0, c='black', lw='2')
plt.plot(1, 1, 'o', c='r')
plt.plot(2, 2, 'o', c='r')
plt.plot(3, 2, 'o', c='r')
plt.annotate('(1, 1)', xy=(1, 1), xytext=(-40, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(2, 2)', xy=(2, 2), xytext=(-60, -5), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(3, 2)', xy=(3, 2), xytext=(-18, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.grid()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
plt.close(fig)
根据条件可以得到方程组
下一讲将进行最小二乘法的验算。