Skip to content

Commit 4cba7e6

Browse files
Exercise6 - Week7 Finished : K-Means & PCA
1 parent 8982931 commit 4cba7e6

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

machine-learning-ex7/ex7/computeCentroids.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
function centroids = computeCentroids(X, idx, K)
23
%COMPUTECENTROIDS returns the new centroids by computing the means of the
34
%data points assigned to each centroid.
@@ -25,7 +26,10 @@
2526
%
2627
% Note: You can use a for-loop over the centroids to compute this.
2728
%
28-
29+
for i = 1:K
30+
idx_sub = idx == i;
31+
centroids(i, :) = mean(X(idx_sub, :));
32+
end
2933

3034

3135

machine-learning-ex7/ex7/findClosestCentroids.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
% Set K
99
K = size(centroids, 1);
10+
% Get sample numbers
11+
m = size(X, 1);
1012

1113
% You need to return the following variables correctly.
12-
idx = zeros(size(X,1), 1);
14+
idx = zeros(m, 1);
1315

1416
% ====================== YOUR CODE HERE ======================
1517
% Instructions: Go over every example, find its closest centroid, and store
@@ -21,6 +23,18 @@
2123
% Note: You can use a for-loop over the examples to compute this.
2224
%
2325

26+
for i = 1:m
27+
idx_value = 1;
28+
dist_value = sum((centroids(1, :) - X(i, :)).^2);
29+
for j = 2:K
30+
dist_temp = sum((centroids(j, :) - X(i, :)).^2);
31+
if dist_temp < dist_value
32+
dist_value = dist_temp;
33+
idx_value = j;
34+
end
35+
end
36+
idx(i) = idx_value;
37+
end
2438

2539

2640

machine-learning-ex7/ex7/kMeansInitCentroids.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
% the dataset X
1414
%
1515

16-
16+
% Initialize the centroids to be random examples
17+
% Randomly reorder the indices of examples
18+
randidx = randperm(size(X, 1));
19+
% Take the first K examples as centroids
20+
centroids = X(randidx(1:K), :);
1721

1822

1923

machine-learning-ex7/ex7/pca.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
% number of examples).
2121
%
2222

23-
24-
23+
sigma = (X' * X) / m;
24+
[U, S, V] = svd(sigma);
2525

2626

2727

machine-learning-ex7/ex7/projectData.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
% x = X(i, :)';
1818
% projection_k = x' * U(:, k);
1919
%
20-
20+
U_reduce = U(:, 1:K);
21+
Z = X * U_reduce;
2122

2223

2324

machine-learning-ex7/ex7/recoverData.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
%
2121
% Notice that U(j, 1:K) is a row vector.
2222
%
23-
24-
23+
U_reduce = U(:, 1:K);
24+
X_rec = U_reduce * Z';
25+
X_rec = X_rec';
2526

2627
% =============================================================
2728

0 commit comments

Comments
 (0)