引入模块,设置随机数种子
import numpy as np
from scipy import optimize
import time
np.random.seed(np.int64(time.time()))
m = 20
n = 10
A = np.random.random(size = [m, n])
b = np.random.random(size = [m])
num0 = np.random.random(size = [n])
def error(x, A, b):
return np.dot(A, x) - b
res = optimize.leastsq(error, num0, args = (A, b))[0]
residual = np.dot(A, res) - b
norm = np.linalg.norm(res, ord = np.inf)
print(norm)
print(residual)
结果
def f(x):
return (-1) * (np.sin(x-2) ** 2) * np.exp(-1 * x * x)
print(optimize.fmin(f, 0))
结果
n = 5
m = 10
import scipy.spatial.distance as dis
X = np.random.random(size = (n, m))
Y = dis.pdist(X, 'cityblock')
print(Y)
结果