myex7.m 5.4 KB

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