PCA在手写体MNIST数据集上做实验,对于很好地理解PCA工具的作用非常明显。
比如:数字9的不同识别体。
具体代码如下:
import os
import struct
import numpy as np
import matplotlib.pyplot as plt
def load_mnist(path, kind='train'):
"""Load MNIST data from `path`"""
labels_path = os.path.join(path,'%s-labels.idx1-ubyte'%kind)
print(labels_path)
#train-labels.idx1-ubyte
images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)
print(images_path)
#train-images.idx3-ubyte
with open(labels_path, 'rb') as lbpath:
magic,n = struct.unpack('>II',lbpath.read(8))
labels = np.fromfile(lbpath,dtype=np.uint8)
&nb