SVD(Singular Value Decomposition,奇异值分解)
算法优缺点:
- 优点:简化数据,去除噪声,提高算法结果
- 缺点:数据的转换可能难于理解
- 适用数据类型:数值型数据
算法思想:
很多情况下,数据的一小部分包含了数据的绝大部分信息,线性代数中有很多矩阵的分解技术可以将矩阵表示成新的易于处理的形式,不同的方法使用与不同的情况。最常见的就是SVD,SVD将数据分成三个矩阵U(mm),sigma(mn),VT(nn),这里得到的sigma是一个对角阵,其中对角元素为奇异值,并且它告诉了我们重要的特征。
这里的实现用的也是numpy种的函数linalg.svd()
实例:用SVD进行图像压缩
这里的数据集是前面用于手写识别的一个数据,大小为32*32=1024像素,因为进行svd之后我们的数据变成一堆浮点数,所以输出函数要改进一下,设置一个阀值(这个值的设置会影响显示效果)。可以看出完成压缩之后我们只需要两个奇异值和U、VT两个矩阵,共计64+64+2=130个像素,达到了近十倍压缩比,而且还原出来的图像基本不变
数据如下:
执行结果:
*********orignal matrix**************
00000000000000110000000000000000
00000000000011111100000000000000
00000000000111111110000000000000
00000000001111111111000000000000
00000000111111111111100000000000
00000001111111111111110000000000
00000000111111111111111000000000
00000000111111100001111100000000
00000001111111000001111100000000
00000011111100000000111100000000
00000011111100000000111110000000
00000011111100000000011110000000
00000011111100000000011110000000
00000001111110000000001111000000
00000011111110000000001111000000
00000011111100000000001111000000
00000001111100000000001111000000
00000011111100000000001111000000
00000001111100000000001111000000
00000001111100000000011111000000
00000000111110000000001111100000
00000000111110000000001111100000
00000000111110000000001111100000
00000000111110000000011111000000
00000000111110000000111111000000
00000000111111000001111110000000
00000000011111111111111110000000
00000000001111111111111110000000
00000000001111111111111110000000
00000000000111111111111000000000
00000000000011111111110000000000
00000000000000111111000000000000
****reconstructed matrix using 3 singular values******
00000000000000000000000000000000
00000000000000000000000000000000
00000000000010111110000000000000
00000000000011111110000000000000
00000000000111111111000000000000
00000000001111111111110000000000
00000000001111111111110000000000
00000000011100000000111000000000
00000000111100000000111100000000
00000001111100000000111100000000
00000001111100000000011100000000
00000001111100000000011100000000
00000001111100000000011100000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001110000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001111000000
00000000111100000000001110000000
00000000111100000000111100000000
00000000001111111111111000000000
00000000001111111111110000000000
00000000001111111111110000000000
00000000000011111111110000000000
00000000000011111111100000000000
00000000000000000000000000000000
#coding=utf-8
from numpy import *
def printMat(inMat, thresh=0.8):
for i in range(32):
for j in range(32):
if float(inMat[i,j]) > thresh:
print 1,
else:
print 0,
print ' ' def imgCompress(numSV=3, thresh=0.8):
myl = []
for line in open('0_5.txt').readlines():
newRow = []
for i in range(32):
newRow.append(int(line[i]))
myl.append(newRow)
myMat = mat(myl)
print '*********orignal matrix**************'
printMat(myMat,thresh)
U, sigmal, VT = linalg.svd(myMat)
SigRecon =mat(zeros((numSV,numSV)))
for k in range(numSV):
SigRecon[k,k] = sigmal[k]
reconMat = U[:,:numSV] * SigRecon * VT[:numSV,:]
print "****reconstructed matrix using %d singular values******" % numSV
printMat(reconMat, thresh) def main():
imgCompress() if __name__ == '__main__':
main()