Exercise 10.1: Least squares
Generate matrix A ∈ Rm×n with m > n. Also generate some vector b ∈ R^m.
Now find x = arg minx ||Ax − b||2.
Print the norm of the residual.
from scipy.optimize import least_squares # 引入最小二乘函数
import numpy as np
m = 20
n = 5
A = np.random.rand(m, n)
b = np.random.rand(m, 1)
x = np.zeros(n)
def func(x):
return np.linalg.norm(A.dot(x) - b)
sol = least_squares(func, x)
print(sol)
print("argmin x :", sol.x)
print(" residual:", func(sol.x))
输出:
active_mask: array([0., 0., 0., 0., 0.])
cost: 18.548562450660654
fun: array([6.09074092])
grad: array([-0.00043129, 0.00825182, 0.0032902 , -0.00024505, 0.0037066 ])
jac: array([[-7.08103180e-05, 1.35481358e-03, 5.40196896e-04,
-4.02331352e-05, 6.08563423e-04]])
message: '`ftol` termination condition is satisfied.'
nfev: 117
njev: 113
optimality: 0.0082518184788379
status: 2
success: True
x: array([ 0.12208824, -0.06974677, 0.27798636, 0.32015007, 0.30762308])
argmin x : [ 0.12208824 -0.06974677 0.27798636 0.32015007 0.30762308]
residual: 6.090740915629338
Exercise 10.2: Optimization
Find the maximum of the function
import math
from scipy.optimize import fmin
def func(x):
return ((math.sin(x - 2))**2) * math.exp(-(x**2))
result = fmin(func, 0)
print(result)
输出:
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 25
Function evaluations: 50
[-1.1415625]
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.
import numpy as np
from scipy.spatial import distance
# http://www.it1352.com/234917.html
m = 20
n = 5
X = np.random.rand(m, n)
dists = distance.pdist(X)
print(dists)
输出:
[1.02397042 0.66198184 0.68270417 0.95980503 0.94876295 1.28910145
0.99786154 1.19195332 0.68494725 0.59830461 0.58438626 1.00953439
1.1914291 1.0226026 1.00228754 0.8516124 1.19131237 0.69077538
0.719278 0.95692451 0.95817779 0.39873196 0.64805639 0.71334487
1.19141197 0.69623684 0.76592437 0.74263162 0.81217497 0.57775737
0.56039256 0.61688712 0.93791503 0.67362314 0.6467085 0.83132238
0.95854108 0.91257872 1.12184453 1.11758816 1.29202565 1.3037946
1.28226153 0.82495697 0.43554784 0.92698746 1.013793 1.21732681
1.13076661 1.09218133 1.10996813 1.35308725 0.82482311 0.77525791
1.01756844 0.89849962 1.14664437 0.57192909 1.09120386 0.63602684
0.5599489 0.78490047 0.92842496 1.0890859 1.08613869 1.03776629
0.59579914 1.08663416 0.17079976 0.84733913 0.59838276 0.75257815
1.16063814 0.64251211 0.7746636 0.92077766 0.65463017 0.69414432
0.58625837 0.52743924 0.9424456 0.60484429 0.56911501 0.93706529
0.99891599 1.01113665 1.07217574 0.84167062 0.78272859 0.84031636
0.91258276 1.02240877 1.04741499 1.06635777 0.43623997 0.56796122
0.3607233 0.79260387 0.64134569 1.029979 0.24457069 0.69491635
1.15515043 0.8260265 1.07687535 0.47372058 0.8340751 1.19018361
0.71294476 1.07642665 1.0829414 1.19344022 0.95507728 0.64252197
1.03617533 0.83883661 1.28503487 1.13280558 1.25331462 1.1769534
0.58440816 1.26014575 0.66493128 1.05428955 0.61345158 1.1251426
0.74108101 1.10167927 0.5731328 0.85382408 1.01816151 0.57873184
0.93051386 1.03131713 1.05847336 0.68455967 0.46310971 1.01288352
0.81169475 0.90925392 0.8417417 0.45930051 1.03363045 0.59979694
0.65918649 0.82508695 0.77421334 1.05611904 0.99491226 0.9204424
0.80624712 1.04649568 0.43769581 0.67649178 0.88116259 0.73154967
0.64295929 1.08572208 0.59676836 1.0783366 0.79180808 0.92484832
0.69486641 0.49884102 1.35050115 0.90339827 0.97047237 0.85480938
1.2740189 0.41498608 1.33672543 0.75396756 1.02861397 1.03091838
1.30615074 1.38120514 0.85513885 1.03696625 1.04190856 1.31548767
0.82111819 0.7661898 0.93336994 0.39533012 0.71387853 0.56117792
0.81520453 0.98433292 0.99389997 0.75721045]