Solutions are available in the /code directory in our .zip.
- Last week we calculated a list containing ex x [0.0,0.1,,1.0], using range, lambda, map and math.exp. Now, lets do the Numpy way, using np.linspace and np.exp. Solution: exp_np.py.
- In the ECG (heartbeat) example, we saw how to use a Boolean expression to give a new array telling us where the Boolean expression is true:
x = np.array([3, -2, -1, 0, 1, 2, 3]) x > 0# array([False, False, False, False, True, True, True]) |
We can also create a new array consisting of the values where it is true:
x = np.array([3, -2, -1, 0, 1, 2, 3]) x[x > 0]
# array([1, 2, 3])
We can use the same idea now on the left-hand side of an assignment, to overwrite only values in an array where the Boolean expression is true. Use this to set all negative values in x to zero. Solution: numpy_boolean_lhs.py.
- Find the biggest jump between any two consecutive values in the heartbeat (ECG) data. When does it occur? What were the before and after values? Hint: recall we have seen np.diff and np.argmax. I suggest np.diff(x, prepend=x[0]) this time instead of prepend=0. Solution: heartbeat_jump.py.
- Use Numpy to create a chessboard pattern like this:
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
- 1 0 1 0 1 0 1]
- 0 1 0 1 0 1 0]
- 1 0 1 0 1 0 1]
- 0 1 0 1 0 1 0]
- 1 0 1 0 1 0 1]
- 0 1 0 1 0 1 0]]
Hints: use np.linspace, np.meshgrid, and the % operator. For the final touch, look up astype so that your array is int-valued, not float. Solution: chessboard.py.
1
- Create the 2d array aij, a vertical 1d array bi, a horizontal 1d array cj, and a scalar d. Calculate the new 2d array q where qij = aij +bi +cj +d. (This scenario will be familiar to CT5141 Optimisation students as it occurs in linear objective functions.) Solution: 2d_1d_scalar.py.
a: 9 5 | 1 | b: 10 | c: 100 200 300 | d: 1000 | q: 1119 1215 1311 |
4 3 | 8 | 20 | 1124 1223 1328 | ||
2 7 | 6 | 30 | 1132 1237 1336 |
- In our fractals example, we claimed that when a point escapes from a circle of radius 2, it will never come back, but in test_escape_and_return(), thats exactly what seemed to happen. Why? Solution: fractal_escape_and_return.py.
- Try different values for zmin and zmax in the Julia example. What is the effect?
- Try different values for c. Do you get any interesting images? I liked c = -0.015 + 0.66j.
- Try different colourmaps in the Julia set. Look up:
https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
to see a gallery.
2
Reviews
There are no reviews yet.