Machine learning

Stage 1 · Lesson 9 of 9

Images as NumPy arrays

Python for data

To a computer, a colour photo is just a of shape (height, width, 3): one number from 0 to 255 for the red, green and blue of every . Load a few images with Pillow and with and print their shape, dtype and a pixel value.

Crop by slicing, flip with [:, ::-1] and turn to greyscale by averaging channels, so the array stops feeling abstract. Two differences cause endless bugs later. OpenCV stores channels as BGR while almost everything else expects RGB, and wants channels first, (3, H, W), as floats rather than integers.

Common trapfeeding a model images in the wrong channel order or unscaled 0 to 255 values: it still trains, just badly, and nothing warns you.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • The Python, Pandas and Data Visualization micro courses, in browser notebooks

Keep handy

  • Start with 10 minutes to pandas, then look up indexing, groupby and reshaping

  • Quick NumPy, pandas and Matplotlib lookups with a try-it editor

Mark this stage as not done?