Numpy库学习

Learning Numpy, a lib of python with matrix and other calculation.

Prepare

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈Rn×m and B ∈Rm×m, for n = 200, m = 500.

Code

1
2
3
4
5
6
7
8
9
10
11
12
import numpy 
from scipy.linalg import toeplitz

numpy.random.seed()
n = 200
m = 500

A = numpy.random.normal(size = [n, m])
B = toeplitz(numpy.random.normal(size=m))

print("------------A-----------\n"+str(A))
print("------------B-----------\n"+str(B))

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[[-0.49837536 -0.3247838   0.50678674 ..., -1.2703934   0.76603943
0.03755445]
[-0.03462894 -0.21418493 1.22520135 ..., 0.84866266 0.40158282
-0.65401731]
[ 1.1291174 0.49403126 -0.28698245 ..., 0.61324651 0.73156374
-1.58197964]
...,
[-0.68737602 -0.89741706 0.12970931 ..., -0.82061635 0.09854131
1.00242481]
[-0.31053207 -0.05350655 -1.51289055 ..., -2.26626492 0.79240834
-0.73548947]
[ 0.29646267 0.44302565 1.34552379 ..., 0.12526137 -0.74942753
-1.40240914]]
------------B-----------
[[ 1.44784231 -0.33696399 -0.1432767 ..., 0.26606438 -0.09475546
-1.50552307]
[-0.33696399 1.44784231 -0.33696399 ..., -1.16119122 0.26606438
-0.09475546]
[-0.1432767 -0.33696399 1.44784231 ..., -1.39562368 -1.16119122
0.26606438]
...,
[ 0.26606438 -1.16119122 -1.39562368 ..., 1.44784231 -0.33696399
-0.1432767 ]
[-0.09475546 0.26606438 -1.16119122 ..., -0.33696399 1.44784231
-0.33696399]
[-1.50552307 -0.09475546 0.26606438 ..., -0.1432767 -0.33696399
1.44784231]]

Knowledge

  1. random.normal() to create a matrix with Gaussian entries.
  2. toepltitz() to create a toepltitz matrix.
  3. random.seed() , without argument, set random seed by system time.

Exercise 9.1: Matrix operations

Calculate A + A, AA’, A’A and AB. Write a function that computes A(B − λI) for any λ.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
A_add_A = A + A
A_mul_AT = numpy.matmul(A, A.transpose())
AT_mul_A = numpy.matmul(A.transpose(), A)
A_mul_B = numpy.matmul(A, B)
print("---------- A+A -----------\n"+str(A_add_A))
print("---------- AA' -----------\n"+str(A_mul_AT))
print("---------- A'A -----------\n"+str(AT_mul_A))
print("---------- AB ------------\n"+str(A_mul_B))

def E_9_1(A, B, N):
return numpy.matmul(A, B-N*numpy.eye([len(B), len(B)]))

