Intro
SVD分解是 singular value decomposition的縮寫,也就是奇異值分解,它是spectral decomposition譜分解的推廣(譜分解適用於方陣)。在機器學習中,這是一種非常有用的降維手段,另外它還可以構建主題詞模型,可謂是功能豐富啊。本文通過一個簡單的例子,來手把手演示SVD分解的數學步驟。
數學表示,分解矩陣 \(A = \Gamma\Lambda\Delta^T\)
其中A是nxp的,\(\Gamma\)是nxr的,\(\Delta\)是pxr的,而\(\Lambda = diag(\lambda^{1/2}, ... , \lambda^{1/2})\)
另外,\(\Gamma^T\Gamma = \Delta^T\Delta=I_r\),這表示他們倆都是關於列垂直。
實例
給定一個2X3的矩陣 A = \(\begin{bmatrix}1 & 1 & 0 \\ 0 & 1 &1\end{bmatrix}\),rank(A)=2
Step1
計算,\(AA^T = \begin{bmatrix}1 & 1 & 0 \\ 0 & 1 & 1\end{bmatrix}\begin{bmatrix}1&0 \\ 1&1 \\ 0&1\end{bmatrix} = \begin{bmatrix}2&1 \\ 1&2\end{bmatrix}\) 的特征值和特征向量
\(det(AA^T - \lambda I) = \begin{vmatrix}2-\lambda & 1 \\ 1 & 2-\lambda\end{vmatrix} \overset{set}{=} 0\)
得到,\(\lambda_1 = 1, \lambda_2 = 3\), \(v_1 = a\begin{pmatrix}1\\-1\end{pmatrix}, v_2 = b\begin{pmatrix}1\\1\end{pmatrix}\)
Step2
計算,\(A^TA = \begin{bmatrix}1&0 \\ 1&1 \\ 0&1\end{bmatrix}\begin{bmatrix}1&1&0 \\ 0 & 1 & 1\end{bmatrix} = \begin{bmatrix}1&1&0 \\ 1&2&1 \\ 0&1&1\end{bmatrix}\) 的特征值和特征向量
得到,\(\lambda_1 = 3, \lambda_2 = 1, \lambda_3 = 0\), \(v_1 = a\begin{pmatrix}1\\2\\1\end{pmatrix}, v_2 = b\begin{pmatrix}1\\0\\1\end{pmatrix}, v_3=c\begin{pmatrix}1\\-1\\1\end{pmatrix}\)
Step3
根據前面得到的非零特征及其對應的特征向量(取norm1),就可以分別得到\(\Gamma, \Lambda, \Delta\)
\(AA^T: \lambda_1 = 1, \lambda_2 = 3, v_1 = \begin{pmatrix}1/\sqrt{2} \\ -1/\sqrt{2}\end{pmatrix}, v_2 = \begin{pmatrix}1/\sqrt{2} \\ 1/\sqrt{2}\end{pmatrix}\)
因此,\(\Gamma = \begin{bmatrix}1/\sqrt{2} & 1/\sqrt{2} \\ -1/\sqrt{2} & 1/\sqrt{2} \end{bmatrix}\)
\(\Lambda = \begin{bmatrix} 1 & 0 \\ 0 & \sqrt{3}\end{bmatrix}\)
\(A^TA: \lambda_1 = 3, \lambda_2 = 1, v_1 = \begin{pmatrix}1/\sqrt{6} \\ 2/\sqrt{6} \\ 1/\sqrt{6}\end{pmatrix}, v_2 = \begin{pmatrix}1/\sqrt{2} \\ 0 \\ -1/\sqrt{2}\end{pmatrix}\)
因此,\(\Delta = \begin{bmatrix}1/\sqrt{6} & 1/\sqrt{2} \\ 2/\sqrt{6} & 0 \\ 1/\sqrt{6} & -1/\sqrt{2}\end{bmatrix}\)
Overall,\(A = \Gamma\Lambda\Delta^T = \begin{bmatrix}1/\sqrt{2} & 1/\sqrt{2} \\ -1/\sqrt{2} & 1/\sqrt{2} \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & \sqrt{3}\end{bmatrix} \begin{bmatrix}1/\sqrt{6} & 1/\sqrt{2} \\ 2/\sqrt{6} & 0 \\ 1/\sqrt{6} & -1/\sqrt{2}\end{bmatrix}^T\)