Commits

Miki Tebeka committed 58e4a84

ex7

Comments (0)

Files changed (2)

ex7.pdf

Binary file added.
 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():
+
+def image(wait=False):
     img = loadmat('ex7/bird_small.mat')['A']
     vectors = img.reshape(128*128, 3)
-    km = KMeans(n_clusters=16)
+    km = KMeans(n_clusters=16, n_jobs=-1)
     km.fit(vectors)
 
     normed = np.array([km.cluster_centers_[i] for i in km.predict(vectors)])
     ax1.imshow(img)
     ax2.imshow(nimg)
     fig.show()
-    raw_input('')
+    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__':
-    image()
-
-
+    faces()