Skip to content

Commit c54dea9

Browse files
solves exercise 7
1 parent 1848141 commit c54dea9

39 files changed

+3694
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ __Instructor__: Andrew Ng.
112112
- [Principal Component Analysis](week8/principal-component-analysis.md)
113113

114114
### Programming Exercises
115+
- [Exercise 7: Questions and Explanations](week8/ex7.pdf)
116+
- [K Means Clustering and PCA(Principal Component Analysis)](week8/ex7)
117+
- [Perform PCA(Principal Component Analysis)](week8/ex7/pca.m)
118+
- [Project a Dataset into lower dimensional space](week8/ex7/projectData.m)
119+
- [Recover the Original Data from the Projection](week8/ex7/recoverData.m)
120+
- [Find Closest Centroids Using K-Means](week8/ex7/findClosestCentroids.m)
121+
- [Compute Centroid Means](week8/ex7/computeCentroids.m)
122+
- [Initialize K means for Centroids](week8/ex7/kMeansInitCentroids.m)
115123

116124
## Week 9
117125
### Quizzes
-21.8 KB
Binary file not shown.
-21.6 KB
Binary file not shown.
-32.9 KB
Binary file not shown.

week8/ex7.pdf

699 KB
Binary file not shown.

week8/ex7/bird_small.mat

44.5 KB
Binary file not shown.

week8/ex7/bird_small.png

32.3 KB
Loading

week8/ex7/computeCentroids.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function centroids = computeCentroids(X, idx, K)
2+
%COMPUTECENTROIDS returns the new centroids by computing the means of the
3+
%data points assigned to each centroid.
4+
% centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by
5+
% computing the means of the data points assigned to each centroid. It is
6+
% given a dataset X where each row is a single data point, a vector
7+
% idx of centroid assignments (i.e. each entry in range [1..K]) for each
8+
% example, and K, the number of centroids. You should return a matrix
9+
% centroids, where each row of centroids is the mean of the data points
10+
% assigned to it.
11+
12+
[m n] = size(X);
13+
centroids = zeros(K, n);
14+
frequency = zeros(K, 1);
15+
for i = 1:m
16+
frequency(idx(i))++;
17+
centroids(idx(i), :) += X(i, :);
18+
endfor
19+
mask = centroids == 0;
20+
frequency = maskZeroAsOne(frequency);
21+
centroids = centroids ./ frequency;
22+
23+
function mat = maskZeroAsOne(mat)
24+
mask = mat == 0;
25+
mat += mask;
26+
endfunction
27+
end

week8/ex7/displayData.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function [h, display_array] = displayData(X, example_width)
2+
%DISPLAYDATA Display 2D data in a nice grid
3+
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
4+
% stored in X in a nice grid. It returns the figure handle h and the
5+
% displayed array if requested.
6+
7+
% Set example_width automatically if not passed in
8+
if ~exist('example_width', 'var') || isempty(example_width)
9+
example_width = round(sqrt(size(X, 2)));
10+
end
11+
12+
% Gray Image
13+
colormap(gray);
14+
15+
% Compute rows, cols
16+
[m n] = size(X);
17+
example_height = (n / example_width);
18+
19+
% Compute number of items to display
20+
display_rows = floor(sqrt(m));
21+
display_cols = ceil(m / display_rows);
22+
23+
% Between images padding
24+
pad = 1;
25+
26+
% Setup blank display
27+
display_array = - ones(pad + display_rows * (example_height + pad), ...
28+
pad + display_cols * (example_width + pad));
29+
30+
% Copy each example into a patch on the display array
31+
curr_ex = 1;
32+
for j = 1:display_rows
33+
for i = 1:display_cols
34+
if curr_ex > m,
35+
break;
36+
end
37+
% Copy the patch
38+
39+
% Get the max value of the patch
40+
max_val = max(abs(X(curr_ex, :)));
41+
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
42+
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
43+
reshape(X(curr_ex, :), example_height, example_width) / max_val;
44+
curr_ex = curr_ex + 1;
45+
end
46+
if curr_ex > m,
47+
break;
48+
end
49+
end
50+
51+
% Display Image
52+
h = imagesc(display_array, [-1 1]);
53+
54+
% Do not show axis
55+
axis image off
56+
57+
drawnow;
58+
59+
end

week8/ex7/drawLine.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function drawLine(p1, p2, varargin)
2+
%DRAWLINE Draws a line from point p1 to point p2
3+
% DRAWLINE(p1, p2) Draws a line from point p1 to point p2 and holds the
4+
% current figure
5+
6+
plot([p1(1) p2(1)], [p1(2) p2(2)], varargin{:});
7+
8+
end

0 commit comments

Comments
 (0)