机器学习实战:SVD图像压缩
·
# coding=utf-8
from numpy import *
from numpy import linalg as la
def printMat(inMat, thresh=0.8):
for i in range(32):
for k in range(32):
if float(inMat[i,k]) > thresh:
print (1,end='')
else: print (0,end='')
print ('')
def imgCompress(numSV=3, thresh=0.8):
myl = []
for line in open(r'C:\Users\li\Downloads\machinelearninginaction\Ch14\0_5.txt').readlines():
newRow = []
for i in range(32):
newRow.append(int(line[i]))
myl.append(newRow)
myMat = mat(myl)
print( "****original matrix******")
printMat(myMat, thresh)
U,Sigma,VT = la.svd(myMat)
SigRecon = mat(zeros((numSV, numSV)))
for k in range(numSV):#construct diagonal matrix from vector
SigRecon[k,k] = Sigma[k]
reconMat = U[:,:numSV]*SigRecon*VT[:numSV,:]
print ("****reconstructed matrix using %d singular values******" % numSV)
printMat(reconMat, thresh)
imgCompress()****original 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
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)