Short Exercises #5

Due: Sunday, November 15 at 3pm CDT

The following short exercises are intended to help you practice some of the programming concepts introduced in week 7. These exercises should not take more than 1-2 hours in total to complete. The goal of these exercises is to help you develop skills with the NumPy library and array concepts. To that end, you might find the NumPy documentation helpful.

Many of the exercises below can be completed using for and while loops, but the purpose of these exercises is to learn how to use the more efficient NumPy functions. Therefore do not use ``for`` or ``while`` loops when completing these exercises. You must also not use list or dictionary comprehensions.

NumPy is a rich library and we do not have time in class to cover all of its functionality in recorded lectures, so we recommend you check out the documentation for the following functions and array methods which may be of help when completing these short exercises:

Functions:

  1. np.argwhere

  2. np.argmin

  3. np.arange

Methods (assuming an array x):

  1. x.mean()

  2. x.std()

  3. x.flatten()

  4. x.copy()

Exercises

NumPy versions of functions from SE2

  1. Complete the function compute_matching, which takes two lists of equal length and returns a list of the same length where the ith element is True if the ith elements of the two lists are equal. For example, given the arrays np.array([10, 20, 30]) and np.array([10, 30, 30]), the function would return np.array([True, False, True]). Note that this is the same behavior as compute_matching from Short Exercises 2 but this time uses NumPy arrays and functions.

  2. Complete the function compute_matching_indices, which takes two arrays of equal length and returns an array of the indices where the elements of the two arrays are equal. For example, given the arrays np.array([10, 20, 30]) and np.array([10, 30, 30]), the function would return np.array([0, 2]). Note that this is the same behavior as compute_matching_indices from Short Exercises 2 but this time uses NumPy arrays and functions.

Arrays and scalars

  1. Complete the function powers(N, p), which computes the first N powers of p. For example, powers(5,2) would return the array np.array(1, 2, 4, 8, 16).

Masking array values

  1. Complete the function clip_values, which takes in an n-dimensional array and returns a new array with its values clipped between min_val and max_val. For example, clip_values(np.array([1, 2, 3]), min_val=2) would return np.array([2, 2, 3] and clip_values(np.array([1, 2, 3]), max_val=2) would return np.array([1, 2, 2]. Remember to return a new array and to not modify the input array.

Indexing

  1. Complete the function find_closest_value which will find the entry in an array that is closest to a target value tgt_value. For example, find_closest_value(np.array([1.0, 1.5, 2.0]), 1.7) would return 1.5 and find_closest_value(np.array([1.0, 1.5, 2.0]), 1.8) would return 2.0. Note this function returns a scalar, not a NumPy array.

  2. Complete the function select_row_col(x, is_row, tgt) that takes in a 2-dimensional array x and returns a subset of either rows or columns as specified by tgt. If is_row=True, tgt is a list of rows to return; if is-row=False, tgt is a list of columns. For example,

In [1]: x = np.array([[0, 1, 2],
                      [3, 4, 5],
                      [6, 7, 8]])

In [2]: se5.select_row_col(x, True, [1, 2])
Out[2]:
array([[3, 4, 5],
       [6, 7, 8]])

In [3]: se5.select_row_col(x, False, [1, 2])
Out[3]:
array([[1, 2],
       [4, 5],
       [7, 8]])

Testing and Submitting your Solutions

Like the previous short exercises, you will need to pull some instructor files to your repository, and then add your code to one of those files. You can find detailed instructions on how to do this in our Coursework Basics page. For instructions on how to test your code, please see our Testing Your Code

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). In the “Short Exercises #5” assignment, simply upload file se5.py (do not upload any other file!). Please note:

  • You are allowed to make as many submissions as you want before the deadline.

  • There are no extensions for the short exercises. The two free extensions you get for the programming assignments cannot be applied towards the short exercises. Please note that, if you need an extension due to extraordinary circumstances, you should alert us via a private message on Piazza.

  • Your score on the short exercises is determined solely based on the automated tests, but we may adjust your score if you attempt to pass tests by rote (e.g., by writing code that hard-codes the expected output for each possible test input).

  • Gradescope will report the test score it obtains when running your code. If there is a discrepancy between the score you get when running our grader script, and the score reported by Gradescope, please let us know so we can take a look at it.