Programming Exercise 7:
K-means Clustering and Principal Component
Analysis
Machine Learning
November 29, 2011
Introduction
In this exercise, you will implement the K-means clustering algorithm and
apply it to compress an image. In the second part, you will us e principal
component analysis to find a low-dimensional representation of face images.
Before starting on the programming exercise, we strongly recommend watch-
ing the video lectures and completing the review questions for the associated
topics.
To get started with the exercise, you will need to download the starter
code and unzip its contents to the directory where you wish to complete
the exercise. If needed, use the cd command in Octave to change to this
directory before starting this exercise.
Files included in this exercise
ex7.m - Octave/Matlab script for the first exercise on K-means
ex7 pca.m - Octave/Matlab script for the second exercise on PCA
ex7data1.mat - Example Dataset for PCA
ex7data2.mat - Example Dataset for K-me ans
ex7faces.mat - Faces Dataset
bird small.png - Example Image
displayData.m - Displays 2D data stored in a matrix
drawLine.m - Draws a line over an exsiting figure
plotDataPoints.m - Initialization for K-means centroids
plotProgresskMeans.m - Plots each step of K-means as it proceeds
1