PCA 重建 Fashion_mnist 数据集

import tensorflow as tf
from tensorflow import keras
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from PIL import Image

读取数据

(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()
print(x_train.shape, y_train.shape)
(60000, 28, 28) (60000,)
# [60000, 28, 28] => [60000, 28 * 28]
x_train_reshape = x_train.reshape(-1, 28 * 28)
print(x_train_reshape.shape)
(60000, 784)
def printImage(images):
    plt.figure(figsize=(10, 10))
    for i in range(100):
        plt.subplot(10,10,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(images[i], cmap=plt.cm.binary)
# 保留图片 95% 的特征
pca = PCA(0.95)
pca.fit(x_train_reshape)
PCA(copy=True, iterated_power='auto', n_components=0.95, random_state=None,
    svd_solver='auto', tol=0.0, whiten=False)
# 需要 压缩到 187 个特征
pca.n_components_
187
# 提取图片主成分
x_train_reduction = pca.transform(x_train_reshape)
x_train_reduction.shape
(60000, 187)
# 由提取后的主成分还原图片
x_train_inverse = pca.inverse_transform(x_train_reduction)
# [60000, 28 * 28] => [60000, 28, 28]
x_train_inverse = x_train_inverse.reshape(-1, 28, 28)
# 输入的前 50 张+重建的前 50 张图片合并
x_concat = np.vstack([x_train[:50], x_train_inverse[:50]])
x_concat.shape
(100, 28, 28)
# x_concat.dtype='int32'
printImage(x_concat)
# 上面 5 行是原始图片, 下面 5 行是经过 PCA 提取主成分还原后的图片

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值