Source

ml-class / ex7.py

import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
import numpy as np


def image(wait=False):
    img = loadmat('ex7/bird_small.mat')['A']
    vectors = img.reshape(128*128, 3)
    km = KMeans(n_clusters=16, n_jobs=-1)
    km.fit(vectors)

    normed = np.array([km.cluster_centers_[i] for i in km.predict(vectors)])
    nimg = normed.reshape(128, 128, 3)

    fig = plt.figure()
    ax1 = fig.add_subplot(1, 2, 1)
    ax2 = fig.add_subplot(1, 2, 2)
    ax1.imshow(img)
    ax2.imshow(nimg)
    fig.show()
    if wait:
        raw_input('')


def pca():
    X = loadmat('ex7/ex7data1.mat')['X']
    pca = PCA(1)
    y = pca.fit_transform(X)
    Xt = pca.inverse_transform(y)
    plt.scatter(X[:,0], X[:,1])
    plt.scatter(Xt[:,0], Xt[:,1], color='red')
    plt.show()


def show_face(m):
    plt.imshow(m.reshape((32, 32)).T, plt.cm.gray)
    plt.show()


def faces():
    X = loadmat('ex7/ex7faces.mat')['X']
    Xc = scale(X, with_std=False)
    pca = PCA(2)
    Y = pca.fit_transform(Xc)
    Xp = pca.inverse_transform(Y)
    show_face(Xp[0])
    show_face(X[0])



if __name__ == '__main__':
    faces()