aboutsummaryrefslogtreecommitdiff
path: root/Assignment 2/ex22.py
diff options
context:
space:
mode:
Diffstat (limited to 'Assignment 2/ex22.py')
-rw-r--r--Assignment 2/ex22.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/Assignment 2/ex22.py b/Assignment 2/ex22.py
new file mode 100644
index 0000000..3487752
--- /dev/null
+++ b/Assignment 2/ex22.py
@@ -0,0 +1,88 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Sun Oct 11 09:28:15 2015
+
+@author: Camil Staps (s4498062)
+
+Run with Python 2.7
+"""
+
+import itertools
+import matplotlib.pyplot as plt
+import matplotlib.pylab as plab
+from mpl_toolkits.mplot3d import Axes3D
+import numpy as np
+import scipy.io
+
+# 2.2.1
+zipd = scipy.io.loadmat('./Data/zipdata.mat')
+traindata = zipd['traindata']
+testdata = zipd['testdata']
+
+data = traindata[:,1:]
+classes = traindata[:,0]
+temp = [(d, c) for d, c in zip(data, classes) if c < 2]
+[data, classes] = [np.array(t) for t in zip(*temp)]
+
+mean = data.mean(0)
+
+# First visualisation
+for i in range(10):
+ plt.subplot(2, 5, i)
+ image = plab.reshape(data[i,:], (16, 16))
+ plt.imshow(image, extent=(0, 16, 0, 16), cmap=plab.cm.gray_r)
+ plt.axis('off')
+plt.show()
+
+# PCA
+Y = data - np.ones((len(data), 1)) * mean
+U, S, Vt = np.linalg.svd(Y, full_matrices=False)
+V = Vt.T
+Z = np.dot(Y, V[:,0:4])
+
+W = np.dot(Z[:10], V[:,0:4].T) + mean
+for i in range(10):
+ plt.subplot(2, 5, i)
+ image = plab.reshape(W[i,:], (16, 16))
+ plt.imshow(image, extent=(0, 16, 0, 16), cmap=plab.cm.gray_r)
+ plt.axis('off')
+plt.show()
+
+Y0 = [d for c, d in zip(classes, Y) if c == 0]
+Y1 = [d for c, d in zip(classes, Y) if c == 1]
+plt.figure(figsize=(16,16))
+for i, j in itertools.product(*[range(4), range(4)]):
+ plt.subplot(4, 4, 4 * i + j + 1)
+
+ Z1 = np.dot(Y0, V[:,i:i + 1])
+ Z2 = np.dot(Y0, V[:,j:j + 1])
+ plt.scatter(Z1, Z2, color='r', marker='.', s=1, label='0')
+ Z1 = np.dot(Y1, V[:,i:i + 1])
+ Z2 = np.dot(Y1, V[:,j:j + 1])
+ plt.scatter(Z1, Z2, color='b', marker='.', s=1, label='1')
+
+ plt.ylabel('PC' + str(i))
+ plt.xlabel('PC' + str(j))
+ plt.gca().axes.get_xaxis().set_ticks([])
+ plt.gca().axes.get_yaxis().set_ticks([])
+plt.legend(bbox_to_anchor=(1.05, 1), loc=2)
+plt.show()
+
+fig = plt.figure(figsize=(8,8))
+ax = fig.add_subplot(111, projection='3d')
+Z1 = np.dot(Y0, V[:,0:1])
+Z2 = np.dot(Y0, V[:,1:2])
+Z3 = np.dot(Y0, V[:,2:3])
+ax.scatter(Z1, Z2, Z3, color='r', marker='.', s=10, label='0')
+Z1 = np.dot(Y1, V[:,0:1])
+Z2 = np.dot(Y1, V[:,1:2])
+Z3 = np.dot(Y1, V[:,2:3])
+ax.scatter(Z1, Z2, Z3, color='b', marker='.', s=10, label='1')
+ax.set_xlabel('PC1')
+ax.set_ylabel('PC2')
+ax.set_zlabel('PC3')
+ax.set_xticks([])
+ax.set_yticks([])
+ax.set_zticks([])
+plt.legend(bbox_to_anchor=(1.05, 1), loc=2)
+plt.show()