PCA:菜馆菜肴推荐系统、基于SVD的图像压缩
链接: 数据集 提取码: 97dw
一 PCA:菜馆菜肴推荐系统
二 基于SVD的图像压缩
内容:请用文字描述
我获得了什么?
我掌握了哪些知识?
还存在哪些不足的方法
from numpy import *
from numpy import linalg as la
'''
原始图像大小为32*32=1024像素,我们能否使用更少的像素来表示这张图呢?
由于矩阵含有浮点数,因此必须定义浅色和深色,这里通过一个阈值来界定
设计函数遍历所有的矩阵元素,当元素大于阈值打印1,否则打印0
'''
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('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(2)