1 前言
有时,我们需要在一系列方向向量中,找到一个特征方向向量来表示这些方向向量的主方向。这里设已知的\(n\)个已经归一化的方向向量序列为 \(\mathbf{p}_1,\mathbf{p}_2,…,\mathbf{p}_n\), 待求的归一化后的特征方向向量为\(\mathbf{p}\)。当拿到\(\mathbf{p}\)后就可以进行后续其它操作。
2 理论
要求特征方向向量\(\mathbf{p}\), 这里得先约定特征方向向量形式。试想一下,所有序列向量均是从原点往外延伸1个单元得到一个位置,如果特征向量位置距离所有序列向量的位置最短,那可以作为一种特征的度量。因此,这里使用欧式距离和约定\(\mathbf{p}\)满足如下要求
(1)
$$
\begin{aligned}
\mathbf{Min}:e_1&=\sum_{i=1}^nw_i||\mathbf{p}-\mathbf{p}_i||_2^2
\\
&=2\sum_{i=1}^nw_i(1-\mathbf{p}\cdot \mathbf{p}_i)
\\
&\Updownarrow
\\
\mathbf{Max}:e_2&=\sum_{i=1}^nw_i\mathbf{p}\cdot \mathbf{p}_i
\end{aligned}
$$
公式里\(w_i\)为每个序列对应的权重,这里要求 \(w_i \geq 0\),一般情况下\(w_i=1\)即可。
在实际方向处理中,一般处理三维情况较多。因此,这里以三维情况来加以推导。需要注意的是,这里的向量为归一化的单位向量,特征主向量的各个值满足一定约束,为了释放约束,这里以\(-\pi<\theta\leq\pi,-\pi<\alpha\leq \pi\)来表示 \(\mathbf{p}\)
(2)
$$
\mathbf{p}=\begin{bmatrix}
\cos\theta \cos \alpha
\\
\cos \theta \sin\alpha
\\
\sin\theta
\end{bmatrix}
$$
且设如下值
(3)
$$
\begin{cases}
\mathbf{p}_i&=\begin{bmatrix}
x_i
\\
y_i
\\
z_i
\end{bmatrix}
\\
\\
a &=\sum_{i=1}^nw_ix_i
\\
\\
b &=\sum_{i=1}^nw_iy_i
\\
\\
c &=\sum_{i=1}^nw_iz_i
\end{cases}
$$
这里将(2)(3)代入(1)有
(4)
$$
\begin{aligned}
\mathbf{Max}:e_2&=\sum_{i=1}^nw_i\mathbf{p}\cdot \mathbf{p}_i
\\
&=\sum_{i=1}^nw_i(\cos\theta \cos \alpha x_i+\cos \theta \sin\alpha y_i+\sin\theta z_i)
\\
&=\cos\theta (a\cos \alpha + b\sin\alpha)+c\sin\theta
\end{aligned}
$$
对于(4), 当\(a=0,b=0\)时,明显可以得到如下解
(5)
$$
\begin{cases}
\theta &= \begin{cases}
\dfrac{\pi}{2}&c>0
\\
\\
-\dfrac{\pi}{2}& c \leq 0
\end{cases}
\\
\alpha &=任意值
\end{cases}
$$
接下来,讨论\(|a| +|b|\neq 0\)的情况。由于\(e_2\)是连续函数,因此\(e_2\)的最大值一定出现在极值处。这里令导数为0有
(6)
$$
\begin{cases}
\dfrac{d e_2}{d\theta} &=c\cos\theta -\sin\theta(a\cos\alpha+b\sin\alpha)&=0
\\
\\
\dfrac{d e_2}{d\alpha} &=\cos \theta(b\cos\alpha-a\sin\alpha)&=0
\end{cases}
$$
对于(6)的问题,这里先考虑\(\cos\theta =0\)的情况,这时(6)等价于如下问题
(7)
$$
\begin{cases}
\cos\theta &=0
\\
\\
a\cos \alpha + b\sin\alpha&=0
\end{cases}\Rightarrow
\begin{cases}
\theta&=\pm \dfrac{\pi}{2}
\\
\\
\alpha &=-\arctan(\dfrac{a}{b})
\end{cases}
$$
接下来考虑\(\cos\theta \neq 0\)的情况,对于(6)有
(8)
$$
\begin{cases}
\tan\theta(a\cos\alpha+b\sin\alpha)&=c
\\
\\
b\cos\alpha-a\sin\alpha&=0
\end{cases}\Rightarrow
\begin{cases}
\theta&=\arctan(\dfrac{c}{\sqrt{a^2+b^2}})
\\
\\
\alpha &=\arctan(\dfrac{b}{a})
\end{cases}
$$
由于(7)是(5)的特例,结合(8)可以得到如下三组候选解
(9)
$$
\begin{cases}
\theta&=\dfrac{\pi}{2}
\\
\\
\alpha &=-\arctan(\dfrac{a}{b})
\end{cases},\begin{cases}
\theta&=-\dfrac{\pi}{2}
\\
\\
\alpha &=-\arctan(\dfrac{a}{b})
\end{cases},\begin{cases}
\theta&=\arctan(\dfrac{c}{\sqrt{a^2+b^2}})
\\
\\
\alpha &=\arctan(\dfrac{b}{a})
\end{cases}
$$
因此,最终将(9)的3组解代入(4),选择最大值对应的解作为最佳解。
3 高维向量
对于维度为\(m>3\)的向量,可以使用上面的类似处理方式来进行处理。在三维空间里,本质上使用了4组解来确定出最佳特征向量,而对于维度为\(m\)的向量,需要使用\(2^{m-1}\)组解来确定。如果处理维度特别大,则计算效率不高,上面的方式不再合适,这时可考虑如下方式来计算特征方向向量
(10)
$$
\begin{aligned}
&w=0
\\
&\mathbf{p}=\mathbf{0}
\\
&\begin{cases}
\mathbf{p} = w\mathbf{p}+w_i\mathbf{p}_i
\\
\mathbf{p}=\dfrac{\mathbf{p}}{|\mathbf{p}|+\delta}
\\
w=w+w_i
\end{cases},i=1,2,3,…,n
\end{aligned}
$$
上面公式里\(\delta\)为大于0的小量。
观察公式(10)可以发现,这样计算的特征向量对序列顺序是敏感的,不同顺序结果有所差异。如果本身聚类向量方向比较接近,则不同顺序的结果差异也比较小。如果要算法对输入顺序不敏感,可以使用某种方式将序列排序后,再进行(10)的计算,这样设计的算法能保证顺序不敏感。