-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_yale.py
55 lines (40 loc) · 1.01 KB
/
ex5_yale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
import matplotlib.pyplot as plt
from scipy.sparse.linalg import svds
import os
import numpy as np
def load_data(path):
list_dir = os.listdir(path)
X = []
for img in list_dir:
if img == "Readme.txt":
continue
else:
im = plt.imread(path + img)
X.append(im.flatten())
return np.matrix(X)
def show_image(X):
image = X[random.randint(0, 165)]
image = image.reshape((243, 320))
plt.imshow(image, cmap='gray')
plt.show()
def main():
# task a
X = load_data("data/yalefaces/")
# task b
m = X.mean(axis=0)
X_centered = X - m
# task c
u, s, vt = np.linalg.svd(X_centered, full_matrices=False)
# u, s, vt = svds(X_centered)
# task d
p = 60
V_p = vt.T[:, 0:p]
Z = np.dot(X_centered, V_p)
# task e
X_rec = m + np.dot(Z, V_p.T)
error = np.square(np.subtract(X_rec, X)).sum()
print "Reconstraction error:", error
show_image(X_rec)
if __name__ == '__main__':
main()