print("--------- E_9_1 ----------\n"+str(E_9_1(A, B, 3)))

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---------- A+A -----------
[[-0.99675071 -0.6495676 1.01357349 ..., -2.5407868 1.53207886
0.0751089 ]
[-0.06925787 -0.42836986 2.4504027 ..., 1.69732532 0.80316565
-1.30803462]
[ 2.25823479 0.98806253 -0.5739649 ..., 1.22649302 1.46312748
-3.16395929]
...,
[-1.37475203 -1.79483411 0.25941863 ..., -1.64123271 0.19708262
2.00484962]
[-0.62106414 -0.1070131 -3.0257811 ..., -4.53252983 1.58481669
-1.47097894]
[ 0.59292533 0.8860513 2.69104758 ..., 0.25052273 -1.49885506
-2.80481829]]
---------- AA' -----------
[[ 524.50900633 -23.41422424 -8.4109328 ..., -17.09509132
-36.58226516 22.36854305]
[ -23.41422424 474.25665538 -20.03371708 ..., -15.65752606
14.95693007 -6.58265367]
[ -8.4109328 -20.03371708 534.41749229 ..., -10.27103728
19.68694583 30.70304678]
...,
[ -17.09509132 -15.65752606 -10.27103728 ..., 440.6393497 -3.86846872
24.44312808]
[ -36.58226516 14.95693007 19.68694583 ..., -3.86846872
460.44551208 -24.6430389 ]
[ 22.36854305 -6.58265367 30.70304678 ..., 24.44312808 -24.6430389
533.38452907]]
---------- A'A -----------
[[ 207.71499004 -8.83528023 17.29332948 ..., 1.35094389
9.97232568 18.87072786]
[ -8.83528023 151.22418385 18.14374619 ..., 0.76927965
4.16070767 -16.40379885]
[ 17.29332948 18.14374619 205.02249127 ..., -8.18432486
-9.26707911 2.07063859]
...,
[ 1.35094389 0.76927965 -8.18432486 ..., 208.97270412
-5.71621365 8.51065013]
[ 9.97232568 4.16070767 -9.26707911 ..., -5.71621365
174.85487874 -6.98119382]
[ 18.87072786 -16.40379885 2.07063859 ..., 8.51065013
-6.98119382 186.52479968]]
---------- AB ------------
[[-14.87853525 8.70049763 0.66427479 ..., -40.15235048 38.96819378
36.98761191]
[ 8.62765483 -38.65652814 1.54867974 ..., 51.0022929 2.29161362
2.36645638]
[-28.67531663 -23.03994764 5.9337154 ..., -1.57998333 35.43202865
-1.02604062]
...,
[ 21.1088727 -24.70137887 2.29477385 ..., 20.56806809 -33.32895855
-10.71314031]
[ 16.33190225 -17.93547546 -9.57141851 ..., -40.75340698 65.49378203
-19.09691261]
[-29.11460503 -43.80853239 35.2469557 ..., 15.52438326 20.15859321
-22.71350542]]
--------- E_9_1 ----------
[[-13.38340918 9.67484904 -0.85608544 ..., -36.34117028 36.67007549
36.87494856]
[ 8.73154164 -38.01397334 -2.12692431 ..., 48.45630492 1.08686515
4.32850831]
[-32.06266882 -24.52204143 6.79466275 ..., -3.41972285 33.23733743
3.71989831]
...,
[ 23.17100075 -22.0091277 1.90564591 ..., 23.02991715 -33.62458247
-13.72041474]
[ 17.26349845 -17.77495581 -5.03274686 ..., -33.95461223 63.116557
-16.89044419]
[-30.00399303 -45.13760934 31.21038433 ..., 15.14859916 22.40687581
-18.50627799]]

Knowledge

  1. matmul() to calculate the mul of 2 matrics.
  2. eye(k) to calculate a diagonal matrix, k*k
  3. transpose() to return matrix’s Inverse matrix

Exercise 9.2: Solving a linear system

Generate a vector b with m entries and solve Bx = b

Code

1
2
3
4
b = numpy.random.normal(size=m)
x = numpy.linalg.solve(B, b)

