欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[高级编程技术作业-Week 13]Scipy相关习题练习

程序员文章站 2022-07-12 22:20:01
...

[高级编程技术作业-Week 13]Scipy相关习题练习

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

[高级编程技术作业-Week 13]Scipy相关习题练习

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)的最小值的相反数。


[高级编程技术作业-Week 13]Scipy相关习题练习

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)

相关标签: Python Scipy