Explore at your own pace

Tutorials

Work through the examples in small steps. Try the tools, inspect the results and run the code.

Tutorial

PCA with small pixel images

Follow the pixel values through centering, principal components and reconstruction. Run the notebook in small steps.

How to run the code

I use Python in these tutorials. If you are starting out, Google Colab is a convenient way to run the examples. If you prefer to work on your own computer, use Jupyter Notebook.

Google Colab

Run Python in your browser without installing it on your computer. Open a new notebook, or upload a downloaded .ipynb file. Run the cells in order, starting with the imports and any installation instructions in the tutorial.

Open Google Colab ↗

Jupyter Notebook

Run the notebook on your own computer. Install Python, Jupyter Notebook, and the libraries listed in the tutorial. Keep the notebook and its input files in the expected folders, then run each cell in order.

See Jupyter installation instructions ↗

Try an example on this website

Some tutorials include interactive tools. Change a setting and inspect the result directly on the page. To study or change the Python code, use the accompanying notebook where one is provided.

Try PCA with small pixel images

Write a little, run it, and check the result

I suggest building a program in small parts. First decide what you want it to do and write the steps in ordinary language. For an image example, these steps might be: create an array, change some pixel values, and display the image. Write a few lines for the first step, run them, and check the output before adding the next step.

  1. Give each part one task. In a notebook, put each task in a separate code cell. Add a text cell to explain what you are doing and what result you expect.
  2. Start with a small example. A 4 × 4 array is easy to inspect. Once the code works, increase the image size or use your own data.
  3. Check values as well as pictures. Print the array shape, a few values, or a quantity you can calculate by hand. A program can run without an error and still give the wrong result.
  4. Change one thing at a time. Keep the working version, make a small change, and run it again. This makes it easier to identify which change caused a problem.

A small example in three cells

Here I create a 4 × 4 image. Its values are dimensionless: 0 is the background and 1 marks a selected pixel. Run the cells below in order.

Cell 1: Create the array and inspect it. The shape should be (4, 4), and all 16 entries should be zero.

import numpy as np
import matplotlib.pyplot as plt

image = np.zeros((4, 4))
print(image.shape)
print(image)

Cell 2: Change the four central pixels. The slice 1:3 selects indices 1 and 2; the end index 3 is excluded. The sum should be 4 and the mean should be 0.25. An assert statement checks a condition and reports an error if it is false.

image[1:3, 1:3] = 1
print(image)
print("Sum:", image.sum())
print("Mean:", image.mean())
assert image.sum() == 4

Cell 3: Display the image. You should see a white 2 × 2 square in a black background. Try changing one pixel and compare the displayed image with the printed array.

plt.imshow(image, cmap="gray", vmin=0, vmax=1,
           interpolation="nearest")
plt.xlabel("Column index")
plt.ylabel("Row index")
plt.xticks(range(4))
plt.yticks(range(4))
plt.show()

When a cell gives an error

Read the last line of the error message first. It usually names the error and gives a short description. Then look at the highlighted line in your code. Check spelling, indentation, variable names, array dimensions, and file locations. If a name is not defined, check whether you ran the cell that creates it. If a library cannot be imported, check that it is installed in the Python environment you are using.

Try to explain the problem and make a small correction yourself before asking for a complete replacement program. Print the relevant values or reduce the code to a few lines that still produce the error. This helps you understand the cause.

In Colab, you can also ask its AI assistant to explain an error. If an “Explain error” option is available, use it, or ask in the AI chat. For example: “Explain this error in simple terms. Which line causes it? Give me a hint before suggesting a fix.” Read the explanation, check the suggested change, and rerun your small test. A suggested fix still needs to produce the result you expect. See Colab’s guidance on its AI features.

When the notebook works, restart its kernel or runtime and run all cells from the beginning. This checks that the result does not depend on a variable left over from an earlier attempt. Save the notebook with a clear name.

Use an editor when the program grows

An IDE, or integrated development environment, brings together tools for writing, running, and debugging code. Visual Studio Code with its Python extension is one option. You can keep related files in a project folder, save a program as a .py file, and use the debugger to pause at a line and inspect variable values. Make sure the selected Python interpreter is the environment where you installed the required libraries. Follow the VS Code Python tutorial for setup and a first program.

More places to learn