1.Generate matrix A belongs to Rm*n with m > n. Also generate some vector b belongs to Rm.
Now x = arg min ||Ax -b||2
Print the norm of the residual
m = 20
n = 10
A = np.random.rand(m,n)
b = np.random.rand(m)
x, residues, rank ,s= lin.lstsq(A,b)
print(x)
print(residues)
运行结果:
2.Find the maximum of the function
f(x) = sin2(x - 2)e^-x2
def fun(x):
return -(np.sin(x-2)**2)*np.e**(-x**2)
min=opt.fmin(fun,0)
print(min);
print(-fun(min))
运行结果3. 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.
n=2
m=2
X=np.random.random((n,n))
print(X)
print(sp.distance.cdist(X, X))
![](https://i-blog.csdnimg.cn/blog_migrate/85a2ffcbb932bbd1800b80c8873ad368.png)