# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1323159727 18000 # Node ID 069653867b3b2cf7357571ed48b0853d09bd15c4 # Parent be1f915bd52a500513333a4dafd63eb71bb0fff6 Implement PCA diff --git a/pca.m b/pca.m --- a/pca.m +++ b/pca.m @@ -1,31 +1,9 @@ function [U, S] = pca(X) -%PCA Run principal component analysis on the dataset X -% [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X -% Returns the eigenvectors U, the eigenvalues (on diagonal) in S -% - -% Useful values -[m, n] = size(X); - -% You need to return the following variables correctly. -U = zeros(n); -S = zeros(n); + ##PCA Run principal component analysis on the dataset X + ## [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X + ## Returns the eigenvectors U, the eigenvalues (on diagonal) in S + ## -% ====================== YOUR CODE HERE ====================== -% Instructions: You should first compute the covariance matrix. Then, you -% should use the "svd" function to compute the eigenvectors -% and eigenvalues of the covariance matrix. -% -% Note: When computing the covariance matrix, remember to divide by m (the -% number of examples). -% + [U, S, ~] = svd (X'*X/rows (X)); - - - - - - -% ========================================================================= - -end +endfunction diff --git a/projectData.m b/projectData.m --- a/projectData.m +++ b/projectData.m @@ -1,26 +1,11 @@ function Z = projectData(X, U, K) -%PROJECTDATA Computes the reduced data representation when projecting only -%on to the top k eigenvectors -% Z = projectData(X, U, K) computes the projection of -% the normalized inputs X into the reduced dimensional space spanned by -% the first K columns of U. It returns the projected examples in Z. -% - -% You need to return the following variables correctly. -Z = zeros(size(X, 1), K); + ##PROJECTDATA Computes the reduced data representation when projecting only + ##on to the top k eigenvectors + ## Z = projectData(X, U, K) computes the projection of + ## the normalized inputs X into the reduced dimensional space spanned by + ## the first K columns of U. It returns the projected examples in Z. + ## -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the projection of the data using only the top K -% eigenvectors in U (first K columns). -% For the i-th example X(i,:), the projection on to the k-th -% eigenvector is given as follows: -% x = X(i, :)'; -% projection_k = x' * U(:, k); -% - - - - -% ============================================================= + Z = X*U(:,1:K); end diff --git a/recoverData.m b/recoverData.m --- a/recoverData.m +++ b/recoverData.m @@ -1,28 +1,11 @@ function X_rec = recoverData(Z, U, K) -%RECOVERDATA Recovers an approximation of the original data when using the -%projected data -% X_rec = RECOVERDATA(Z, U, K) recovers an approximation the -% original data that has been reduced to K dimensions. It returns the -% approximate reconstruction in X_rec. -% - -% You need to return the following variables correctly. -X_rec = zeros(size(Z, 1), size(U, 1)); - -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the approximation of the data by projecting back -% onto the original space using the top K eigenvectors in U. -% -% For the i-th example Z(i,:), the (approximate) -% recovered data for dimension j is given as follows: -% v = Z(i, :)'; -% recovered_j = v' * U(j, 1:K)'; -% -% Notice that U(j, 1:K) is a row vector. -% - - - -% ============================================================= + ##RECOVERDATA Recovers an approximation of the original data when using the + ##projected data + ## X_rec = RECOVERDATA(Z, U, K) recovers an approximation the + ## original data that has been reduced to K dimensions. It returns the + ## approximate reconstruction in X_rec. + ## + + X_rec = Z*U(:, 1:K)'; end