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:
np.argwhere
np.argmin
np.arange
Methods (assuming an array x):
x.mean()
x.std()
x.flatten()
x.copy()
Exercises¶
NumPy versions of functions from SE2¶
Complete the function
compute_matching
, which takes two lists of equal length and returns a list of the same length where the ith element isTrue
if the ith elements of the two lists are equal. For example, given the arraysnp.array([10, 20, 30])
andnp.array([10, 30, 30])
, the function would returnnp.array([True, False, True])
. Note that this is the same behavior ascompute_matching
from Short Exercises 2 but this time uses NumPy arrays and functions.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 arraysnp.array([10, 20, 30])
andnp.array([10, 30, 30])
, the function would returnnp.array([0, 2])
. Note that this is the same behavior ascompute_matching_indices
from Short Exercises 2 but this time uses NumPy arrays and functions.
Arrays and scalars¶
Complete the function
powers(N, p)
, which computes the firstN
powers ofp
. For example,powers(5,2)
would return the arraynp.array(1, 2, 4, 8, 16)
.
Masking array values¶
Complete the function
clip_values
, which takes in an n-dimensional array and returns a new array with its values clipped betweenmin_val
andmax_val
. For example,clip_values(np.array([1, 2, 3]), min_val=2)
would returnnp.array([2, 2, 3]
andclip_values(np.array([1, 2, 3]), max_val=2)
would returnnp.array([1, 2, 2]
. Remember to return a new array and to not modify the input array.
Indexing¶
Complete the function
find_closest_value
which will find the entry in an array that is closest to a target valuetgt_value
. For example,find_closest_value(np.array([1.0, 1.5, 2.0]), 1.7)
would return1.5
andfind_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.Complete the function
select_row_col(x, is_row, tgt)
that takes in a 2-dimensional arrayx
and returns a subset of either rows or columns as specified bytgt
. Ifis_row=True
,tgt
is a list of rows to return; ifis-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.