-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise7.py
More file actions
39 lines (28 loc) · 834 Bytes
/
exercise7.py
File metadata and controls
39 lines (28 loc) · 834 Bytes
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
def findClosestCentroids(X, centroids):
K = centroids.shape[0]
idx = np.zeros(X.shape[0], dtype=int)
for i, x in enumerate(X):
distance = [np.sqrt(np.sum(np.square(x - c))) for c in centroids]
idx[i] = np.argmin(distance)
return idx
def computeCentroids(X, idx, K):
m, n = X.shape
centroids = np.zeros((K, n))
Ck = np.zeros(K)
for i, c_i in enumerate(idx):
centroids[c_i] = centroids[c_i] + X[i]
Ck[c_i] += 1
for i, c in enumerate(centroids):
c /= Ck[i]
return centroids
def pca(X):
m, n = X.shape
U = np.zeros(n)
S = np.zeros(n)
covariance = (X.T@X) / m
U, S, V = np.linalg.svd(covariance)
return U, S
def projectData(X, U, K):
return X @ U[:, :K]
def recoverData(Z, U, K):
return Z @ (U[:, :K]).T