print("--------- E_9_2 ----------\n"+str(x))

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
--------- E_9_2 ----------
[ 0.77346147 -0.80520425 -0.35482528 -0.56178735 -0.21607332 0.09578284
0.45176672 -0.349336 -0.07061673 -0.67937045 0.77608967 0.02769846
-0.00266615 -0.78878366 0.32858871 -0.22730774 0.46410093 0.08025457
-0.39018536 0.31449226 -0.15399999 -0.90204851 0.36424562 0.61096627
0.33907227 -0.78779662 -0.48162797 -0.64274742 0.12995262 0.99430534
-0.14856798 -1.15273747 0.09436944 0.40647302 0.65276383 -0.10382041
0.41243053 -0.29856156 0.63452485 -0.22499859 -0.30249207 -0.14497932
0.85522387 0.21110914 -0.23258855 -0.9897657 -0.31338026 -0.26002534
0.924013 -0.47043625 -0.45807153 -0.75775808 1.12480813 0.30321897
-0.29125859 -0.33289894 -0.64971968 0.50094724 0.36235713 -0.00269059
-0.15268968 -0.01259865 0.41392597 -0.83904654 -0.47292343 -0.18366766
0.15149868 1.16077591 -0.53859254 -0.98168055 -0.42284948 1.11918415
0.47384948 -0.21576477 -0.57785042 0.16373249 0.58937659 0.05069881
-0.60646457 -0.07515867 -0.25892701 0.60705024 -0.40504958 -0.41706291
0.02971281 0.69272723 -0.27455502 -0.26949703 -0.88870169 -0.1796936
0.3022728 0.32450019 -0.40309466 -0.28997186 0.28757543 0.83201615
0.18273413 0.19503945 -0.83802164 0.13698447 0.35706851 0.41057604
0.15437131 -0.12593118 -0.78262502 0.46311827 0.18733747 -0.12280002
-0.17000113 0.72736406 0.04638688 -0.0446584 -0.3740762 0.10985653
0.55118338 0.58155485 -0.31415809 0.02940531 0.26057846 1.10319678
0.32003339 -0.66484382 -0.24373552 0.85396941 0.25111252 -0.07437428
-0.42373725 -0.00220312 0.49085239 0.58744762 -0.73623476 0.00606373
0.30887715 0.40351642 0.24192468 -0.60608552 -0.06592915 0.57245086
1.18607234 -0.05819671 -0.63773722 -0.8275492 0.47117888 0.8240523
0.09995859 -1.1189529 -0.68472816 0.08958122 0.94135285 -0.21713449
-0.22415887 -0.73924185 -0.00985684 0.53434199 -0.22782094 -0.00677473
0.18406202 0.25589949 0.02174665 -0.47176496 -1.00158691 0.29509947
0.46664914 -0.65511163 -0.15173256 -0.22610517 0.71416397 0.42033317
-0.8626862 -0.95496022 0.41023983 0.69597171 0.79422447 -0.10630244
-0.90460346 0.04098153 0.52159034 0.65757128 -0.40015286 -0.10895269
-0.21034717 0.56477992 0.35828024 0.08569485 -0.29118951 0.26209546
-0.08259761 -0.49767682 0.00403437 0.15095307 0.35145479 0.5900982
0.02764191 -0.2800535 0.37674186 0.24215259 0.19028278 -0.22832022
-0.58452005 0.23195453 1.01708531 0.21018793 -0.07069491 -0.20749496
-0.30999703 0.98823655 0.88781881 0.01026305 -0.31819752 -0.45149265
0.23417009 0.20983107 0.22389019 -0.00310436 0.33739053 -0.02782074
0.05340627 -0.24885277 0.25400433 0.64797729 -0.03559467 -0.05009246
-0.08380507 0.36138515 0.5550696 0.28391703 -0.39964763 0.33188527
-0.15623489 0.53671244 -0.07852636 -0.33260849 -0.26049435 0.16521133
-0.29556138 -0.14604016 0.1189506 -0.72699557 0.75795078 -0.72868493
0.60271873 -0.62082569 0.20283552 0.06472603 0.31866239 -0.90097767
-0.15603714 -0.53582783 0.88605567 -0.36223599 -0.59827917 -1.29951569
0.1108652 0.66520771 0.01635112 -0.47215453 -1.24440155 -0.08236616
0.77307018 0.08110942 0.20703049 -1.09960752 0.53298015 -0.1027147
0.57313429 -0.26114314 0.36161462 -0.63713623 0.24713223 -0.12035843
0.11065281 0.09846977 -0.43356553 -0.02745542 -0.03750936 0.15417482
0.02681122 0.35293284 -0.22082268 0.52545969 -0.52711138 0.94215319
0.59478862 0.44168817 -0.27060941 -0.43870625 0.34660181 1.0296679
0.96253921 0.09362239 0.12398285 -0.32504824 0.59667371 -0.29713152
-0.15533326 -0.08547569 0.24128572 -0.25817327 0.12638511 -0.32930891
0.34818106 0.62209695 0.00579373 -0.09340648 0.29222168 0.50828812
0.03484351 0.3983519 -0.65951852 0.07763202 0.09992687 0.31966399
0.19685432 -0.37118144 -0.13390334 0.54808429 0.50920353 -0.074975
-0.76687343 -0.43923484 -0.09119431 0.32772465 0.34325338 0.01731712
-0.28073798 0.39537496 0.09365181 -0.24057869 -0.5238085 -0.3035102
0.53520429 0.94732342 -0.40593518 -0.37112931 -0.0610225 0.26211594
0.51096725 -0.74657876 -0.4668039 0.34383364 0.65990638 -0.67455365
-0.20302455 -0.51223175 0.20865335 0.31091778 -0.3855612 -0.58707168
0.21290637 0.28386365 0.25445908 -0.42757652 -0.34554999 0.57824806
0.56947678 -0.44043856 -0.59497373 -0.19046726 0.78359797 0.125395
0.01909045 -1.25379069 -0.09729798 0.52626197 0.78733424 -0.54835847
-0.47638112 -0.07003016 0.55939806 1.16409273 -0.0810496 -0.18461299
0.41337285 0.72340257 0.47537153 0.11145316 -0.41716581 0.15447172
0.87950718 0.32799822 -0.72525093 -0.25424443 -0.33243891 0.89624186
-0.24327463 -0.377505 -0.01318703 1.15073861 0.50827694 0.14629782
-0.60745337 0.35594258 0.77265696 0.51980829 -0.43604754 0.19361863
0.0974694 0.12374675 0.22393652 -0.25111504 -0.16791473 0.66888136
0.08905014 -0.24185122 -0.82934214 0.17298365 0.32383379 0.38730725
-0.55691312 -0.14960641 -0.3161664 1.00423863 -0.13530144 0.2758329
-0.59409022 -0.72711962 0.18561096 0.29493483 -0.49462417 -0.02535275
-0.32179269 0.63802358 0.06450911 -0.36244535 -0.55088169 0.47010579
-0.03932424 -0.05139748 -0.72489824 0.43677258 0.85363747 0.59513256
-1.1427693 -0.75490234 -0.05380666 0.84642801 0.97061214 -0.82256622
-1.12268162 -0.11974293 0.25058075 -0.14730279 -0.41490229 0.47094733
0.0695535 -0.06111872 -0.00325791 -0.69918284 0.97462969 0.10417971
-0.06695869 -1.23309132 -0.17049135 0.64043296 0.82727541 -0.20828416
-0.59323465 -0.65802288 0.32621394 0.5368197 0.37921229 -1.08605397
0.16858299 0.03444726 0.11315069 0.05051093 0.03860219 -0.35531796
0.3553949 0.39874088 -0.52344943 -0.13202249 0.91263459 0.32986441
0.26566883 -0.62952324 -0.54520924 -0.43928257 1.0950506 0.1665704
-1.02249691 -0.05817481 -0.30783025 0.09503854 0.06272448 -0.35042346
0.06067587 0.08451991 0.3850205 -0.77288649 0.17066585 0.10336318
0.37866533 -0.63292289 -0.15179168 0.06741788 0.41384271 -0.29224392
0.39719698 -1.22197233]

