PCA-MNIST识别集应用实例

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)
    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))
        images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)
    return images, labels
#-----------------------------------------------------------------------------
X_train,y_train=load_mnist("d:\\")
print(shape(X_train),shape(y_train))
X_test,y_test=load_mnist("d:\\",kind="t10k")
print(shape(X_test),shape(y_test))
def Plt_numberfrom0to10():
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(nrows=2,ncols=5,sharex=True,sharey=True, )
    ax = ax.flatten()
    for i in range(10):
        img = X_train[y_train == i][0].reshape(28, 28)
        ax[i].imshow(img, cmap='Greys', interpolation='nearest')
    ax[0].set_xticks([])
    ax[0].set_yticks([])
    plt.tight_layout()
    plt.show()
def Plt_number(t):
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(nrows=5,ncols=5,sharex=True,sharey=True, )
    ax = ax.flatten()
    for i in range(25):
        img = X_train[y_train == t][i].reshape(28, 28)
        ax[i].imshow(img, cmap='Greys', interpolation='nearest')
    ax[0].set_xticks([])
    ax[0].set_yticks([])
    plt.tight_layout()
    plt.show()
Plt_numberfrom0to10()
for i in range(10):
    Plt_number(i)
def main(X_train,y_train,X_test,y_test):
    import numpy as np
#    from sklearn.datasets import fetch_mldata
    #mnist=fetch_mldata("MNIST original")
    #print(mnist)
    X_train=np.array(X_train,dtype=float)
    y_train=np.array(y_train,dtype=float)
    X_test=np.array(X_test,dtype=float)
    y_test=np.array(y_test,dtype=float)
    print(y_train)
main(X_train,y_train,X_test,y_test)
from sklearn.neighbors import KNeighborsClassifier
knn_clf=KNeighborsClassifier()
knn_clf.fit(X_train,y_train)
print("normal:",knn_clf.score(X_test,y_test))



猜你喜欢

转载自blog.csdn.net/weixin_42039090/article/details/80518672
今日推荐