scipy

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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值