Module 2 · NumPy and SciPy · Lesson 12 of 66

Indexing and selection

Grab single values, slices, rows, columns and values that pass a test.

In this lesson · about 18 minutes

  • Index and slice 1-D and 2-D arrays
  • Know that a slice is a view, and when to copy
  • Select values with a condition

Indexing a 1-D array works like a list: arr[8] is one value and arr[1:5] is a slice.

Arrays can also broadcast: assign one value to a whole slice and it fills every position, arr[0:5] = 100. A list can't do that.

2-D arrays

The format is arr_2d[row, col]. arr_2d[row][col] also works, but the comma form is clearer and is what we'll use. Each side can be a single index or a slice:

  • arr_2d[1] is the whole second row.
  • arr_2d[1, 0] is one value.
  • arr_2d[:2, 1:] is the top two rows, from column 1 to the end.
  • arr_2d[:, 0] is the first column.
Loading a grid…

Fancy indexing

Pass a list of row numbers to pick those rows, in any order: arr2d[[6, 4, 2, 7]].

Selecting with a condition

A comparison on an array gives an array of True and False. Put that inside the brackets to keep only the True positions. This is how you'll filter data all through the course.

Reproduce the output

For each exercise below, mat is already made for you:

mat = np.arange(1, 26).reshape(5, 5)

array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

Exercise

Bottom-right block

Write code that gives:

array([[12, 13, 14, 15],
       [17, 18, 19, 20],
       [22, 23, 24, 25]])
Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

One value

Write code that gives 20.

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

A column, kept as a column

Write code that gives:

array([[ 2],
       [ 7],
       [12]])
Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

The last row

Write code that gives array([21, 22, 23, 24, 25]).

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

The last two rows

Write code that gives:

array([[16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])
Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

Multiples of 3

Select every value in mat that is divisible by 3. You should get a flat array starting array([ 3, 6, 9, ….

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Practice sets mastered: 0 of 1

Exercises solved: 0 of 6

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.