Lesson 6 of 147 min
NumPy arrays
A NumPy array is a grid of numbers with a shape, rows first, and you pick from it with one position or one slice for each side.
After this lesson you can create, index and slice arrays and read their shape.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
An array you don't write by hand
In the lesson the zero came from a library, and the corner was written by hand. Real code often makes an array in one line. np.arange counts from 0 up to a number, without it, and reshape lays those numbers out as rows. The shape you ask for has to hold them all: 3 rows of 4 is 12.
import numpy as np
a = np.arange(12)
print(a)
grid = a.reshape(3, 4)
print(grid)
print(grid.shape, grid.dtype)[ 0 1 2 3 4 5 6 7 8 9 10 11] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] (3, 4) int64
Whole numbers or decimals
dtype says what kind of number an array keeps. Whole numbers give int64. The zero from the lesson keeps decimals, float64, which is why thirteen shows as 13.0. np.zeros makes an array of a shape you give, all zeros, as decimals.
import numpy as np
z = np.zeros((2, 3))
print(z)
print(z.dtype)
print(np.array([[1, 2], [3, 4]]).dtype)[[0. 0. 0.] [0. 0. 0.]] float64 int64
Counting from the end, and a step
A position can count from the end of a side: -1 is the last one. And a slice can take a step: ::2 takes every second position. Here the last column of the zero (no ink on its right edge), then every second row, then every second column.
from sklearn import datasets
img = datasets.load_digits().images[0]
print(img[:, -1])
print(img[::2].shape)
print(img[:, ::2].shape)[0. 0. 0. 0. 0. 0. 0. 0.] (4, 8) (8, 4)
A slice changes the picture
The lesson said a slice looks at img's own numbers and doesn't copy them. So if you change the slice, you change the picture too. Here the 3 by 3 block is set to zero, and row 3 of img loses its ink on the left.
from sklearn import datasets
img = datasets.load_digits().images[0]
part = img[2:5, 1:4]
part[:] = 0
print(img[3])[0. 0. 0. 0. 0. 8. 8. 0.]
Keep the picture with copy
When you want to change a piece and keep the picture as it was, take a copy first. .copy() makes new numbers of its own, so row 3 keeps its 4.0 and its 12.0.
from sklearn import datasets
img = datasets.load_digits().images[0]
part = img[2:5, 1:4].copy()
part[:] = 0
print(img[3])[ 0. 4. 12. 0. 0. 8. 8. 0.]
Run on Python 3.13.3, NumPy 2.5.5 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
img is the zero, with the shape (8, 8). What does img[2] give?
2 of 2
What does this print?
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
print(a[1, 2])The ideas in this lesson
- An array and its shape
- A NumPy array is a grid of numbers. Its shape says how many there are along each side, rows first.
- Indexing
- One position for each side picks one number, rows first. Positions go from 0 to one less than the size.
- Slicing
- A slice like 2:5 takes positions 2, 3 and 4. A colon takes a whole side; a single position takes its side away.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
A picture in numbers
Here's a bet. This is a zero, written by hand. It's the way a computer keeps it. It's a grid of numbers, eight rows of eight. The bigger the number, the darker the ink. In Python, the whole picture is one NumPy array. Now look at this line. It asks the picture for position two. What comes out? One number, or a whole row?
An array
Keep your guess. You'll check it in a minute. To check it, you need two things first. How does Python hold a picture like this?
Line three loads almost eighteen hundred handwritten digits. They come with scikit-learn, a library for machine learning. Line four takes the first one, img.
Img is a NumPy array: numbers in a grid. Sixty-four of them, one for each square, and each says how dark that square is. NumPy keeps them as decimals, so thirteen is written thirteen point zero.
Look at the shade... and there's the zero.
Its shape
How big is it? Every array knows how many numbers it has along each side.
That's its shape.
Img's shape is eight by eight. Eight rows... then eight columns. Rows come before columns.
Writing one
Can you write an array yourself? Most of them come from a library, like this one. But yes. Import NumPy as np.
Then give np.array a list of rows, and each row is a list.
Here's the zero's top-left corner, written by hand. Two rows of three numbers. Its shape is two by three.
One position
Img is the zero, eight rows of eight. Now, back to the question. What does position two give you?
Positions count from zero, as in a list. Line five keeps what it cuts out, and calls it part.
And one position picks along the first side: the rows.
So you get a whole row. The third row, all eight numbers. Look, there are the two sides of the zero. Its shape is just eight, written with a comma after it. That's how Python writes a shape with one number. Part has one side: a row.
If you said one number, think of a list. A list has one side. This picture has two... and you gave a position for only one of them.
One number
So how do you get one number? Give a position for each side. Rows first.
Row two, then column three.
One number: two point zero, a light square inside the zero. One number has no sides at all. So its shape is empty: two brackets with nothing inside.
Row eight
The picture has eight rows. So is position eight the bottom row?
It stops. Index error. Index eight is out of bounds for axis zero, with size eight.
Index is another word for position. Out of bounds means outside the picture. Axis zero is NumPy's word for the first side: the rows. And size eight means the positions go from zero to seven.
If you said the bottom row, you were counting from one. The bottom row is seven.
A slice
Img is the zero, eight rows of eight, and one position picks one row. So how do you cut out a piece of the picture? With a slice. Two, colon, five is a slice. It goes from position two up to five... and five is not taken.
How many rows will that be?
Three. Rows two, three and four.
Its shape is three by eight.
A block
Now add a slice for the columns too: one to four. That cuts out a block, three by three. It's the left side of the zero.
And look at the lit squares. They're the same squares. Part looks at img's own numbers. It doesn't copy them.
A column
And a whole column? A colon on its own takes a whole side. So colon, then four, means every row. Column four. A line down through the empty middle of the zero.
Before it runs: will it be eight rows of one number?
Or something else?
Not eight rows of one. It's just eight, with that comma. The numbers come from a column. But one position on the columns takes that side away. So part has one side: eight numbers in a row.
All the pictures
Now look back at line four. Digits dot images holds all the pictures, almost eighteen hundred of them, in one array. Its shape has three numbers. What could three numbers count?
First the pictures. Then the rows, then the columns of each picture.
The zero in square brackets is one position, on the first side. It picks the first picture... and that side is gone. Eight by eight. It's the same move you made on the picture: one position, and one side gone.
And like any side, its positions stop one before its size. So one past the last picture stops with the same error.
A model learns from a stack of pictures shaped like this one.
Rows first
So here's what you saw. An array is a grid of numbers, with a shape.
Rows come first. One position for each side picks a number. A slice takes a run of them. And a single position takes its side away.
A grid and its shape
So a picture is a grid of numbers. Its shape tells you how to cut it. Now try it on this lesson's page, and take the quiz. Get the bottom row of the picture, without the number seven.
You use this lesson in Lab 3: A thousand digits, no loop, after Vectorization and broadcasting.
Facts last checked on .
