| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- %% Machine Learning Online Class
- % Exercise 7 | Principle Component Analysis and K-Means Clustering
- %
- % Instructions
- % ------------
- %
- % This file contains code that helps you get started on the
- % exercise. You will need to complete the following functions:
- %
- % pca.m
- % projectData.m
- % recoverData.m
- % computeCentroids.m
- % findClosestCentroids.m
- % kMeansInitCentroids.m
- %
- % For this exercise, you will not need to change any code in this file,
- % or any other files other than those mentioned above.
- %
- %% Initialization
- clear ; close all; clc
- %% ================== Part 1: Load Example Dataset ===================
- % We start this exercise by using a small dataset that is easily to
- % visualize
- %
- fprintf('Visualizing example dataset for PCA.\n\n');
- % The following command loads the dataset. You should now have the
- % variable X in your environment
- load ('ex7data1.mat');
- % Visualize the example dataset
- plot(X(:, 1), X(:, 2), 'bo');
- axis([0.5 6.5 2 8]); axis square;
- fprintf('Program paused. Press enter to continue.\n');
- %% =============== Part 2: Principal Component Analysis ===============
- % You should now implement PCA, a dimension reduction technique. You
- % should complete the code in pca.m
- %
- fprintf('\nRunning PCA on example dataset.\n\n');
- % Before running PCA, it is important to first normalize X
- [X_norm, mu, sigma] = featureNormalize(X);
- % Run PCA
- [U, S] = pca(X_norm);
- % Compute mu, the mean of the each feature
- % Draw the eigenvectors centered at mean of data. These lines show the
- % directions of maximum variations in the dataset.
- hold on;
- drawLine(mu, mu + 1.5 * S(1,1) * U(:,1)', '-k', 'LineWidth', 2);
- drawLine(mu, mu + 1.5 * S(2,2) * U(:,2)', '-k', 'LineWidth', 2);
- hold off;
- fprintf('Top eigenvector: \n');
- fprintf(' U(:,1) = %f %f \n', U(1,1), U(2,1));
- fprintf('\n(you should expect to see -0.707107 -0.707107)\n');
- fprintf('Program paused. Press enter to continue.\n');
- %% =================== Part 3: Dimension Reduction ===================
- % You should now implement the projection step to map the data onto the
- % first k eigenvectors. The code will then plot the data in this reduced
- % dimensional space. This will show you what the data looks like when
- % using only the corresponding eigenvectors to reconstruct it.
- %
- % You should complete the code in projectData.m
- %
- fprintf('\nDimension reduction on example dataset.\n\n');
- % Plot the normalized dataset (returned from pca)
- plot(X_norm(:, 1), X_norm(:, 2), 'bo');
- axis([-4 3 -4 3]); axis square
- % Project the data onto K = 1 dimension
- K = 1;
- Z = projectData(X_norm, U, K);
- fprintf('Projection of the first example: %f\n', Z(1));
- fprintf('\n(this value should be about 1.481274)\n\n');
- X_rec = recoverData(Z, U, K);
- fprintf('Approximation of the first example: %f %f\n', X_rec(1, 1), X_rec(1, 2));
- fprintf('\n(this value should be about -1.047419 -1.047419)\n\n');
- % Draw lines connecting the projected points to the original points
- hold on;
- plot(X_rec(:, 1), X_rec(:, 2), 'ro');
- for i = 1:size(X_norm, 1)
- drawLine(X_norm(i,:), X_rec(i,:), '--k', 'LineWidth', 1);
- end
- hold off
- fprintf('Program paused. Press enter to continue.\n');
- pause;
|