还有一个很好的图像上的例子,总结起来理解,也就是找出图像中像素特征值重要性程度,进行图片的压缩。也可以理解成提取关键特征。原文内容不错,保留一下代码

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# 读取数据
img_eg =mpimg.imread("../data/test.jpg")
print(img_eg.shape)
# 大小(552, 440, 4)

# 利用numpy进行矩阵分解
img_temp = img_eg.reshape(552, 440 * 4)
U,Sigma,VT = np.linalg.svd(img_temp)
print(U.shape,Sigma.shape,VT.shape)
# 分解后的大小(552, 552) (552,) (1760, 1760)

# 分别提取不同维度的特征进行还原
sval_nums = 60
img_restruct1 = (U[:, 0:sval_nums]).dot(np.diag(Sigma[0:sval_nums])).dot(VT[:sval_nums, :])
img_restruct1 = img_restruct1.reshape(552,440,4)


sval_nums = 120
img_restruct2 = (U[:,0:sval_nums]).dot(np.diag(Sigma[0:sval_nums])).dot(VT[0:sval_nums,:])
img_restruct2 = img_restruct2.reshape(552,440,4)

#用matplot进行可视化
fig, ax = plt.subplots(nrows=1, ncols=3, figsize = (24,32))
ax[0].imshow(img_eg)
ax[0].set(title = "src")

ax[1].imshow(img_restruct1.astype(np.uint8))
ax[1].set(title = "nums of sigma = 60")

ax[2].imshow(img_restruct2.astype(np.uint8))
ax[2].set(title = "nums of sigma = 120")
plt.show()

展示结果如图所示。。。

另外见压缩损失的均方误差


的第二部分