奇异值分解(SVD)和简单图像压缩
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
1 #coding=utf-8 2 from numpy import * 3 def printMat(inMat, thresh=0.8): 4 for i in range(32): 5 for j in range(32): 6 if float(inMat[i,j]) > thresh: 7 print 1, 8 else: 9 print 0, 10 print ' ' 11 12 def imgCompress(numSV=3, thresh=0.8): 13 myl = [] 14 for line in open('0_5.txt').readlines(): 15 newRow = [] 16 for i in range(32): 17 newRow.append(int(line[i])) 18 myl.append(newRow) 19 myMat = mat(myl) 20 print '*********orignal matrix**************' 21 printMat(myMat,thresh) 22 U, sigmal, VT = linalg.svd(myMat) 23 SigRecon =mat(zeros((numSV,numSV))) 24 for k in range(numSV): 25 SigRecon[k,k] = sigmal[k] 26 reconMat = U[:,:numSV] * SigRecon * VT[:numSV,:] 27 print "****reconstructed matrix using %d singular values******" % numSV 28 printMat(reconMat, thresh) 29 30 def main(): 31 imgCompress() 32 33 if __name__ == '__main__': 34 main()