[python 作业] [第十二周] scipy exercise

这里写图片描述
代码:

import numpy as np
from scipy import linalg

m = 8
n = 6

A = np.random.random(size=(m, n))  
b = np.random.random(size=(m, 1))  

x, residual, rank, sigma = linalg.lstsq(A, b)

print('x = \n', x)
print('\nresidual = ', residual)

运行结果:

x =
 [[-0.33086002]
 [ 0.57158951]
 [-0.20039154]
 [ 0.0827364 ]
 [ 0.42251961]
 [ 0.63691177]]

residual =  [0.04890205]

这里写图片描述
代码:

import numpy as np
from scipy import optimize

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

res = optimize.fmin(f, 0)
print(-f(res)[0])

运行结果:

Optimization terminated successfully.
         Current function value: -0.911685
         Iterations: 20
         Function evaluations: 40
0.9116854117069156

这里写图片描述
代码:

import numpy as np
from scipy.spatial.distance import pdist

n = 8
m = 6

X = np.random.normal(size=(n, m))
res = pdist(X)
print(res)

运行结果:

[3.49988497 4.69111063 4.5213589  3.34846844 4.299797   3.98998393
 4.14794446 5.32816082 2.68402884 4.34600268 4.81397471 4.49009711
 5.31291905 4.0624216  5.02380939 5.09030061 4.44667527 3.11379282
 3.72434268 4.34173185 3.91284831 4.25980313 4.31800204 3.06496398
 3.33777872 5.20893929 5.64443982 2.23220161]

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/80589599