Scipy库习题

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
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import scipy.linalg

m = 20
n = 10
A = np.random.rand(m, n)
b = np.random.rand(m).reshape(m, 1)

x = scipy.linalg.lstsq(A, b)[0]

print("x = "+str(x))
err = A.dot(x) - b
residual = scipy.linalg.norm(err,ord=2)
print("norm of the residual: "+str(residual))

Result

1
2
3
4
5
6
7
8
9
10
11
x = [[ 0.29354894]
[ 0.3171796 ]
[ 0.3076955 ]
[ 0.16784761]
[ 0.37531644]
[-0.12455975]
[ 0.24540134]
[ 0.17275431]
[-0.28511405]
[-0.52271425]]
norm of the residual: 0.7740732857559364

Exercise 10.2: Optimization

Find the maximum of the function
$$ f(x) = sin^2(x-2)e^{-x^2}$$

Code

1
2
3
4
5
6
7
8
import numpy as np
import scipy.linalg
import scipy.optimize

f = lambda x: -scipy.sin(x-2)**2*(x-2)*scipy.exp(-(x*x))
# 由于只有取最小值的方法,因此要取反函数并对答案取反
res = scipy.optimize.fmin(f, scipy.rand(), full_output=True)
print("Max of the func: "+str(-res[1]))

Result

1
2
3
4
5
Optimization terminated successfully.
Current function value: -0.000225
Iterations: 20
Function evaluations: 40
Max of the func: 0.0002253477813642845

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
2
3
4
5
6
7
8
9
10
11
import numpy as np
import scipy.spatial.distance

m, n = 4, 3
# 4 cities, x,y,z to discribe a city
A = np.random.rand(m,n)
print("4 Cities: ")
print(A)
D = scipy.spatial.distance.pdist(A)
print("Distance between each 2 cities: ")
print(D)

Result

1
2
3
4
5
6
7
4 Cities:
[[0.42422873 0.17197828 0.82144308]
[0.15882959 0.42992466 0.80128027]
[0.7649197 0.21990926 0.38651754]
[0.75747478 0.15525905 0.16286141]]
Distance between each 2 cities:
[0.37064751 0.55455201 0.73828332 0.7638585 0.91727634 0.23293162]