Mercurial > hg > machine-learning-hw7
comparison pca.m @ 3:069653867b3b
Implement PCA
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 06 Dec 2011 03:22:07 -0500 |
parents | ded78d0b4987 |
children |
comparison
equal
deleted
inserted
replaced
2:be1f915bd52a | 3:069653867b3b |
---|---|
1 function [U, S] = pca(X) | 1 function [U, S] = pca(X) |
2 %PCA Run principal component analysis on the dataset X | 2 ##PCA Run principal component analysis on the dataset X |
3 % [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X | 3 ## [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X |
4 % Returns the eigenvectors U, the eigenvalues (on diagonal) in S | 4 ## Returns the eigenvectors U, the eigenvalues (on diagonal) in S |
5 % | 5 ## |
6 | 6 |
7 % Useful values | 7 [U, S, ~] = svd (X'*X/rows (X)); |
8 [m, n] = size(X); | |
9 | 8 |
10 % You need to return the following variables correctly. | 9 endfunction |
11 U = zeros(n); | |
12 S = zeros(n); | |
13 | |
14 % ====================== YOUR CODE HERE ====================== | |
15 % Instructions: You should first compute the covariance matrix. Then, you | |
16 % should use the "svd" function to compute the eigenvectors | |
17 % and eigenvalues of the covariance matrix. | |
18 % | |
19 % Note: When computing the covariance matrix, remember to divide by m (the | |
20 % number of examples). | |
21 % | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 % ========================================================================= | |
30 | |
31 end |