Latest Page Update: 23-09-2026
Solution is now available! Download the full solution from here: Solution
Principal Component Analysis (PCA) in Python¶
Introduction¶
This exercise is an introduction to principal component analysis in Python. The classical iris data set is used as an example. Check here for an explanation.
Thanks to Jonas Attrup Rasmussen for providing the markdown version of the exercise.
Learning Objectives¶
After completing this exercise, the student should be able to do the following:
- Use NumPy to read text files.
- Create a data matrix from a text file
- Organise measured data into a data matrix
- Compute the variance of a set of measurements
- Compute the covariance between two sets of measurements
- Use the function pairplot from the seaborn package to visualise the covariance between multiple sets of measurements
- Compute the covariance matrix from multiple sets of measurements using matrix multiplications
- Compute the covariance matrix from multiple sets of measurements using the NumPy cov function.
- Compute the principal components using Eigenvector analysis (NumPy function eig).
- Visualize how much of the total of variation each principal component explain.
- Project original measurements into principal component space
- Use the function pairplot to visualise the covariance structure after projecting to principal component space.
- Compute the PCA using the PCA function from the sci-kit learn decomposition package.
Getting The Data¶
Start by creating an exercise folder where you keep your data, Python scripts and/or Notebooks. Download the data you'll need for this exercise by clicking here: Data
Place the data into the folder you just created.
Alternatively, you can also fetch the data for the whole course through the Image Analysis GitHub repository. See the Data and GitHub section for more information. If you're using Git, it may be wise run:
git pull
As this will fetch updates to the material, which may happen throughout the course.