高階程式設計技術 scipy課後習題

2021-08-20 11:09:34 字數 3264 閱讀 5030

步驟如下:

(1)m = int(input(" please input m: "))

n = int(input(" please input n: "))

if m < n:

m, n = n, m

因為題目沒有要求m和n的具體值,只要求m的值大於n的值,所以通過使用者互動來確定m和n的值。

(2)a = np.matrix(np.random.rand(m, n))

b = np.matrix(np.random.rand(m, 1))

因為題目沒有要求矩陣a和b的分布,所以使用隨機數中的均勻分布。

(3)x, residuals, rank, s  = scipy.linalg.lstsq(a, b)

norm = scipy.linalg.norm(b - np.dot(a, x), ord = 2)

分別使用 scipy.linalg 中的 lstsq 和 norm 計算最小二乘和殘差。

(4)print("\n x is \n", x)

print("\n norm is ", norm)

輸出結果。

執行樣例如下:

完整**如下:

步驟如下:

(1) def fun(x):

return(-(math.sin(x-2)**2)*math.exp(-(x**2)))

定義函式

(2) fval = -scipy.optimize.brute(fun, ((-10, 10, 0.1),),full_output=true)[1]

因為我沒找到使用scipy.optimize.brute求最大值的方法,所以退而求其次先將函式反過來求最小值,在將最小值取反就是最大值。

(3)print("\n the maximum is : ", fval)

輸出結果

執行樣例如下:

完整**如下:

步驟如下:

(1)n = int(input(" please input the row of matrix: "))

m = int(input(" please input the column of matrix: "))

通過使用者互動確定矩陣的行和列

(2)x = np.matrix(np.random.rand(n, m))

用均勻分布的隨機數生成n行m列的矩陣

(3)print("\n the matrix x is: \n", x)

輸入矩陣x

(4)print("\n the pairwise distances between every two rows are: \n", scipy.spatial.distance.cdist(x, x, "euclidean"))

scipy.spatial.distance.cdist(xa, xb, metric='euclidean', p=none, v=none, vi=none, w=none),該函式用於計算兩個輸入集合的距離,通過metric引數指定計算距離的不同方式得到不同的距離度量值,其中的metric='euclidean'引數指定了歐式距離,也就是我們平常說的距離。

執行樣例如下:

完整**如下:

# exercise 10.3

import numpy as np

import scipy.spatial.distance

n = int(input(" please input the row of matrix: "))

m = int(input(" please input the column of matrix: "))

x = np.matrix(np.random.rand(n, m))

print("\n the matrix x is: \n", x)

print("\n the pairwise distances between every two rows are: \n", scipy.spatial.distance.cdist(x, x, "euclidean"))

高階程式設計技術 sklearn課後作業

作業要求 1 create a classification dataset n samples 1000,n features 10 2 split the dataset using 10 fold cross validation 3 train the algorithms gaussian...

高階程式設計技術作業 5

題目描述 使用乙個for迴圈列印數字1 20 包含 展示 for number in range 1,21 print number input null output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 題目描述 通過給函式rang...

高階程式設計技術作業 7

題目描述 使用乙個字典來儲存一些人喜歡的數字。請想5個人的名字,並將這些名字用作字典中 的鍵 想出每個人喜歡的乙個數字,並將這些數字作為值儲存在字典中。列印每個人的名字和喜歡 的數字。展示 dic for name,number in dic.items print name str number ...