Skip to content

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:

  1. Use NumPy to read text files.
  2. Create a data matrix from a text file
  3. Organise measured data into a data matrix
  4. Compute the variance of a set of measurements
  5. Compute the covariance between two sets of measurements
  6. Use the function pairplot from the seaborn package to visualise the covariance between multiple sets of measurements
  7. Compute the covariance matrix from multiple sets of measurements using matrix multiplications
  8. Compute the covariance matrix from multiple sets of measurements using the NumPy cov function.
  9. Compute the principal components using Eigenvector analysis (NumPy function eig).
  10. Visualize how much of the total of variation each principal component explain.
  11. Project original measurements into principal component space
  12. Use the function pairplot to visualise the covariance structure after projecting to principal component space.
  13. 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.