Ringkasan SVD memfaktorkan matriks menjadi basis ortogonal dan nilai singular untuk aproksimasi low-rank. Memotong nilai singular kecil membantu mereduksi noise sambil mempertahankan sinyal utama. SVD adalah fondasi penting untuk PCA dan berbagai model faktor laten.
Intuisi
# SVD memecah data menjadi mode independen yang diurutkan berdasarkan kekuatan kontribusinya. Mode kuat memberi representasi ringkas dengan kehilangan informasi terkontrol.
Penjelasan Rinci
# 1. Mengapa SVD?
# Setiap matriks (A) dapat ditulis sebagai (U\Sigma V^\top); memotong (\Sigma) memberi aproksimasi low-rank terbaik. SVD stabil dan tersedia di SciPy/NumPy. 2. Definisi
# $$A = U \Sigma V^\top$$(U): vektor singular kiri. (\Sigma): nilai singular terurut menurun. (V): vektor singular kanan. 3. Menyiapkan gambar
# 1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt
import japanize_matplotlib
from scipy import linalg
from PIL import Image
img = Image . open ( "./sample.png" ) . convert ( "L" ) . resize (( 163 , 372 )) . rotate ( 90 , expand = True )
img
4. Hitung SVD
# 1
2
3
4
X = np . asarray ( img )
U , Sigma , VT = linalg . svd ( X , full_matrices = True )
print ( f "A: { X . shape } , U: { U . shape } , Σ: { Sigma . shape } , V^T: { VT . shape } " )
5. Kompresi low-rank
# 1
2
3
4
5
6
7
8
9
for rank in [ 1 , 2 , 3 , 4 , 5 , 10 , 20 , 50 ]:
U_i = U [:, : rank ]
Sigma_i = np . matrix ( linalg . diagsvd ( Sigma [: rank ], rank , rank ))
VT_i = VT [: rank , :]
temp_image = np . asarray ( U_i * Sigma_i * VT_i )
plt . title ( f "rank= { rank } " )
plt . imshow ( temp_image , cmap = "gray" )
plt . show ()
Beberapa nilai singular saja sudah cukup menjaga kualitas visual.
6. Melihat vektor singular
# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
total = np . zeros (( 163 , 372 ))
for rank in [ 1 , 2 , 3 , 4 , 5 ]:
U_i = U [:, : rank ]
Sigma_i = np . matrix ( linalg . diagsvd ( Sigma [: rank ], rank , rank ))
VT_i = VT [: rank , :]
if rank > 1 :
for ri in range ( rank - 1 ):
Sigma_i [ ri , ri ] = 0
temp_image = np . asarray ( U_i * Sigma_i * VT_i )
total += temp_image
plt . figure ( figsize = ( 5 , 5 ))
plt . suptitle ( f "Kontribusi $u_ { rank } $" )
plt . subplot ( 211 )
plt . imshow ( temp_image , cmap = "gray" )
plt . subplot ( 212 )
plt . plot ( VT [ 0 ])
plt . show ()
plt . imshow ( total )
7. Tips
# Pilih jumlah nilai singular berdasarkan fraksi energi (\sum_{i=1}^k \sigma_i / \sum_j \sigma_j). Menghilangkan nilai kecil = denoising. PCA dapat dihitung lewat SVD pada data yang sudah dicentangkan. Gunakan SVD terpotong/randomized untuk matriks besar.