[高级编程技术作业-Week 13]Scipy相关习题练习
程序员文章站
2022-07-12 22:20:01
...
Code:
#10.1
import numpy as np
import scipy.optimize as opt
m = 10
n = 5
A = np.random.random((m, n))
b = np.random.normal(size = m)
result = opt.lsq_linear(A, b)
print(result['x'])
Code:
#10.2
import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
def f(x):
return (-1) * (np.sin(x - 2))**2 * np.exp(-x*x)
result = opt.minimize_scalar(f)
print(-result['fun'])
Analysis:因为SciPy没有最大值函数,故将问题进行转换:要求f(x)的最大值,即求-f(x)的最小值的相反数。
Code:
#10.3
import numpy as np
import scipy.spatial.distance as dis
n = 10
m = 2
X = np.random.normal(size = (n, m))
result = dis.pdist(X)
print(result)
上一篇: 算法笔记 - 数学问题
下一篇: appium : 手机操作整理