Worksheet 2#
This worksheet is due Tuesday of Week 2, by 11:59pm.
You are encouraged to work in a group of up to 3 total students (working alone is also okay). Everyone should make a submission on Canvas, but it’s fine for all group members to submit the same thing.
See Worksheet 1 for instructions on how to make a Education workspace (which will provide higher-priority access to Deepnote resources).
Loading a NumPy array from a pickle file#
Import
numpy
using the usual abbreviationnp
, and also import the Pythonpickle
module (no abbreviation).Notice that there is an attached file
wkst2-starter.pickle
. Load the data from that file and store it in the variablearr
using the following code.
with open("wkst2-starter.pickle", "rb") as f:
arr = pickle.load(f)
Check the type of this object using the function
type
and check its dimensions by computing theshape
attribute (no parentheses aftershape
). The variablearr
should represent an 8000 by 7 NumPy array.
Practice with Boolean indexing#
Define the variable
x
to be the column at index4
.Check that
x
is a length-8000 NumPy array.How many values in
x
are equal to10
?Define
arrsub
to be the sub-array ofarr
containing all the rows for which the column at index4
is equal to the integer10
. Use Boolean indexing witharr
andx
.What is the shape of
arrsub
? Use theshape
attribute again. (It should be7
columns, but how many rows?)Define a new variable,
a
, to be equal to the number of rows ofarrsub
.
Counting rows satisfying a condition using NumPy#
How many rows in
arr
contain the number40
? You can usearr == 40
to create a Boolean array. From the resulting Boolean array, you can use thesum
method, in particular,.sum(axis=1)
, to count for each row, how many times40
appears. We want to find all the rows where40
occurs at least once. Once you have a length-8000 array ofTrue
andFalse
values (withTrue
if40
occurs in the row and withFalse
otherwise). You want to count the number of timesTrue
occurs. Save the result with the variable nameb
. (Reality check: the answer should be between 500 and 700.)
Submission#
Store these answers, as well as the names of everyone in your group, as a Python dictionary
ansdict = {"names": [name1, name2, name3], "a": a, "b": b}
The "names"
value must be a list, with length the number of group members, and with entries the full names of your group members. The names of your group members should be strings. For example, ["Chris Davis", "Jinghao Chen"]
.
Save the resulting dictionary in a pickle file using the following code.
with open("wkst2-ans.pickle", "wb") as f:
pickle.dump(ansdict, f)
Submit this pickle file on Canvas.