Knowledge

  1. numpy.linalg.solve(a, b) to calculate the result of ax = b

Exercise 9.3: Norms

Compute the Frobenius norm of A: |A|F and the infinity norm of B: |B|∞. Also find the largest and smallest singular values of B.

Code

1
2
3
4
5
6
7
8
9
10
A_FN = numpy.linalg.norm(A, 'fro')
B_IN = numpy.linalg.norm(B, numpy.inf)
print("---------- A_FN ------------\n"+str(A_FN))
print("---------- B_IN ------------\n"+str(B_IN))

U, sigma, V = numpy.linalg.svd(B)
B_max_singular = numpy.max(sigma)
B_min_singular = numpy.min(sigma)
print("---------- B_max_singular ------------\n"+str(B_max_singular))
print("---------- B_min_singular ------------\n"+str(B_min_singular))

Result

1
2
3
4
5
6
7
8
---------- A_FN ------------
316.813378033
---------- B_IN ------------
387.19726512
---------- B_max_singular ------------
51.6474766426
---------- B_min_singular ------------
0.0562452006537

Knowledge

  1. 矩阵范数:norm, numpy.linalg.norm()
  2. 奇异值分解:singular values, numpy.linalg.svd()

Exercise 9.4: Power iteration

Generate a matrix Z, n × n, with Gaussian entries, and use the power iteration to find the largest eigenvalue and corresponding eigenvector of Z. How many iterations are needed till convergence?

Optional: use the time.clock() method to compare computation time when varying n.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import time

Z = numpy.random.normal(size=[n, n])

def power_iteration_eigenvalue( X, err=0.01):
x = numpy.random.normal(size=len(X)).transpose()
step = numpy.inf
times = 0
time_begin = time.clock()

while step > err:
old = numpy.max(x)
x = x/old
x = numpy.matmul(X, x)
new = numpy.max(x)
step = abs(old-new)
times += 1
# print("step "+str(times)+" : "+str(new))

time_end = time.clock()
return x, new, times, time_end-time_begin

[eigenvector, eigenvalue, times, running_time] = power_iteration_eigenvalue(Z)

print("------------ E_9_4 ----------------")
print("eigenvector: "+str(eigenvector))
print(" eigenvalue: "+str(eigenvalue))
print(" step count: "+str(times))
print(" time used: "+str(running_time))

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
------------ E_9_4 ----------------
eigenvector: [ 6.19366521e+00 -7.19111305e+00 7.47822455e+00 1.76596816e+00
-1.22108278e+01 -6.68997718e+00 -1.07959431e+01 7.38413171e+00
9.60996846e+00 -3.39415864e+00 -1.23836889e+01 1.60311426e+01
-3.55845284e-01 -1.69182976e+01 6.50582275e+00 -3.93769884e+00
7.23395058e+00 -6.30992140e+00 3.77506490e+00 -3.23313667e+00
6.64974007e+00 -7.24150321e+00 9.88020162e+00 2.08854412e-03
3.07409841e+00 3.42129651e+00 -5.92382223e+00 -1.35043015e+00
-1.40770229e+00 1.24531978e+01 -6.32025548e+00 -1.26441627e+01
3.26831772e-01 -1.42739520e+00 1.32925887e+00 1.06598272e+01
-8.48197449e+00 4.82646327e+00 3.67864359e+00 -2.92456117e+00
1.49065221e+01 -3.89251672e+00 1.24128342e+01 1.27677631e+01
-1.11263113e+01 -1.46350312e+01 9.36651121e+00 -3.84124488e-01
-5.67795245e+00 -8.75638867e+00 6.02814460e+00 -9.83530958e-01
6.50873016e+00 9.18944025e+00 -1.08015910e+01 -2.27073094e+00
2.05116066e+00 9.74180888e+00 4.34640807e+00 -1.45355560e+00
1.72229371e+00 1.60129994e+00 -9.49922896e+00 -9.26528224e+00
1.41245323e+01 1.29324148e+00 1.15170129e+01 -2.78286401e+00
-2.41103992e+00 -6.04664999e+00 1.60510520e+01 9.06866735e+00
-2.39722406e+00 -7.74249726e+00 -6.22329381e+00 5.66999289e+00
7.07244637e-01 -1.42750525e+01 1.00944549e+01 -1.56414851e-01
-6.77685962e+00 -1.31324847e+00 1.40962962e+01 -2.82314517e+00
1.15847925e+00 -1.45499963e+01 -1.47587123e+00 -5.45102517e+00
2.07919993e+00 1.48515566e+00 -3.34405736e+00 -4.93831256e-01
3.52542969e+00 -4.49030531e+00 -4.14344570e+00 -6.73085059e+00
-1.96366164e+00 -4.73358636e+00 -1.42491876e+01 7.14478669e-01
-4.03345293e+00 -8.64438839e-01 -1.91710524e+00 4.02300501e-01
-3.12336156e+00 3.25908960e+00 8.73984942e+00 1.80387754e+00
5.37126471e-01 2.33427637e-01 7.20753102e+00 8.38898454e+00
6.73706722e+00 2.25368920e+00 4.03456123e+00 -5.26635000e-01
-1.09389778e+01 -1.69632014e+01 -5.04630146e+00 2.99535217e+00
-4.97695971e+00 7.08832858e+00 -1.42509689e+00 5.44444010e-01
1.14700391e+00 -3.34913269e+00 -1.68841359e+00 -1.85218648e+00
6.17064696e+00 -1.17127043e+00 -7.25388347e+00 -2.70293145e+00
-8.28104371e+00 1.64055792e+00 4.94486141e+00 4.18690047e+00
-9.94856800e+00 -8.29269461e+00 5.81317629e+00 -1.31414167e+01
8.10423657e-01 -1.30089443e+01 -7.10088385e+00 -7.62918634e+00
5.14150493e+00 4.43537243e+00 -3.52960575e-01 -1.35215797e+00
-5.68239918e+00 -5.78998103e+00 -4.91632266e-01 -2.68038511e+00
-1.03668811e+01 5.49950249e+00 -2.26436209e+00 1.23865979e+01
6.14732355e+00 4.31623361e+00 -8.40648179e+00 -3.72063488e+00
2.01815136e+00 -5.14877954e-01 -1.16816147e+00 -2.24455122e+01
-5.57878300e+00 3.10970510e-01 1.29690655e+00 -1.69972653e+01
1.58677609e+01 -5.77551543e+00 -4.36274195e+00 -5.63981586e+00
-4.54383986e+00 -7.16379461e-01 -4.07212591e+00 -5.45036929e+00
-9.33501142e+00 -1.46760018e+01 -4.31132630e+00 1.16448053e+01
6.21216923e+00 -6.78022246e+00 4.13536082e+00 3.69218241e+00
1.97466803e+00 -1.08666389e+00 8.80475201e+00 2.46159249e+00
2.21402112e+00 -2.46765512e+00 3.57973968e+00 -1.11716056e-01
-8.24346370e+00 -2.90508233e+00 4.13721878e+00 -4.10881873e+00
5.15073997e+00 -1.22542384e+01 3.96149388e+00 -1.13627510e+01]
eigenvalue: 16.0510519646
step count: 68
time used: 0.007216

