Short Exercises #5

Due: Wednesday, Nov 10 at 4:30pm CST

The following short exercises are intended to help you practice some of the programming concepts introduced in week 6. 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 (particularly the Numpy Reference) 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()

Fetching the instructor files

To get the files for this set of short exercises, first set the GITHUB_USERNAME environment variable by running the following command at the Linux command line (replacing replace_me with your GitHub username):

GITHUB_USERNAME=replace_me

(remember you can double-check whether the variable is properly set by running echo $GITHUB_USERNAME)

Then navigate to your Short Exercises repository and pull the new material:

cd ~/cmsc12100
cd short-exercises-$GITHUB_USERNAME
git pull upstream main

You will find the files you need in the se5 directory.

IMPORTANT: If you are unable to obtain the instructor files by running the commands above do not try to add the files in some other way. Doing so will likely prevent you from submitting your code. Instead, please seek assistance on Ed Discussion or at office hours.

Testing

As usual, you will want to test your solution manually before you try the automated tests. Remember to set up autoreload before you start testing.

$ ipython3

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: import se5

Exercises

Arrays

  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]).

  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]).

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 and the value in an one-dimensional array that is closest to the mean of the array. For example, find_closest_value(np.array([1.0, 2.0, 3.0])) would return (1, 2.0) and find_closest_value(np.array([5.0, 1.0, 8.0])) would return (0, 5.0).

  2. Complete the function select_row_col(x, row_idx, col_idx) that takes in a 2-dimensional array x and returns a subset of rows or columns or sub-array specified by row_idx and col_idx. If you specify row_idx as a list and col_idx as None, you will return a subset of rows. Similarly, if you specify row_idx as None and col_idx as a list, you will return a subset of columns. If you specify row_idx as a list and col_idx as a list, you will return a sub-array specified by the given rows and columns. If you specify both row_idx and col_idx as None, you will return the array itself. For example,

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

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

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

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

Submitting your work

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). Gradescope will fetch your files directly from your GitHub repository, so it is important that you remember to commit and push your work!

To submit your work, go to the “Gradescope” section on our Canvas site. Then, click on “Short Exercises #5”. Then, under “Repository”, make sure to select your uchicago-cmsc12100-aut-21/short-exercises-$GITHUB_USERNAME.git repository. Under “Branch”, just select “main”.

Finally, click on “Upload”. An autograder will run, and will report back a score. Please note that this autograder runs the exact same tests (and the exact same grading script) described in Testing Your Code. If there is a discrepancy between the tests when you run them on your computer, and when you submit your code to Gradescope, please let us know.

Your ESNU score on this set of exercises will be determined solely on the basis of these automated tests:

Grade

Percent tests passed

Exemplary

at least 95%

Satisfactory

at least 75%

Needs Improvement

at least 50%

Ungradable

less than 50%

If there is a discrepancy between the tests when you run them on your computer, and when you submit your code to Gradescope, please let us know. Please remember that you can submit as many times as you want before the deadline. We will only look at your last submission, and the number of submissions you make has no bearing on your score.