10.1 Least squares
import numpy as np
import matplotlib.pyplot as plt
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))
0.5779410996716825
10.2 Optimization
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])
Optimization terminated successfully.
Current function value: -0.911685
Iterations: 20
Function evaluations: 40
0.9116854117069156
10.3 Pairwise distances
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.8255083 1.3233615 1.70564367 1.68816353 1.907729
1.37292287 1.33206963 1.46680072 1.67172353 1.51245699 1.83919143
1.6343457 2.01359711 1.5035566 2.0194571 1.83912518 1.54535941
1.10851153 1.13954515]
[1.8255083 0. 1.45480887 1.39334739 1.83538545 1.62762047
2.08628072 1.35684929 1.60240418 1.54565392 1.45733713 1.6938131
1.76131921 1.87014255 1.6101837 1.95375927 2.0942955 1.85699064
1.47641468 1.83369239]
[1.3233615 1.45480887 0. 1.55233309 1.41634091 1.50623781
1.73603052 1.21601569 1.22644976 1.1942339 1.29232644 1.44460897
1.84999601 1.70236359 1.58569062 1.76824458 1.62407271 1.50669274
1.14259028 1.14262761]
[1.70564367 1.39334739 1.55233309 0. 1.7907392 1.27934106
1.51898882 1.00008568 1.22838733 1.42852277 1.68648643 1.54097522
1.5891838 1.82396554 1.50133431 1.64998201 1.57734591 1.51057835
1.50575196 1.56994583]
[1.68816353 1.83538545 1.41634091 1.7907392 0. 1.68987049
1.89287219 1.59130217 1.49533072 1.74618442 1.48528268 1.46713302
1.78249026 1.73106833 1.33860987 1.85383719 1.6815895 1.8147035
1.69545955 1.69541312]
[1.907729 1.62762047 1.50623781 1.27934106 1.68987049 0.
1.87570553 1.52995765 1.50961105 1.27674787 1.78387663 1.56257289
1.91022634 1.36631796 1.45011892 1.54083066 1.25633545 1.65211347
1.62264078 1.51248051]
[1.37292287 2.08628072 1.73603052 1.51898882 1.89287219 1.87570553
0. 1.52765451 1.61216536 1.66519004 1.59938005 1.70418421
1.87272336 1.96557277 1.74486191 1.83879798 2.0750937 1.33432537
1.7897053 1.38791718]
[1.33206963 1.35684929 1.21601569 1.00008568 1.59130217 1.52995765
1.52765451 0. 1.47191267 1.63401719 1.2076179 1.26888924
1.49216377 1.78667064 1.74629737 1.70191685 1.56163056 1.52834119
1.1623544 1.58563124]
[1.46680072 1.60240418 1.22644976 1.22838733 1.49533072 1.50961105
1.61216536 1.47191267 0. 1.40832784 1.60407676 1.46302626
1.44836971 1.90399108 1.11646533 1.74357027 1.66662889 1.3618925
1.3114959 0.97000807]
[1.67172353 1.54565392 1.1942339 1.42852277 1.74618442 1.27674787
1.66519004 1.63401719 1.40832784 0. 1.71777062 1.70299916
2.05823119 1.35905002 1.40489921 1.70707118 1.83324038 1.73194925
1.3355756 1.23135219]
[1.51245699 1.45733713 1.29232644 1.68648643 1.48528268 1.78387663
1.59938005 1.2076179 1.60407676 1.71777062 0. 0.99743719
1.57653889 1.59907937 1.80004159 1.76459584 2.05587007 1.57505553
1.33647951 1.65257311]
[1.83919143 1.6938131 1.44460897 1.54097522 1.46713302 1.56257289
1.70418421 1.26888924 1.46302626 1.70299916 0.99743719 0.
1.41888379 1.51823106 1.72850623 1.29840362 1.6736986 1.7436242
1.5881486 1.8212931 ]
[1.6343457 1.76131921 1.84999601 1.5891838 1.78249026 1.91022634
1.87272336 1.49216377 1.44836971 2.05823119 1.57653889 1.41888379
0. 2.09721039 1.5902207 1.29134721 1.96374179 1.46191596
1.63851904 1.86740906]
[2.01359711 1.87014255 1.70236359 1.82396554 1.73106833 1.36631796
1.96557277 1.78667064 1.90399108 1.35905002 1.59907937 1.51823106
2.09721039 0. 1.78767414 1.49435082 1.82145252 2.16266469
1.7392523 1.9214798 ]
[1.5035566 1.6101837 1.58569062 1.50133431 1.33860987 1.45011892
1.74486191 1.74629737 1.11646533 1.40489921 1.80004159 1.72850623
1.5902207 1.78767414 0. 1.8016522 1.7375671 1.74832521
1.43666134 1.33591522]
[2.0194571 1.95375927 1.76824458 1.64998201 1.85383719 1.54083066
1.83879798 1.70191685 1.74357027 1.70707118 1.76459584 1.29840362
1.29134721 1.49435082 1.8016522 0. 1.69125662 1.69379011
2.00591262 2.04427939]
[1.83912518 2.0942955 1.62407271 1.57734591 1.6815895 1.25633545
2.0750937 1.56163056 1.66662889 1.83324038 2.05587007 1.6736986
1.96374179 1.82145252 1.7375671 1.69125662 0. 2.13058872
1.95479252 1.85766133]
[1.54535941 1.85699064 1.50669274 1.51057835 1.8147035 1.65211347
1.33432537 1.52834119 1.3618925 1.73194925 1.57505553 1.7436242
1.46191596 2.16266469 1.74832521 1.69379011 2.13058872 0.
1.62567976 1.20866569]
[1.10851153 1.47641468 1.14259028 1.50575196 1.69545955 1.62264078
1.7897053 1.1623544 1.3114959 1.3355756 1.33647951 1.5881486
1.63851904 1.7392523 1.43666134 2.00591262 1.95479252 1.62567976
0. 1.1218935 ]
[1.13954515 1.83369239 1.14262761 1.56994583 1.69541312 1.51248051
1.38791718 1.58563124 0.97000807 1.23135219 1.65257311 1.8212931
1.86740906 1.9214798 1.33591522 2.04427939 1.85766133 1.20866569
1.1218935 0. ]]