Learning scipy
Exercise 10.1: Least squares
Generate matrix A ∈ $R_{m×n}$ with m > n. Also generate some vector b ∈ $R_m$ .
Now find $x=arg \min_x||Ax-b||_2$.
Print the norm of the residual.
Code
1 | import numpy as np |
Result
1 | x = [[ 0.29354894] |
Exercise 10.2: Optimization
Find the maximum of the function
$$ f(x) = sin^2(x-2)e^{-x^2}$$
Code
1 | import numpy as np |
Result
1 | Optimization terminated successfully. |
Exercise 10.3: Pairwise distances
Let X be a matrix with n rows and m columns. How can you compute the pairwise distances between every two rows?
As an example application, consider n cities, and we are given their coordinates in two columns. Now we want a nice table that tells us for each two cities, how far they are apart.
Again, make sure you make use of Scipy’s functionality instead of writing your own routine.
Code
1 | import numpy as np |
Result
1 | 4 Cities: |