NumPy · Chapter 12 of 18

Sorting and unique values

By the end of this chapter you can sort an array with np.sort and a.sort(), along an axis, get the order that sorts it with np.argsort and use that order on other arrays, rank from the top with [::-1], find the top few with np.partition and np.argpartition, place values in sorted bands with np.searchsorted and np.digitize, count what is there with np.unique, and compare two lists with np.isin and NumPy's set functions. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Analysis
  • 4 tasks
  • NumPy
  • In the all labs plan

The first 2 chapters of NumPy are free. This one comes with the all labs plan.

Why you need this

The day's numbers are in arrays now, and the questions have changed from "how much" to "which ones". Bhanwar has one morning to walk the rows, so he wants the weakest panels in order, worst first, each with its id. Rukmini wants each row's best panels at noon, to see which rows keep up. Meenal wants every block's price put in a band (cheap, normal, dear, very dear) to plan when the plant should sell. And Bhanwar's list of suspect panels and the thermal drone's list overlap: which panels are on both, and which on only one?

With what you know so far, "the ten weakest" means a loop that keeps the ten smallest seen so far, or Python's own sorted() on a list of 2,400 (energy, id) pairs. The loop is easy to get wrong at the edges, and Python's sort works on float objects one comparison at a time. NumPy sorts the numbers in their own block of memory, in C, and it can hand you the order instead of the sorted values, which is what "with its id" needs.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    row_leaders.py: the best panels of every row

    Write row_leaders(noon, k) in row_leaders.py. noon is one block's grid of output in watts, shape (rows, cols), such as the day's block 48 of shape (40, 60); k is a whole number. Return, for every row, the column numbers of that row's k highest readings, highest first, as an int array of shape (rows, k): row_leaders(noon, 3) for a (40, 60) grid has shape (40, 3), and row r holds the three columns of row r's best panels.

    Return column numbers, not watts, and rank each row on its own. Two panels with exactly the same reading may come in either order. The Check tries blocks 48 and 40 of the day with k of 3 and of 1, and a grid of 2 rows by 4 columns with k of 2.

  2. Task 2

    price_band.py: the price band of every block

    Write price_bands(prices, edges) in price_band.py. edges is a sorted array of prices in rupees a MWh, such as [3000, 5000, 7000]. Band 0 is below the first edge, band 1 from the first edge up to the second, and so on; with three edges the top band, 3, is 7000 and over. A price exactly on an edge goes in the band above it, so 5000 is band 2. Return every price's band as an int array of the same shape as prices: price_bands(np.array([2350.0, 5000.0, 9174.0]), np.array([3000.0, 5000.0, 7000.0])) is [0 2 3].

    The Check tries 96 prices with the edges [3000, 5000, 7000], prices that sit exactly on those edges, the two edges [4000, 8000], and prices of shape (2, 3). Use no Python loop: a for, a while or a comprehension in price_bands fails the task.

  3. Task 3

    two_lists.py: two lists of suspect panels

    Bhanwar's list of panels he thinks are weak and the thermal drone's list are arrays of panel ids as strings, such as "P1273", of different lengths, and a list may name a panel more than once. Write two functions in two_lists.py:

    on_both(walked, drone) returns the ids that are on both lists, each once, sorted, as an array of strings.

    on_one(walked, drone) returns the ids that are on exactly one of the two lists, each once, sorted, as an array of strings.

    For walked ["P1273", "P1336", "P1273", "P0462"] and drone ["P1336", "P1217", "P1273"], on_both gives ['P1273' 'P1336'] and on_one gives ['P0462' 'P1217']. The Check also tries two lists with nothing in common (on_both is then an empty array) and two lists with the same ids (on_one is then empty).

  4. Task 4 · interview question

    Interview: weakest.py, sorting the energies alone

    weakest_panels(energy, ids, k) in weakest.py gets every panel's day energy in kWh, shape (2400,), and the panel ids in the same order (ids[i] made energy[i]). It must return a tuple of two arrays, (ids, kwh): the ids of the k panels that made the least energy, least first, and their energies in the same order. weakest_panels(energy, ids, 10) is a tuple of an array of 10 id strings and an array of 10 floats.

    Its energies come back right, but Bhanwar walked to P0001, P0002 and P0003 and found nothing wrong. Fix it so that every id is the panel that made the energy beside it. Panels with the same energy (the three dead panels make 0 kWh) may come in any order. The Check uses the whole day with k of 10 and of 1, and 6 panels with k of 2.

    Imran will ask: why was sorting the energies on their own a bug, and what keeps each id with its energy?

What you will learn

  • np.sort and a.sort(): a sorted copy, or the array sorted in place, along any axis.
  • np.argsort: the positions that would sort an array, to put other arrays in the same order.
  • Descending order: reverse with [::-1].
  • np.partition and np.argpartition: the top or bottom few, without sorting everything.
  • np.searchsorted and np.digitize: where values fall among sorted edges.
  • np.unique: each value once, with counts, first positions and the inverse.
  • np.isin and the set functions: np.intersect1d, np.union1d, np.setdiff1d and np.setxor1d.

More in Analysis

Build "Sorting and unique values" yourself

DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of NumPy are free. This one comes with the all labs plan.