python homework——the 13th week

10.1
import numpy as np
import scipy.linalg as lin

A=np.random.randn(20,15)
b=np.random.randn(20)
c,re,rand,sigma=lin.lstsq(A,b)
b_=A.dot(c)

print(lin.norm(b-b_))

python homework——the 13th week

10.2
import numpy as np
import scipy.optimize as opt
def f(x):
 return -np.sin(x-2)**2 * np.e**(-x**2)
min_=opt.fmin(f,0)
print(-f(min_)[0])

python homework——the 13th week

10.3
import numpy as np
import scipy.spatial.distance as dis

X=np.random.randn(10,8)
print(dis.squareform(dis.pdist(X)))

position=np.random.randn(10,2)
print(dis.squareform(dis.pdist(position)))

python homework——the 13th week