10.1 Least squares
使用lstsq进行线性回归
import numpy
as np
from scipy.linalg
import *
m =
20
n =
15
A = np.random.random((m, n))
b = np.random.random((m,))
cc, re, rank, sigma = lstsq(A, b)
b1 = A.dot(cc)
delta = b - b1
print(norm(delta))
10.2 optimization
使用fmin即可
import numpy
as np
from scipy.linalg
import *
from scipy.optimize
import fmin
def
f(
x):
return -(
1 * np.sin(x-
2)**
2 * np.e**(-
1*x**
2))
opt = fmin(f,
0)
print(-f(opt)[
0])
10.3 pairwise distances
可以直接使用scipy的距离计算函数 pdist求出
import numpy
as np
import scipy.spatial.distance
n =
20
m =
15
A = np.random.rand(n,m)
X = scipy.spatial.distance.pdist(A)
Y = scipy.spatial.distance.squareform(X)
print(Y)
[[0. 1.70723364 1.67232394 2.14848234 1.81661124 1.52028073
1.72870584 1.44979784 1.91241827 1.6690722 1.57910147 1.81669617
2.14387069 1.70279202 1.70946341 1.47205802 1.91203106 1.88894804
1.72074019 1.33649791]
[1.70723364 0. 1.66529196 1.56321923 1.31698055 1.44271224
1.5209942 1.39646947 1.29319299 1.4415709 1.34903143 1.70376255
1.71089653 1.70809496 1.85715285 1.53346995 1.90699968 1.53557633
1.69210972 1.4356786 ]
[1.67232394 1.66529196 0. 2.16537299 1.61166394 1.62408598
1.99695736 1.39343426 1.95026217 1.37335845 1.82294837 2.07437076
2.06216394 2.17549423 1.74777236 1.46421705 1.68179628 1.85163016
2.15962043 1.57020591]
[2.14848234 1.56321923 2.16537299 0. 1.61744096 1.88851528
1.34413788 1.89398291 1.46709356 1.51890132 1.54229883 1.65237536
1.02859956 1.68758486 1.42972634 2.11055697 1.61379488 1.55930157
1.69316533 1.61673748]
[1.81661124 1.31698055 1.61166394 1.61744096 0. 1.57506447
1.64856206 1.54462691 1.26073619 1.67646547 1.67084812 1.81246537
1.58863275 1.56247758 1.86333648 1.42115663 1.56454956 1.36803946
1.61047539 1.34794565]
[1.52028073 1.44271224 1.62408598 1.88851528 1.57506447 0.
1.93076771 1.80667192 1.62390781 1.62131565 1.22412401 1.89877188
2.2287198 1.8424469 1.97116297 1.50919167 1.8116297 1.81767725
1.93822376 1.49301997]
[1.72870584 1.5209942 1.99695736 1.34413788 1.64856206 1.93076771
0. 1.5984583 1.3739134 1.73435405 1.32009888 1.46882034
1.29935889 1.17612914 1.35878312 2.07095613 1.67426578 1.64686582
1.42055562 1.35513523]
[1.44979784 1.39646947 1.39343426 1.89398291 1.54462691 1.80667192
1.5984583 0. 1.8219781 1.21034371 1.96697615 1.9136222
1.7131313 1.85591055 1.8493316 1.52669117 1.88982055 1.83252903
1.7503535 1.56149337]
[1.91241827 1.29319299 1.95026217 1.46709356 1.26073619 1.62390781
1.3739134 1.8219781 0. 1.63035212 1.34622488 1.36837991
1.515803 0.87209497 1.78371141 1.76672116 1.58975982 0.97117112
1.40584691 1.46286706]
[1.6690722 1.4415709 1.37335845 1.51890132 1.67646547 1.62131565
1.73435405 1.21034371 1.63035212 0. 1.65543959 1.83492604
1.55897106 1.8694899 1.83625683 1.38260266 1.81402838 1.52835775
2.00287105 1.53885769]
[1.57910147 1.34903143 1.82294837 1.54229883 1.67084812 1.22412401
1.32009888 1.96697615 1.34622488 1.65543959 0. 1.55108518
1.82778351 1.5795985 1.52008291 1.91682534 1.72790657 1.63059121
1.83492781 1.31934342]
[1.81669617 1.70376255 2.07437076 1.65237536 1.81246537 1.89877188
1.46882034 1.9136222 1.36837991 1.83492604 1.55108518 0.
1.52766165 1.50209549 1.65426736 2.1394657 2.11508613 1.98777079
1.35365351 1.6889287 ]
[2.14387069 1.71089653 2.06216394 1.02859956 1.58863275 2.2287198
1.29935889 1.7131313 1.515803 1.55897106 1.82778351 1.52766165
0. 1.61023629 1.56312161 2.14209356 1.51085459 1.53239633
1.69013196 1.69610014]
[1.70279202 1.70809496 2.17549423 1.68758486 1.56247758 1.8424469
1.17612914 1.85591055 0.87209497 1.8694899 1.5795985 1.50209549
1.61023629 0. 1.84857612 1.89971775 1.65491187 1.21571993
1.37919973 1.56664618]
[1.70946341 1.85715285 1.74777236 1.42972634 1.86333648 1.97116297
1.35878312 1.8493316 1.78371141 1.83625683 1.52008291 1.65426736
1.56312161 1.84857612 0. 2.19546315 1.28728038 1.81340596
1.4690638 1.17895255]
[1.47205802 1.53346995 1.46421705 2.11055697 1.42115663 1.50919167
2.07095613 1.52669117 1.76672116 1.38260266 1.91682534 2.1394657
2.14209356 1.89971775 2.19546315 0. 1.99391454 1.57438674
2.17212559 1.65971252]
[1.91203106 1.90699968 1.68179628 1.61379488 1.56454956 1.8116297
1.67426578 1.88982055 1.58975982 1.81402838 1.72790657 2.11508613
1.51085459 1.65491187 1.28728038 1.99391454 0. 1.20761278
1.7316566 1.35060886]
[1.88894804 1.53557633 1.85163016 1.55930157 1.36803946 1.81767725
1.64686582 1.83252903 0.97117112 1.52835775 1.63059121 1.98777079
1.53239633 1.21571993 1.81340596 1.57438674 1.20761278 0.
1.81923254 1.58007452]
[1.72074019 1.69210972 2.15962043 1.69316533 1.61047539 1.93822376
1.42055562 1.7503535 1.40584691 2.00287105 1.83492781 1.35365351
1.69013196 1.37919973 1.4690638 2.17212559 1.7316566 1.81923254
0. 1.15343142]
[1.33649791 1.4356786 1.57020591 1.61673748 1.34794565 1.49301997
1.35513523 1.56149337 1.46286706 1.53885769 1.31934342 1.6889287
1.69610014 1.56664618 1.17895255 1.65971252 1.35060886 1.58007452
1.15343142 0. ]]