NumPy Indexing · Exercises
Data

NumPy Indexing Exercises

4 exercises that build on the NumPy Indexing and Slicing lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: NumPy indexing is how you pick values out of an array: by position and slice (arr[row, col]), by a True/False mask, or by a list of positions, and the way you pick decides whether you get a view of the original data or an independent copy. By the end you can select single values, rows, columns and blocks of a 2-D array, tell views from copies, filter and update values with combined masks, and use fancy indexing, np.where and argsort to find and reorder data. You will practise: Slice a seating grid; Predict views, copies and fancy indexing; Stop the function changing the original; Filter and rank the rows of a table.

0 / 4 completed

NumPy Indexing practice exercises

1. Slice a seating grid

Write code Easy

seats is a 2-D array: each row is a row of seats in a hall. Write two functions:

  • back_row(grid) returns the last row of grid.
  • first_two_columns(grid) returns the first two columns of every row, so its shape is (number_of_rows, 2).

Both must work for a grid of any size.

Your code
import numpy as np

seats = np.arange(1, 21).reshape(4, 5)

def back_row(grid):
    pass

def first_two_columns(grid):
    pass

print(back_row(seats))
print(first_two_columns(seats))

2. Predict views, copies and fancy indexing

Predict the output Medium

Read the code and type exactly what it prints. Think about which lines change prices and which only change a copy.

Read this code
import numpy as np

prices = np.array([10, 20, 30, 40, 50])
cheap = prices[:3]
cheap[0] = 99
safe = prices[1:4].copy()
safe[0] = -1
print(prices)
print(safe)
print(prices[[4, 0, 4]])
print(prices[prices > 25].sum())

3. Stop the function changing the original

Fix the bug Medium

capped(readings, limit) should return a new array in which every value above limit is replaced by limit. The array passed in must not change, but right now it does. Fix the bug.

Your code
import numpy as np

def capped(readings, limit):
    result = readings
    result[result > limit] = limit
    return result

data = np.array([12, 55, 30, 80, 41])
print(capped(data, 50))
print(data)

4. Filter and rank the rows of a table

Write code Hard

results is a 2-D array where each row is [player_id, score, minutes]. Write two functions that work on any table and any column number col:

  • rows_between(table, col, low, high) returns the rows whose value in column col is between low and high inclusive, keeping the original row order. Use a boolean mask.
  • best_rows(table, col, n) returns the n rows with the largest value in column col, largest first. Use argsort.
Your code
import numpy as np

results = np.array([[1, 72, 30],
                    [2, 95, 45],
                    [3, 60, 20],
                    [4, 88, 50],
                    [5, 79, 35]])

def rows_between(table, col, low, high):
    pass

def best_rows(table, col, n):
    pass

print(rows_between(results, 1, 70, 90))
print(best_rows(results, 1, 2))

Next lessonNumPy Math and Broadcasting

More NumPy Indexing and Slicing examples · All Python exercises