Miki Tebeka avatar Miki Tebeka committed 58e4a84

ex7

Comments (0)

Files changed (2)

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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.