Python for data · lesson 3 of 11
Parts in this lesson
Aa[2:5]a slice: start, up to but not including stop
BX[1, 2]row 1, column 2
CX[:, 2]every row, column 2: one feature
DX[X[:, 2] > 200]a mask: keep the rows where it’s True
EX[0].copy()a copy that’s safe to change

Reading parts of an array

Last updated

A model needs parts of its data all the time: one example, one feature, the first 80% of the rows for training, every row where a condition holds. In this lesson you'll pick numbers out of arrays by position, by slice, by a True/False mask and by a list of rows, and meet the one surprise in NumPy that catches everyone: a slice that changes the array it came from.

By the end of this lesson you can

  • Pick one number, one row or one column from a 1-D or 2-D array
  • Take slices with start, stop and step, including from the end
  • Filter rows with a Boolean mask, and pick rows with a list of positions
  • Tell when a slice is a view of the original, and make a copy when you need one

One dimension

Picking from a 1-D array works like a list: positions start at 0, and a slice start:stop:step goes up to but not including stop. Step through the picture, then run the cells:

Slices of a 1-D and a 2-D array. a = np.arange(0, 100, 10). Positions start at 0. a[3] is one number: 30.0102030405060708090a[3]0123456789
1 of 8 a = np.arange(0, 100, 10). Positions start at 0. a[3] is one number: 30.
slices.ipynb
  • a[3] is one number, 30. a[-1] counts from the end: 90.
  • a[2:5] is positions 2, 3 and 4.
  • a[::3] leaves out start and stop (the whole array) and takes every 3rd.

np.int32(30) is NumPy's way of showing one number that came out of an array: the value is 30, and it remembers its type.

Rows and columns

Here's a data matrix from lesson 0.2: five penguins, three features each. In two dimensions you give the row, then a comma, then the column.

penguins.ipynb
  • X[1, 2] is row 1, column 2: the second penguin's flipper length, 186.
  • X[0] is row 0 on its own: one example, all its features. A model's prediction for one penguin starts here.
  • X[:, 2] means every row (:), column 2: one feature, for all penguins. You'll do this to plot a feature, or to scale it.
  • X[1:3, :2] is rows 1 and 2, columns 0 and 1: a block.

Read : as "all of this direction". X[:, 2] and X[0, :] are the two moves you'll make most: a column is a feature, a row is an example.

Masks and lists of rows

A comparison on an array gives an array of True and False, one for each number. Put that inside the square brackets, and you get the numbers where it's True. Run it:

masks.ipynb
  • prices > 100 is a Boolean mask: True, False, for each price.
  • prices[prices > 100] keeps the prices over ₱100: 120, 250 and 101.
  • X[:, 2] > 200 asks a question of one column, and X[long_flippers] keeps the whole rows where the answer is yes. That's how you'll pick every penguin of one species, or every example the model got wrong.
  • X[[0, 3]] uses a list of positions to pick rows 0 and 3. In lesson 0.7 you'll shuffle the positions of every row and use a list like this to split data into training and test sets.

A mask can combine conditions with & (and) and | (or), each in brackets: X[(X[:, 2] > 190) & (X[:, 1] < 17)]. Try it in the notebook.

Wrong way: changing a slice

Say you want to try a change on one penguin without touching the data. Run the first cell:

views.ipynb

first = X[0] didn't copy the row. It made a view: a new name for the same numbers in memory. Setting first[2] = 0 changed X too, and the data now says this penguin has no flipper. Nothing warned you.

A view shares memory with the array it came from. a = np.array([45, 46, 45, 44, 47]): one array, one block of memory.4546454447a
1 of 4 a = np.array([45, 46, 45, 44, 47]): one array, one block of memory.

Basic slices (X[0], X[:, 2], X[1:3]) are views, because copying a big array every time you looked at part of it would be slow. Masks and lists of positions (X[X > 0], X[[0, 3]]) always make copies.

The fix is the second cell: .copy() makes a separate array, so changing it leaves X alone. Use it whenever you take part of your data to change it. A model that quietly edits its own training data is one of the hardest bugs to find.

Check what you learned

X has shape (100, 4). What shape is X[:, 1]?
a = np.array([5, 12, 8, 20]). What is a[a > 10]?
Predict, then check with step 4’s notebook: col = X[:, 2], then col[0] = 999. Does X change?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning