Day 8 exercises¶
Solutions at Day 8 exercise solutions.
Simple arrays¶
5 minutes.
Create this array:
2 7 12 0
3 9 3 4
4 0 1 3
What is the array shape
?
What is the array ndim
?
How about the len
of the array?
Can you get the ndim
and len
from the shape?
Creating arrays using functions¶
10 minutes.
Create a 1D array from 2 through 5 inclusive.
Make an array with 10 elements between 2 and 5 inclusive.
Make an all-ones array shape (4, 4).
Make an identity array shape (6, 6).
Make this array with a single Python / numpy command:
1 0 0 0 2 0 0 0 3
Look at the docstring for np.random.randn
. Make a shape (3, 5) array with random numbers from a mean=0, variance = 1 normal distribution.
Simple visualizations¶
7 minutes.
- Make an array
x
with 100 evenly spaced values between 0 and 2 * pi; - Make an array
y
which contains the cosine of the corresponding value inx
- soy[i] = cos(x[i])
(hint:np.lookfor('cosine')
). - Plot
x
againsty
; - Make a 10 by 20 array of mean 0 variance 1 normal random numbers;
- Display this array as an image;
- Investigate
plt.cm
. See if you can work out how to make the displayed image be grayscale instead of color.
Indexing and slicing, array creation¶
10 minutes.
Create the following array, call this
a
(you did this before):2 7 12 0 3 9 3 4 4 0 1 3
Get the 2nd row of
a
([ 3 9 3 4]
);Get the 3rd column of
a
([12 3 1]
);Create the following arrays (with correct data types):
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 2], [1, 6, 1, 1]] [[0., 0., 0., 0., 0.], [2., 0., 0., 0., 0.], [0., 3., 0., 0., 0.], [0., 0., 4., 0., 0.], [0., 0., 0., 5., 0.], [0., 0., 0., 0., 6.]]
Par on course: 3 statements for each
Hint: Individual array elements can be accessed similarly to a list, e.g.
a[1]
ora[1, 2]
.Hint: Examine the docstring for
diag
.Skim through the documentation for
np.tile
, and use this function to construct the array:[[4, 3, 4, 3, 4, 3], [2, 1, 2, 1, 2, 1], [4, 3, 4, 3, 4, 3], [2, 1, 2, 1, 2, 1]]
Fancy indexing using boolean arrays¶
5 minutes.
Create the following array
a
(same as before):2 7 12 0 3 9 3 4 4 0 1 3
Use
>
to make a mask that is true where the elements are greater than 5, like this:False True True False False True False False False False False False
Return all the elements in
a
that are greater than 5.Set all the elements greater than 5 to be equal to 5, to get this:
2 5 5 0 3 5 3 4 4 0 1 3
Elementwise operations¶
10 minutes.
Remember our array a
:
2 7 12 0
3 9 3 4
4 0 1 3
Use array slicing to get a new array composed of the even columns (0, 2) of
a
. Now get array that contains the odd columns (1, 3) ofa
. Add these two arrays.Generate this array:
[2**0, 2**1, 2**2, 2**3, 2**4]
Generate an array length 10 such that this is true of the elements (where
x[i]
is the element ofx
at indexi
):x[i] = 2 ** (3 * i) - i
Summary functions¶
Remember our array a
:
2 7 12 0
3 9 3 4
4 0 1 3
What are the:
- sum of all the values?
- sum of the columns?
- sum of the rows?
- mean?
- min?
- max?