The main goals for this lab are:
Make sure to write down your answers below. In future labs, the TAs or Instructor will check your answers. Whenever you see TODO, that means there is an action item for you to complete.
From the terminal, you can start a python interpreter by just typing python
in the command-line. Once you create a python, you can run it from terminal by just running python <filename>.py
.
If you are new to python, go through at least the following sections of this Python tutorial:
Some steps below are borrowed from numpy quickstart tutorial, which I encourage you to reference throughout the course.
During this course we will often use numpy to manage our datasets. Type the following commands in the python interpreter (filling in missing pieces) and make sure the output makes sense:
python
>>> import numpy as np
>>>
>>> # create a 4x5 array of zeros
>>> m = np.zeros([4,5])
>>> m
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
>>>
>>> # create a 3x5 array of zeros using reshape
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>>
>>> # create an array from a list
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
TODO and DISCUSS
.shape
attribute to find the shape of m
, a
, and b
abovem
using assignment (similar to lists, i.e. m[2][1]
)m
? Try out a few of these to see how the entries behave.-1
is used as part of the shape? For multi-dimensional arrays, how many dimensions can be specified with -1
.
Switch who is navigator and driver
Try out the slicing operations below, predicting what will be printed each time. Make sure you have modified the m
array enough that output is clear (you may need to recreate m
if you switched who was sharing the screen).
>>> m[2]
>>> m[:,1]
>>> m[:3,:2]
TODO and DISCUSS
concatenate
function and apply it to your m
array and a
array to produce an 7x5 array.
Look up the sort
function (from numpy) and apply it to both axes of your m
array (as well as axis=None
) and make sure the output makes sense.optional: save your interpreter commands to a file and email to each partner
Complete the following math pre-exam. This will be graded on effort, not correctness - the purpose of this part of Lab 1 is so that I know how much math review to do later on. You have a few options for working on this part:
This part of the lab should be submitted individually.
For the remaining labs, before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go.