python scipy

10.1

这里写图片描述

import numpy as np
from scipy import linalg

A = np.matrix(np.random.randint(3,size=8).reshape(4,2))
A = np.append(A, np.ones(4).reshape(4,1), axis=1)
b = np.matrix(np.random.randint(3,size=4).reshape(4,1))
x, resid, rank, sigma = linalg.lstsq(A, b)

print("x = ")
print(x)

print("residual = : ")
print(resid)

下面为运行截图,运行结果具有随机性
这里写图片描述

10.2

这里写图片描述

import numpy as np
import scipy.optimize as opt

def f(x):
    return -(np.sin(x-2)**2) * (np.e ** (-x**2))

grid = (-10, 10, 0.1)  

res = opt.brute(f, (grid,), finish=opt.fmin_bfgs)  

print(-f(res[0]))  

这里写图片描述

10.3

这里写图片描述

import numpy as np
import scipy
X = np.random.randint(10, size=20).reshape(5,4)
print(X)
print(scipy.spatial.distance.cdist(X, X, metric='euclidean'))

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_36974075/article/details/80563490