Week 12: Numpy and Matplotlib

Warning

This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.

Week 12: Closure#

Syllabus#

  • NumPy, a library for numeric computing in Python, especially when handling large arrays and matrices. By examples, we cover:

    • NumPy array, an \(n\)-dimensional array

    • Compact syntax

    • Vectorized NumPy operators, for example + , *

    • NumPy functions for element-wise operations: numpy.sqrt() , numpy.exp() , numpy.sin() , numpy.abs()

    • Other functions and methods: numpy.mean() , numpy.std()

    • Indexing, slicing, and boolean indexing

    • NumPy arrays are mutable

    • Preallocation

  • Matplotlib, a library for scientific visualization in Python. By examples, we cover:

    • Line plots

    • Bar plots

    • Labels

    • Legends

    • Titles

    • Text

Checkpoints#

Checkpoint 12.1: Checkerboard Sum #

Given a 2D NumPy array, we want to compute the sum of all elements occurring in a checkerboard pattern of arbitrary size. The square in the first row and the first column is always black.

Write a function which takes as input a 2D NumPy array. The function should return the sum of all elements in the black squares of the checkerboard pattern.

Consider the 2D NumPy array below.

import numpy as np
A = np.array([[ 1.42,  4.0,  55.56, 63.0],
              [ 2.22,  2.22, 33.73, 40.11],
              [12.1,  17.24, 18.0,  33.5],
              [21.15, 14.76, 17.3,  22.1],
              [ 5.34,  6.0,   9.8,   8.18]])

Arranged in a checkerboard pattern the array looks like this:

image

The sum of all elements occurring in a checkerboard pattern on the black squares is

\[ 1.42 + 55.56 + 2.22 + 40.11 + 12.1 + 18.0 + 14.76 + 22.1 + 5.34 + 9.8 = 181.41 \]

and this is what your function should return, as seen below.

>>> checkerboard_sum(A)
np.float64(181.41)

The filename and requirements are in the box below:

checkerboard_sum.py

checkerboard_sum(A)

Return checkerboard sum.

Parameters:

  • A

numpy.ndarray

A 2D NumPy array.

Returns:

  • float

The sum of elements in checkerboard pattern.

Use the following script to check your function test_checkerboard_sum.py. If your function fails the test in this script, it will also fail when you hand it in.

Checkpoint 12.2: Robust Values #

Given a NumPy array of numbers, we want find the values which are not more than one standard deviation away from the mean.

Given \(N\) numbers \(x_i\), the mean and the standard deviation are

\[ \mu = \frac{1}{N}\sum_{i=1}^N x_i \quad\quad \text{and} \quad\quad \sigma = \sqrt{\frac{1}{N}\sum_{i=1}^N (x_i - \mu)^2} \,\,. \]

The robust values (that we want to keep) are less than exactly one standard deviation away from the mean, i.e. a robust value \(x_i\) satisfies \(\mu -\sigma \leq x_i\) and \(x_i \leq \mu + \sigma\).

Write a function which takes as input a NumPy array. The function should return a NumPy array containing only the robust values in the same order as in the original array.

As an example, consider the input below.

>>> import numpy as np
>>> x = np.array([41.42, 44.32, 45.56, 63.01, 12.22, 42.82, 43.73, 40.11])

The mean of the numbers is \(\mu = 41.65\), and the standard deviation is \(\sigma = 13.00\) (all values are here displayed with two decimals). The robust values are in the interval \([28.64, 54.65]\), so only values \(63.01\) and \(12.22\) should be removed, as seen in the code cell below.

>>> robust_values(x)
array([41.42, 44.32, 45.56, 42.82, 43.73, 40.11])

The filename and requirements are in the box below:

robust_values.py

robust_values(x)

Return values within one standard deviation from the mean of the input.

Parameters:

  • x

numpy.ndarray

A NumPy array.

Returns:

  • numpy.ndarray

A NumPy array with robust values.

Use the following script to check your function test_robust_values.py. If your function fails the test in this script, it will also fail when you hand it in.