ex7.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. %% Machine Learning Online Class
  2. % Exercise 7 | Principle Component Analysis and K-Means Clustering
  3. %
  4. % Instructions
  5. % ------------
  6. %
  7. % This file contains code that helps you get started on the
  8. % exercise. You will need to complete the following functions:
  9. %
  10. % pca.m
  11. % projectData.m
  12. % recoverData.m
  13. % computeCentroids.m
  14. % findClosestCentroids.m
  15. % kMeansInitCentroids.m
  16. %
  17. % For this exercise, you will not need to change any code in this file,
  18. % or any other files other than those mentioned above.
  19. %
  20. %% Initialization
  21. clear ; close all; clc
  22. %% ================= Part 1: Find Closest Centroids ====================
  23. % To help you implement K-Means, we have divided the learning algorithm
  24. % into two functions -- findClosestCentroids and computeCentroids. In this
  25. % part, you should complete the code in the findClosestCentroids function.
  26. %
  27. fprintf('Finding closest centroids.\n\n');
  28. % Load an example dataset that we will be using
  29. load('ex7data2.mat');
  30. % Select an initial set of centroids
  31. K = 3; % 3 Centroids
  32. initial_centroids = [3 3; 6 2; 8 5];
  33. % Find the closest centroids for the examples using the
  34. % initial_centroids
  35. idx = findClosestCentroids(X, initial_centroids);
  36. fprintf('Closest centroids for the first 3 examples: \n')
  37. fprintf(' %d', idx(1:3));
  38. fprintf('\n(the closest centroids should be 1, 3, 2 respectively)\n');
  39. fprintf('Program paused. Press enter to continue.\n');
  40. pause;
  41. %% ===================== Part 2: Compute Means =========================
  42. % After implementing the closest centroids function, you should now
  43. % complete the computeCentroids function.
  44. %
  45. fprintf('\nComputing centroids means.\n\n');
  46. % Compute means based on the closest centroids found in the previous part.
  47. centroids = computeCentroids(X, idx, K);
  48. fprintf('Centroids computed after initial finding of closest centroids: \n')
  49. fprintf(' %f %f \n' , centroids');
  50. fprintf('\n(the centroids should be\n');
  51. fprintf(' [ 2.428301 3.157924 ]\n');
  52. fprintf(' [ 5.813503 2.633656 ]\n');
  53. fprintf(' [ 7.119387 3.616684 ]\n\n');
  54. fprintf('Program paused. Press enter to continue.\n');
  55. pause;
  56. %% =================== Part 3: K-Means Clustering ======================
  57. % After you have completed the two functions computeCentroids and
  58. % findClosestCentroids, you have all the necessary pieces to run the
  59. % kMeans algorithm. In this part, you will run the K-Means algorithm on
  60. % the example dataset we have provided.
  61. %
  62. fprintf('\nRunning K-Means clustering on example dataset.\n\n');
  63. % Load an example dataset
  64. load('ex7data2.mat');
  65. % Settings for running K-Means
  66. K = 3;
  67. max_iters = 10;
  68. % For consistency, here we set centroids to specific values
  69. % but in practice you want to generate them automatically, such as by
  70. % settings them to be random examples (as can be seen in
  71. % kMeansInitCentroids).
  72. initial_centroids = [3 3; 6 2; 8 5];
  73. % Run K-Means algorithm. The 'true' at the end tells our function to plot
  74. % the progress of K-Means
  75. [centroids, idx] = runkMeans(X, initial_centroids, max_iters, true);
  76. fprintf('\nK-Means Done.\n\n');
  77. fprintf('Program paused. Press enter to continue.\n');
  78. pause;
  79. %% ============= Part 4: K-Means Clustering on Pixels ===============
  80. % In this exercise, you will use K-Means to compress an image. To do this,
  81. % you will first run K-Means on the colors of the pixels in the image and
  82. % then you will map each pixel onto its closest centroid.
  83. %
  84. % You should now complete the code in kMeansInitCentroids.m
  85. %
  86. fprintf('\nRunning K-Means clustering on pixels from an image.\n\n');
  87. % Load an image of a bird
  88. A = double(imread('bird_small.png'));
  89. % If imread does not work for you, you can try instead
  90. % load ('bird_small.mat');
  91. A = A / 255; % Divide by 255 so that all values are in the range 0 - 1
  92. % Size of the image
  93. img_size = size(A);
  94. % Reshape the image into an Nx3 matrix where N = number of pixels.
  95. % Each row will contain the Red, Green and Blue pixel values
  96. % This gives us our dataset matrix X that we will use K-Means on.
  97. X = reshape(A, img_size(1) * img_size(2), 3);
  98. % Run your K-Means algorithm on this data
  99. % You should try different values of K and max_iters here
  100. K = 16;
  101. max_iters = 10;
  102. % When using K-Means, it is important the initialize the centroids
  103. % randomly.
  104. % You should complete the code in kMeansInitCentroids.m before proceeding
  105. initial_centroids = kMeansInitCentroids(X, K);
  106. % Run K-Means
  107. [centroids, idx] = runkMeans(X, initial_centroids, max_iters);
  108. fprintf('Program paused. Press enter to continue.\n');
  109. pause;
  110. %% ================= Part 5: Image Compression ======================
  111. % In this part of the exercise, you will use the clusters of K-Means to
  112. % compress an image. To do this, we first find the closest clusters for
  113. % each example. After that, we
  114. fprintf('\nApplying K-Means to compress an image.\n\n');
  115. % Find closest cluster members
  116. idx = findClosestCentroids(X, centroids);
  117. % Essentially, now we have represented the image X as in terms of the
  118. % indices in idx.
  119. % We can now recover the image from the indices (idx) by mapping each pixel
  120. % (specified by its index in idx) to the centroid value
  121. X_recovered = centroids(idx,:);
  122. % Reshape the recovered image into proper dimensions
  123. X_recovered = reshape(X_recovered, img_size(1), img_size(2), 3);
  124. % Display the original image
  125. subplot(1, 2, 1);
  126. imagesc(A);
  127. title('Original');
  128. % Display compressed image side by side
  129. subplot(1, 2, 2);
  130. imagesc(X_recovered)
  131. title(sprintf('Compressed, with %d colors.', K));
  132. fprintf('Program paused. Press enter to continue.\n');
  133. pause;