\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)
Basic numpy exercises¶
- For code template see:
lab_01_code.py
; - For solution see: Basic numpy exercises.
Simple arrays¶
Create an array with variable name a
and the following contents (shape (3,
4)):
2 7 12 0
3 9 3 4
4 0 1 3
>>> #- create array "a" with values
>>> #- 2 7 12 0
>>> #- 3 9 3 4
>>> #- 4 0 1 3
What is the array shape
?
>>> #- Array shape?
What is the array ndim
?
>>> #- Array ndim?
How about the len
of the array?
>>> #- Array length
Can you get the ndim
and len
from the shape?
>>> #- Get ndim and length from the shape
Creating arrays using functions¶
- Create a 1D array from 2 through 5 inclusive.
>>> #- 1D array 2 through 5
- Make an array with 10 equally spaced elements between 2 and 5 inclusive.
>>> #- 10 equally spaced elementd between 2 and 5
- Make an all-ones array shape (4, 4).
>>> #- Shape 4,4 array of 1
- Make an identity array shape (6, 6).
>>> #- Identity array shape 6, 6
Make this array with a single Python / numpy command:
1 0 0 0 2 0 0 0 3
>>> #- Array with top left value == 1 etc
Look at the docstring for np.random.randn
. Make a shape (3, 5) array with
random numbers from a standard normal distribution (a normal distribution with
mean 0 and variance 1).
>>> #- Array of random numbers shape 3, 5
Simple visualizations¶
- Make an array
x
with 100 evenly spaced values between 0 and 2 * pi;
>>> #- x is an array with 100 evenly spaced numbers 0 - 2 pi
- Make an array
y
which contains the cosine of the corresponding value inx
- soy[i] = cos(x[i])
(hint:np.lookfor('cosine')
).
>>> #- y has cosines of values in x
- Plot
x
againsty
;
>>> #- plot x against y
- Make a 10 by 20 array of mean 0 variance 1 normal random numbers;
>>> #- Shape 10, 20 array of random numbers
- Display this array as an image;
>>> #- Display as image
- Investigate
plt.cm
. See if you can work out how to make the displayed image be grayscale instead of color.
>>> #- Grayscale image of array
Indexing and slicing, array creation¶
See discussion at Index ordering and reshape in NumPy and MATLAB.
Create the following array, call this
a
(you did this before):2 7 12 0 3 9 3 4 4 0 1 3
>>> #- Create array "a"
- Get the 2nd row of
a
([ 3 9 3 4]
);
>>> #- 2nd row of a
- Get the 3rd column of
a
([12 3 1]
);
>>> #- 3rd column of a
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
.
>>> #- Build given arrays
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]]
>>> #- Use np.tile to construct array
Fancy indexing using boolean arrays¶
Create the following array
a
(same as before):2 7 12 0 3 9 3 4 4 0 1 3
>>> #- Create array a
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
>>> #- Make mask for values greater than 5
- Return all the elements in
a
that are greater than 5.
>>> #- Return all values 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
>>> #- Set all elements greater than 5 to equal 5
Elementwise operations¶
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.
>>> #- Add even and odd columns of a
Generate this array:
[2**0, 2**1, 2**2, 2**3, 2**4]
>>> #- Generate array of powers of 2
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
>>> #- Generate array
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 values in a
- sum of the columns?
>>> #- Sum of the values of the columns in a
- sum of the rows?
>>> #- Sum of the values of the rows in a
- mean?
>>> #- Mean of all the values in a
- min?
>>> #- Minimum of all the values in a
- max?
>>> #- Maximum of all the values in a