第十三周作业


10.1

代码:

import numpy as np
import scipy.optimize as opt

A = np.random.randn(5, 4)
b = np.random.randn(5, 1)
x = np.random.randn(4, 1)
print('The matrix A:\n{}'.format(A))
print('The vector b:\n{}'.format(b))
def fun(x, A, b):
	x = x.reshape(4, 1)
	return (np.dot(A, x) - b).reshape(-1)

x = opt.leastsq(fun, x, args = (A, b))[0].reshape(4,1)

print ('The vector x after leastsq:\n{}'.format(x))
print('The norm of the residual:\n{}'.format(np.linalg.norm(np.dot(A, x)-b)))

结果:

The matrix A:
[[ 1.91526735 -0.07920178  0.91062286 -0.79701241]
 [ 0.93881208 -0.23656367 -1.03735791 -0.33011667]
 [-0.16269345  0.98403367 -0.41020912  1.65456638]
 [-1.08385968  2.78313116 -0.15163233 -0.37604114]
 [ 0.14602272  0.36011239  0.69340755  0.57077108]]
The vector b:
[[-0.9501774 ]
 [-0.23918005]
 [-1.424578  ]
 [ 0.78009385]
 [-0.74941098]]
The vector x after leastsq:
[[-0.78923482]
 [-0.15455917]
 [-0.15427191]
 [-0.87788511]]
The norm of the residual:
0.036695178952734456

矩阵A和向量b都服从标准正态分布,x无所谓,最后在定义了残差,调用leastsq函数解出x后,用norm函数计算残差的范数。

10.2

代码:

import numpy as np

x = np.linspace(-1.2, 1.2, 1e5)
y = (np.sin(x-2)**2)*np.exp(-x**2)
print('When x is {},the maximum of the function is {}'.format(x[np.argmax(y)], np.max(y)))

结果:

When x is 0.21623016230162317,the maximum of the function is 0.9116854116143976

通过分析确定该函数的最大值区间所在,然后用这区间里尽可能多的数代入函数求出其中的最大值。

10.3

代码:

from scipy.spatial.distance import pdist, squareform
import numpy as np

X = np.random.randn(5, 10)
print ('The matrix X is:\n{}'.format(X))
Y = squareform(pdist(X))
print('The distance matrix,dist(u=X[i],v=X[j]) = entry[i,j]:\n')
print(Y)

结果:
The matrix X is:
[[-0.27623312  0.44151323  0.5297747  -0.52848283  1.47840962 -1.12377143
   0.12190629  0.02163978  0.44171134  0.53210091]
 [ 0.75063198  0.77780588  0.31744836 -0.10536899  0.87808988 -0.43679796
  -0.18216892  0.24204502 -0.30046521  1.22432349]
 [-0.6351976   0.05878036  0.87457556 -1.12642862  1.02035373 -1.1705866
   0.81724874 -0.96268763 -0.30462207 -0.27091757]
 [ 1.33824726  1.19081278  0.99236552 -0.79829027 -0.89773403  0.8479355
   0.50667326  0.57090731  1.30828918  0.66477409]
 [-0.66684725 -0.83517034  0.41395524 -2.0757017   0.71551395  1.55428593
  -0.44566853 -0.11823455 -0.18592841  0.23501001]]
The distance matrix,dist(u=X[i],v=X[j]) = entry[i,j]:

[[0.         1.84255451 1.90211131 3.76918687 3.57319219]
 [1.84255451 0.         3.0058508  3.12080924 3.69924363]
 [1.90211131 3.0058508  0.         4.35619048 3.4654595 ]
 [3.76918687 3.12080924 4.35619048 0.         4.12214808]
 [3.57319219 3.69924363 3.4654595  4.12214808 0.        ]]

使用scipy的内置函数pdist即可计算pairwise distance,其中关于这个函数有这样的说明

Pairwise distances between observations in n-dimensional space.

See Notes for common calling conventions.

很明显这就是题目要求的函数,另外这个函数返回的结果是一维的即(n-1)!个距离,为了美观我使用了函数squareform把它变成对称矩阵。函数文档地址:

点击打开链接

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值