Skip to content

Commit edfc473

Browse files
Mark LindermanMark Linderman
Mark Linderman
authored and
Mark Linderman
committed
ex7 pca
1 parent 0f78e7d commit edfc473

File tree

6 files changed

+10
-11
lines changed

6 files changed

+10
-11
lines changed

ML Notes.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,11 @@ Keep in mind that the goal is to translate a point in 2 dimensional space (x,y)
507507

508508
So the procedure is that first, you compute the "covariance" matrix (called Sigma, which, confusingly, is not a sum operation though it looks a lot like one and is also *not* the standard deviation). And then use that matrix called Sigma to compute the "eigenvectors" of that matrix. Say what? He glossed over the details but computing eigenvectors is the same as a "single value decomposition". You can do this in octave using the svd function. There are equivalents in other languages (like Python, I'd assume). The octave svd function, returns 3 vectors, U, S and V - or maybe that's a matrix...
509509

510-
So, to get into the details, to compute the covariance matrix, you take each sample *column*, or feature (not rows this time), and multiply it by it's transposed version (producing an n x n matrix) and then add them all together which still results in an n x n matrix. (Don't forget: n is the feature index and m the samples index.) Averaging that over the number of samples ($\frac{1}{m}$) just gives you a different n x n matrix. That's $\Sigma$.
510+
So, to get into the details, to compute the covariance matrix, you take each sample *column*, or feature (not rows this time), and multiply it by it's transposed version (producing an n x n matrix) and then add all the matrices that result (1 per feature)together. The result is still an n x n matrix. (Don't forget: n is the feature index and m the samples index.) Averaging that over the number of samples ($\frac{1}{m}$) just gives you a different n x n matrix. That's $\Sigma$.
511511

512-
$$ \Sigma = \frac{1}{m}\sum_{1-1}^m(x^{(i)})(x^{(i)})^T $$
512+
$$ \Sigma = \frac{1}{m}\sum_{i-1}^m(x^{(i)})(x^{(i)})^T $$
513513

514-
That's from Ng. But it can't be right since he claims it'll result in an n x n matrix. But if $x^{(i)}$ is a single example, a row, as it's always been throughout the course, then this will result in 1 x 1 vector. But it doesn't matter since the vectorized version below does look like it will return an n x n matrix. So, moving on, the next step (in octave):
514+
That's from Ng. But it can't be right since he claims it'll result in an n x n matrix. But if $x^{(i)}$ is a single example, a row, as it's always been throughout the course, then this will result in 1 x 1 vector. (I think he must mean to reverse those two terms, transposed one first.) It doesn't matter since the vectorized version below does look like it will return an n x n matrix. So, moving on, the next step (in octave):
515515

516516
$$ [U,S,V] = svd(\Sigma) $$
517517

machine-learning-ex7/ex7/ex7.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
% indices in idx.
165165

166166
% We can now recover the image from the indices (idx) by mapping each pixel
167-
% (specified by its index in idx) to the centroid value
167+
% (specified by its index in idx) to the centroid value (1 of 16 colors)
168168
X_recovered = centroids(idx,:);
169169
Y_recovered = centroidsB(idxB,:);
170170

machine-learning-ex7/ex7/pca.m

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
% number of examples).
2121
%
2222

23-
24-
25-
26-
23+
% calling it covariance here - also commonly known as Sigma
24+
covariance = (1/m) * transpose(X) * X;
25+
[U, S, _] = svd(covariance);
2726

2827

2928
% =========================================================================

machine-learning-ex7/ex7/projectData.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
% projection_k = x' * U(:, k);
1919
%
2020

21-
21+
Z = X * U(:, K);
2222

2323

2424
% =============================================================

machine-learning-ex7/ex7/recoverData.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
% Notice that U(j, 1:K) is a row vector.
2222
%
2323

24-
24+
X_rec = Z * transpose(U(:, K));
2525

2626
% =============================================================
2727

machine-learning-ex7/ex7/token.mat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created by Octave 4.4.1, Fri Feb 15 21:20:49 2019 EST <[email protected]>
1+
# Created by Octave 4.4.1, Sun Feb 17 21:26:38 2019 EST <[email protected]>
22
# name: email
33
# type: sq_string
44
# elements: 1

0 commit comments

Comments
 (0)