第十三周作业(scipy练习)

Exercise 10.1: Least Squares 题目描述 第十三周作业(scipy练习)
文章图片

代码:

import numpy as np m = 20 n = 10 A = np.random.randint(0, 10, (m,n)) B = np.random.randint(0, 10, (m,1))res = np.linalg.lstsq(A, B, rcond=0) # x print(res[0]) # residual print(res[1])

Exercise 10.2:Optimization 题目描述 第十三周作业(scipy练习)
文章图片

代码:
import numpy as np from scipy.optimize import minimizedef func(x): return np.sin(x-2)**2*np.exp((-1)*x**2)*(-1)res = minimize(func,0) print(-res.fun)

Exercise 10.3:Pairwise distances 题目描述 【第十三周作业(scipy练习)】第十三周作业(scipy练习)
文章图片

代码:
import numpy as np from scipy.spatial.distance import pdistdef mnvector(): print("m x n matrix:") m = 10 n = 10 Matrix = np.random.randint(10,30,(m,n)) Distance = pdist(Matrix,'euclidean') print(Matrix) print(Distance) def ncities(): print("n cities:") m = 10 Matrix = np.random.randint(10,20,(m,3)) Distance = pdist(Matrix,'euclidean') print(Matrix) print(Distance)def main(): mnvector() ncities()if __name__ == '__main__': main()

    推荐阅读