NumPy - 奇異值分解



什麼是奇異值分解 (SVD)?

奇異值分解,通常縮寫為 SVD,是線性代數中的一種矩陣分解技術。SVD 將一個矩陣分解成三個其他矩陣,捕捉原始矩陣的重要屬性。

例如,如果你有一個矩陣 A,則 SVD 表示為:

A = UΣVT

這裡,UV 是正交矩陣,Σ 是對角矩陣。

U 的列稱為左奇異向量,V 的列(或 VT 的行)稱為右奇異向量,Σ 的元素為奇異值。

NumPy 中的 SVD

NumPy 提供了 `numpy.linalg.svd()` 函式來計算矩陣的奇異值分解。讓我們來看一個例子。

示例

在這個例子中,矩陣 A 被分解成三個矩陣:UΣ(表示為奇異值陣列 S)和 VT

import numpy as np

# Define a 3x3 matrix
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

# Compute the Singular Value Decomposition
U, S, VT = np.linalg.svd(A)

print("Matrix U:\n", U)
print("Singular values:", S)
print("Matrix V^T:\n", VT)

以下是獲得的輸出:

Matrix U:
[[-0.21483724  0.88723069  0.40824829]
 [-0.52058739  0.24964395 -0.81649658]
 [-0.82633754 -0.38794278  0.40824829]]
Singular values: [1.68481034e+01 1.06836951e+00 4.41842475e-16]
Matrix V^T:
[[-0.47967118 -0.57236779 -0.66506441]
 [-0.77669099 -0.07568647  0.62531805]
 [-0.40824829  0.81649658 -0.40824829]]

理解各個組成部分

SVD 的各個組成部分具有特定的屬性和作用,如下所示:

  • 矩陣 U:U 的列是 A 的左奇異向量。這些向量構成 A 的列空間的正交基。
  • 奇異值:Σ 的對角線元素是 A 的奇異值。這些值給出 A 沿相應奇異向量的作用大小。
  • 矩陣 VTVT 的行是 A 的右奇異向量。這些向量構成 A 的行空間的正交基。

重建原始矩陣

你可以從它的 SVD 組成部分重建原始矩陣 A。在 NumPy 中,你可以使用 `numpy.dot()` 函式進行矩陣乘法來實現這一點。

示例

在下面的例子中,我們正在重建原始矩陣 "A":

import numpy as np

# Define a 3x3 matrix
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

# Compute the Singular Value Decomposition
U, S, VT = np.linalg.svd(A)

# Create the diagonal matrix Σ from the singular values
Sigma = np.zeros((3, 3))
np.fill_diagonal(Sigma, S)

# Reconstruct the original matrix
A_reconstructed = np.dot(U, np.dot(Sigma, VT))

print("Original matrix:\n", A)
print("Reconstructed matrix:\n", A_reconstructed)

使用其 SVD 組成部分成功地重建了原始矩陣 A,證明了分解的準確性。

Original matrix:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Reconstructed matrix:
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

SVD 的應用

SVD 是一種強大的工具,具有許多應用,例如:

  • 降維:在資料分析和機器學習中,SVD 用於在保留重要資訊的同時減少維數。
  • 影像壓縮:SVD 用於透過減少儲存影像所需的資料量來壓縮影像。
  • 降噪:SVD 可以透過識別和丟棄小的奇異值來幫助去除資料中的噪聲。
  • 訊號處理:在訊號處理中,SVD 用於分析和濾波訊號。
  • 推薦系統:SVD 用於推薦系統來預測使用者偏好。

示例:使用 SVD 進行影像壓縮

讓我們來看一個如何使用 SVD 進行影像壓縮的例子。我們將使用灰度影像,並透過僅保留最重要的奇異值來壓縮它:

import numpy as np
import matplotlib.pyplot as plt
from skimage import data, color

# Load a sample image and convert it to grayscale
image = color.rgb2gray(data.astronaut())  
# Compute the Singular Value Decomposition
U, S, VT = np.linalg.svd(image, full_matrices=False)

# Retain only the first k singular values
k = 50
U_k = U[:, :k]
S_k = np.diag(S[:k])
VT_k = VT[:k, :]

# Reconstruct the compressed image
image_compressed = np.dot(U_k, np.dot(S_k, VT_k))

# Plot the original and compressed images
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title(f"Compressed Image with k={k}")
plt.imshow(image_compressed, cmap='gray')
plt.axis('off')

plt.show()

原始影像和壓縮後的影像並排顯示,展示了 SVD 如何在保留其基本特徵的同時減小影像大小。

SVD Compression

SVD 的優點

SVD 提供了一些優點,例如:

  • 數值穩定性:SVD 數值穩定,可以處理病態矩陣。
  • 最佳低秩逼近:SVD 提供矩陣的最佳低秩逼近,使其成為降維的理想選擇。
  • 魯棒性:SVD 對資料中的微小擾動具有魯棒性。
  • 通用性:SVD 可以應用於任何矩陣,無論其屬性如何。
廣告