2020-06-12
閱讀量:
2418
SVD-矩陣奇異值分解 —— 原理與幾何意義
1.簡(jiǎn)介
SVD 全稱:Singular Value Decomposition。SVD 是一種提取信息的強(qiáng)大工具,它提供了一種非常便捷的矩陣分解方式,能夠發(fā)現(xiàn)數(shù)據(jù)中十分有意思的潛在模式。
主要應(yīng)用領(lǐng)域包括:
- 隱性語(yǔ)義分析 (Latent Semantic Analysis, LSA) 或隱性語(yǔ)義索引 (Latent Semantic Indexing, LSI);
- 推薦系統(tǒng) (Recommender system),可以說(shuō)是最有價(jià)值的應(yīng)用點(diǎn);
- 矩陣形式數(shù)據(jù)(主要是圖像數(shù)據(jù))的壓縮。
2.線性變換

3. SVD 推導(dǎo)



.4 實(shí)現(xiàn)代碼
以下是基于 python 的 numpy 庫(kù)實(shí)現(xiàn)的 SVD 矩陣分解的代碼,用于實(shí)現(xiàn)圖像壓縮的功能。
代碼鏈接 : https://github.com/zlxy9892/ml_
# -*- coding: utf-8 -*-
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
from sklearn import datasets
from skimage import io
def getImgAsMat(index):
ds = datasets.fetch_olivetti_faces()
return np.mat(ds.images[index])
def getImgAsMatFromFile(filename):
img = io.imread(filename, as_grey=True)
return np.mat(img)
def plotImg(imgMat):
plt.imshow(imgMat, cmap=plt.cm.gray)
plt.show()
def recoverBySVD(imgMat, k):
# singular value decomposition
U, s, V = la.svd(imgMat)
# choose top k important singular values (or eigens)
Uk = U[:, 0:k]
Sk = np.diag(s[0:k])
Vk = V[0:k, :]
# recover the image
imgMat_new = Uk * Sk * Vk
return imgMat_new
# -------------------- main --------------------- #
A = getImgAsMatFromFile('D:/pic.jpg')
plotImg(A)
A_new = recoverBySVD(A, 30)
plotImg(A_new)
這里用小黃人的大頭照做個(gè)例子看看:
原圖:

設(shè)置 k = 10,得到的壓縮圖:

k = 20:

k = 30:

繼續(xù)增加 k 值,將會(huì)得到越來(lái)越接近原始圖的壓縮圖。
5 參考資料






評(píng)論(0)


暫無(wú)數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論
0條評(píng)論