Exercise
Exercise¶
How do you import numpy?
Name three different options to create a numpy array.
How do you get the shape of an array?
How do you get the data type of an array?
What types can the elements of an numpy array have? Name four different ones.
What type will have the arrays created from the following lists?
l1 = [1, 2, 3] l2 = [1., 2, 3] l3 = [1., 2, 3 + 0j]
What is the fundamental difference between a slice of a numpy array and a list?
What is the meaning of
...
in an index tuple?
Over which axis are multi-dimensional arrays iterated?
Which shape does the following array has:
a = np.arange(3 * 4 * 10).reshape(2, -1, 5)
Test the performance difference between numpy and pure Python. Compute the square of 1000 numbers using both a list and a numpy array. You can measure the time for the computation by adding the cell magic
%%timeit
as the first line of the cell. Use one cell for each case. Note that%%timeit
runs a lot of repetitions to get a more robust estimate of the run time.
Create an evenly spaced 1D array ranging from -π to π in steps of 0.1. Now create an array covering the same range but having exactly 100 elements. Note that there is a numpy constant (
np.pi
) for π.
Compute the mean, variance and standard deviation of one of the arrays created above.
Write a function that computes the angle in radians between two n-dimensional vectors. Use as many numpy functions as possible. The cosine of the angle between two vectors is given by
Extend the function above to take two multi-dimensional arrays as input. Interpret, by default, the data along the last axis as the vectors. However, also try to give the possibility to use another axis.
How can you deal with missing data in numpy?
How do you mask and unmask data?
Draw ten random numbers which are uniformly distributed between zero and one.
Repeat intermediate level exercise 4 of the basic exercises but without using
Turtle
. First code a single canon ball shot. Then try to do all possible combinations of initial speed and angle within a single loop. Exploit numpys broadcasting capabilities!