Knowledge

  1. eigenvalue: 特征值
  2. 幂迭代法:x(k) = x(k-1)*A

Exercise 9.5: Singular values

Generate an n × n matrix, denoted by C, where each entry is 1 with probability p and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of C. What can you say about the
relationship between n, p and the largest singular value?

Code

1
2
3
4
5
6
7
8
p = 0.1
print("-------------- E_9_5 ------------------")
while p<1:
C = numpy.random.binomial(1, p, [n, n])
U, singular_value, V = numpy.linalg.svd(C)
max_singular = numpy.max(singular_value)
print("n= "+str(n)+" p= "+str(p)+" max_singular= "+ str(max_singular))
p += 0.1

Result

1
2
3
4
5
6
7
8
9
10
11
-------------- E_9_5 ------------------
n= 200 p= 0.1 max_singular= 20.6567657213
n= 200 p= 0.2 max_singular= 40.4920520455
n= 200 p= 0.3 max_singular= 60.6782510601
n= 200 p= 0.4 max_singular= 81.0362830637
n= 200 p= 0.5 max_singular= 101.14764207
n= 200 p= 0.6 max_singular= 120.373897161
n= 200 p= 0.7 max_singular= 139.768077408
n= 200 p= 0.8 max_singular= 160.37471516
n= 200 p= 0.9 max_singular= 179.191890776
n= 200 p= 1.0 max_singular= 200.0

Knowledge

  1. random.binomial(n, p, size), n is 0~n, p is probability, size is size
  2. we can find that max_singular = n * p

Exercise 9.6: Nearest neighbor

Write a function that takes a value z and an array A and finds the element in A that is closest to z. The
function should return the closest value, not index.

Hint: Use the built-in functionality of Numpy rather than writing code to find this value manually. In
particular, use brackets and argmin.

Code

1
2
3
4
5
6
7
8
9
def closest( X, z):
ind = numpy.argmin(numpy.abs(X - z * numpy.ones((len(X), len(X[0])))))
return X[ind//len(X[0])][ind%len(X[0])]

print("-------------- Closest ---------------")
z = -1
while z <= 1:
print("z= "+str(z)+" closest= "+str(closest(A, z)))
z += 0.1

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-------------- Closest ---------------
z= -1 closest= -1.00000868018
z= -0.9 closest= -0.899991177575
z= -0.8 closest= -0.799982994952
z= -0.7 closest= -0.699995902891
z= -0.6 closest= -0.59999964816
z= -0.5 closest= -0.499995702358
z= -0.4 closest= -0.399997886746
z= -0.3 closest= -0.299999122356
z= -0.2 closest= -0.200002337307
z= -0.1 closest= -0.0999872558528
z= -1.38777878078e-16 closest= -1.45838021333e-05
z= 0.1 closest= 0.10001924864
z= 0.2 closest= 0.200009578588
z= 0.3 closest= 0.30001507798
z= 0.4 closest= 0.399992197442
z= 0.5 closest= 0.499998597393
z= 0.6 closest= 0.599983955592
z= 0.7 closest= 0.700043443047
z= 0.8 closest= 0.800006259776
z= 0.9 closest= 0.900013425609
z= 1.0 closest= 1.00000424317

Knowledge

  1. argmin(X) to return X’s minimum’s index

Reference