Numpy 练习题 [python]

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


Exercise 9.1  Matrix operations

Calculate A + A, AA^T, A^TA and AB. Write a function that computes A(B -tI) for any t.

import numpy as np  
from scipy.linalg import toeplitz 
n = 500
m = 200
A = np.random.normal(0, 1, (n, m))
B = toeplitz([np.random.randint(0,10) for i in range(m)])
I = np.eye(m)

def my_cal(A, B, c, I):
    print("A(B − λI):")
    print(np.dot(A, B - c * I))
    
print(I)
print("A + A = ")
print(A + A)
print("A * A^T = ")
print(np.dot(A, (A.T)))
print("A^T * A = ")
print(np.dot((A.T), A))
print("A * B = ")  
print(np.dot(A, B))
c = input("Please input the number λ: ")
my_cal(A, B, (float)(c), I)

输出如下:

A + A =
[[ 3.06909086 -1.50271134  0.67174266 ... -3.10578702 -2.23321335
   3.59647301]
 [ 1.47597495 -3.17790483 -0.68489134 ...  0.24584515  2.11483797
  -2.32826117]
 [-0.92104658 -1.07095285 -0.5354614  ... -5.32512822 -1.39767061
  -0.9678747 ]
 ...
 [ 3.71809692 -1.40291651 -0.11921303 ...  2.55538161 -1.57372239
   2.35351413]
 [-1.08751964  4.01600624 -0.70876684 ...  2.60531867  1.75911318
   0.46472421]
 [ 2.41136412  2.44978935 -1.32850447 ... -1.96899581 -4.35339454
  -0.13607945]]
A * A^T =
[[548.29952494 -22.09141065   9.61249477 ...   6.7253801  -58.35455005
  -18.42045925]
 [-22.09141065 506.6149021   22.11848442 ...  24.97457073 -14.13473161
   -4.01925732]
 [  9.61249477  22.11848442 503.26137116 ...  -0.97582535  25.21591873
    8.60506048]
 ...
 [  6.7253801   24.97457073  -0.97582535 ... 494.30333535  47.9519977
  -38.59133845]
 [-58.35455005 -14.13473161  25.21591873 ...  47.9519977  503.92636209
   16.84103945]
 [-18.42045925  -4.01925732   8.60506048 ... -38.59133845  16.84103945
  528.13223938]]
A^T * A =
[[205.70542049  -4.40435249  -6.0173339  ...   8.65782328  -6.40483879
   21.34610686]
 [ -4.40435249 221.5055544   -3.5043793  ...   3.36388935   2.85606594
   11.60282367]
 [ -6.0173339   -3.5043793  193.97071406 ...   2.84577274  -2.42543091
    7.48245373]
 ...
 [  8.65782328   3.36388935   2.84577274 ... 249.3008149   14.64135152
  -11.66543594]
 [ -6.40483879   2.85606594  -2.42543091 ...  14.64135152 212.78225417
   19.72743095]
 [ 21.34610686  11.60282367   7.48245373 ... -11.66543594  19.72743095
  222.30600027]]
A * B =
[[ 236.66845275  -27.29305289   27.01271686 ...  -42.91157397
   139.93292964   92.40224676]
 [ -24.57688845 -176.99066409 -101.23252293 ...  -90.8795647
    15.47198343 -106.32165478]
 [  86.97564604   28.81030362  -54.15793908 ...   18.06059017
   -33.46252969   64.10356434]
 ...
 [  84.60859263   73.87854772  -12.21131522 ...  -23.99543969
    81.70987545  147.86192277]
 [ 204.59927798  162.3218905   212.20593848 ...  109.67763438
   109.1394425   114.76613906]
 [  54.69245838   -9.34945747  156.27873985 ...  127.02542345
    53.03847578   15.25735865]]
Please input the number λ: 3
A(B − λI):
[[ 232.06481646  -25.03898589   26.00510288 ...  -38.25289345
   143.28274966   87.00753725]
 [ -26.79085088 -172.22380685 -100.20518591 ...  -91.24833243
    12.29972647 -102.82926303]
 [  88.35721591   30.41673289  -53.35474698 ...   26.0482825
   -31.36602378   65.55537639]
 ...
 [  79.03144725   75.98292249  -12.03249567 ...  -27.8285121
    84.07045903  144.33165158]
 [ 206.23055744  156.29788114  213.26908874 ...  105.76965637
   106.50077273  114.06905275]
 [  51.0754122   -13.0241415   158.27149656 ...  129.97891716
    59.56856759   15.46147783]]


Exercise 9.2  Solving a linear system:

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

import numpy as np  
from scipy.linalg import toeplitz 
n = 200
m = 500
B = toeplitz([np.random.randint(0,10) for i in range(m)])
b = np.random.randint(0, 10, m)

print("The solution of Bx = b: ")
print(np.linalg.solve(B,b))

输出如下:

The solution of Bx = b:
[-0.44824098 -0.03220555  1.81374464  0.4412983  -0.01355246 -0.39036682
 -0.74736775 -0.04613821  0.58183856  1.37922975 -0.6072447   0.41451853
 -0.29337274  0.23946239 -0.33755649  0.32935062  0.38822194 -0.07338522
  0.47738232 -1.53009123  1.68666719 -0.89777971 -0.97010471  0.05142401
 -0.70895877  0.42823973 -0.26575333  0.54400576 -0.96304928 -0.05186556
  0.61981005 -0.14153431  1.06969075  0.28451017 -0.00497315 -1.09306304
  0.17499214 -0.33512127  0.23583123 -0.10216405 -2.64940533  0.17015912
 -0.94892919  0.38938615  0.36898975  1.04665965 -1.49712064  0.0185039
  1.39980762 -0.71347249  2.03700789 -1.20304172  0.80022708 -0.5281429
 -0.56147942  0.67418818 -0.29262279  1.25352396 -1.56468146  1.09701217
 -1.0005353   0.84517154  2.06553476 -1.37338196  0.40809092 -0.70089702
  2.4175596  -1.90289553 -0.29376839  0.30483825 -1.37250389  0.50120982
 -1.43374031  1.93411667 -0.92270928  0.18149932 -0.41600039  1.81648341
  2.86788032 -1.21939664  2.02910781 -2.39405009  0.34451828 -1.14673104
 -0.47423046  1.14797527 -0.71623474 -0.75546384 -2.68725788  1.9430062
 -1.07866925  0.2064839  -0.14075385  0.31404969  1.50888398 -0.49432313
  3.60452548 -1.58935591 -0.0907105  -0.69000366  0.00741395  0.82202619
 -0.34526596  1.33702824 -3.30618512  1.53395755 -0.78066625  0.24721687
  1.16230487 -0.73908116  0.74614957 -1.97559296  2.44479381 -0.448386
  0.05276791  0.54267293 -0.90078373 -0.03978781 -1.77845108  2.46897471
 -0.99612562 -1.00379242 -1.34053095 -0.02894756  2.44328091 -0.99285811
  1.70252546 -1.3389048   0.77173685  0.04279382 -0.41881292  1.68251264
 -0.11922023 -0.40081827 -2.4804488   2.29099515  0.03556716 -0.51532943
  0.05282987 -1.78336748  0.43294898 -0.71234207  1.1277306  -0.53397058
  0.36259264  0.68728333 -0.85219199 -0.25694065  0.39110751  1.76443833
 -1.5884575   0.06221241 -0.34859658 -0.04269303  1.65378051 -0.42615014
 -0.05740766 -0.92265983  0.45879379 -1.38161992  0.93793119  0.37011633
 -0.9095583  -0.13928365 -0.09082968  1.11161    -0.71682695  0.99362698
 -1.53545494  0.09618673  1.6856487  -0.87278375  0.67127594  0.59932326
 -0.45912452 -0.88597836  2.24854683 -0.83671294 -1.53818519  2.14821902
 -1.1180676  -1.19430981  0.93372632  1.19277484 -1.80819226  0.80696807
  1.29067777 -3.2305663   0.46595003  1.11469545 -0.40742758 -0.86858827
  0.55219908 -1.41780735 -0.588899    1.96372431 -1.59729475 -0.06582068
  1.10609806  0.45042337 -0.64609912  0.84046538  1.86266182 -0.95877205
  0.48150318  0.13542555 -0.00366917 -0.36694069 -0.6366165  -0.23585867
 -0.05460575  0.64733502 -1.07802907 -0.45962326  1.59456695  0.96298101
 -1.46394751 -0.42809779  0.42647969  0.29609916  1.73129808 -1.10497974
 -0.75922939  0.6504216   0.45798529 -1.74723878 -0.01091872  1.92624299
 -2.36719553  0.09884489  2.03880464  0.3127398  -1.26295423  0.08871769
  0.60524821 -0.15227317  1.83550755 -3.13510034 -0.34363606  0.9159489
 -1.43152284 -0.68733349 -0.5537071   1.53077008 -2.57299019  0.36750835
  0.03768743  1.39099913  1.49898925 -1.42211631  1.58984028 -0.82049406
  2.26261338 -0.47822612 -0.40814039  1.19237115 -0.26392994 -0.85817118
 -2.2287237   2.44906459 -0.72464974 -1.87708791  0.48131874  0.41217291
  0.26653417 -0.47320362 -0.35557935 -1.69481858  2.44334753  0.74464318
 -2.84408642  1.27512914 -0.017323   -1.83492618 -0.10480775  3.21341275
 -1.08281818 -1.44101704  1.26697173  0.15184536  0.70048668  0.16128129
 -0.55318241 -0.31112269  1.54874999 -0.11032858 -0.95356757 -0.10500003
  0.45196014 -0.62578592 -1.53028011  0.62663416 -0.49818266  0.38987972
  0.67250286 -0.53698734  1.28094183  0.24819322  0.25228437 -0.30309929
  1.97321415 -0.30735646 -1.53969154  1.12701784  0.11793916  0.0543041
 -0.46849866  0.09761009 -1.51004807  0.6306688   0.73295614 -2.27999764
  1.24754945  0.67398116 -1.26592654 -0.37405361  1.73051704 -1.83134927
 -1.14560668  2.41844879 -2.1972351  -0.16991387  0.98790916  0.13618129
 -0.74554222  0.57910975  0.8391428  -1.68982647  2.26414889  0.37830795
 -0.67514601 -0.56549655  0.48847833  0.27876414  0.32154865  1.25605606
 -2.35992556  0.20263144 -0.53657625 -0.1468368   0.38406374  0.44287562
 -0.74288482 -1.39025794  1.24823888  0.09916711  0.9914596  -1.01960184
 -0.555636    0.40884663  0.26358068  1.06619694 -0.48732893  1.68986648
 -0.39922758 -0.13361676 -0.28209175 -0.19515669  0.89949303 -0.90650173
 -0.26864915 -2.4824621   1.54784148  0.53948448 -0.25789394 -0.50159939
 -1.34539936  1.52122823 -1.28021888  1.97693525  0.33835899  0.84861753
 -0.28580302 -1.14101725  2.28492715 -0.15872606  0.73598089 -3.65178828
  0.31673453  0.88673944 -1.04152359  1.29096748 -1.226068    1.04941052
 -2.42914133  0.42156119  1.28723457  0.19663013  1.20851639 -1.4204899
  1.40790844 -2.37153051  1.87932093 -0.15352219 -1.14613241  0.7959744
 -1.97018239  1.49923224 -1.31298629  1.66190497 -2.063282   -0.13170104
  1.83752804  0.21730412  2.13318616 -0.43832426  1.08827652 -2.98104791
  0.4286207   1.34720304 -1.02573199  0.25595686 -1.55738446  0.93984216
 -2.06288254  0.72733908 -0.34450551  0.26380071  0.53436441 -1.3321137
  2.07038061  0.03108279  1.98016263 -2.1927902   0.3645062   0.85023249
 -1.61883038  0.89106295 -1.1415652   0.91665095 -1.4127007   0.39769579
  0.56088305  1.55554248  0.47525718 -2.55085632  2.85826982 -1.27815487
  1.14273352  0.38662133 -1.28269646  0.43086785 -1.43298732  0.6965344
 -0.90327023  1.26387946 -0.61233305 -0.174021    0.09910602 -0.26505906
  2.47106106  0.40779554  0.79778609 -1.09951995  1.03944574 -0.18798719
 -0.59360861  1.27492692 -1.91403799 -0.90316589 -0.55142006  0.30027707
 -0.79725678  0.76434286 -0.22954343 -0.24814066  1.05009068 -1.23485443
  0.93283384 -0.34947841 -0.70314712  0.21755862  0.03320248  0.44181187
 -0.80181721  0.66384721 -2.75930063  0.07698611  0.70236514 -0.02239141
  0.72902157 -0.24415311  0.1826949  -1.15697855  0.23453216  0.03860282
  1.13587753 -0.03266215 -0.75498006 -0.16954901  0.22533164  0.37443543
 -0.25105086  0.71935608 -0.34317277  0.04691082  1.38414939  0.01119389
 -0.49946384  0.57299072]

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.

import numpy as np  
from scipy.linalg import toeplitz 
n = 200
m = 500
A = np.random.normal(0, 1, (n, m))
B = toeplitz([np.random.randint(0,10) for i in range(m)])

A2 = np.linalg.norm(A, 'fro')  
B2 = np.linalg.norm(B, np.inf)
B_lar = np.linalg.norm(B, 2)  
B_sma = np.linalg.norm(B, -2)  

print(A2)
print(B2)
print(B_lar)
print(B_sma)

输出如下:

315.60883018142715
2374.0
2312.772676799252
0.15646103735605557

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.

import numpy as np  
from scipy.linalg import toeplitz
import time
n = 200
m = 500

Z = np.random.standard_normal((n, n))
u = np.ones(n)
v = np.zeros(n)
pre_norm = 0
norm = 0
times = 0
 
begin = time.clock()
while(True):
    v = np.dot(Z, u)
    pre_norm = norm
    norm = np.linalg.norm(v)
    u = v / norm
    times += 1
    if abs(pre_norm - norm) < 0.0001:
        break;
end = time.clock()
      
print("The largest eigenvalue:", norm)  
print("Corresponding eigenvector of Z:", u)
print("The number of iterations:" + str(times))
print("computation time when varying n:" + str(end - begin))  

输出如下:

The largest eigenvalue: 15.200900929346451
Corresponding eigenvector of Z: [-1.14731895e-02  9.42801461e-03 -4.04150247e-02  9.81546599e-02
  3.52152633e-02 -4.08718447e-02 -2.95951376e-02  3.93807870e-02
  1.39358709e-01 -1.32021601e-01 -1.44349578e-01 -4.51490068e-02
  9.51033349e-02  4.71623726e-02  1.13235365e-01  6.26852463e-02
 -3.56166041e-02  6.29540111e-02  5.42150122e-02  2.23907266e-02
 -5.52446196e-02  7.69890409e-02  1.64605084e-02 -1.87844950e-02
  1.21030001e-01 -2.65989590e-02  1.35470106e-02  3.89178506e-02
 -4.17412216e-02  9.26525008e-03  8.25676715e-02  6.71128335e-02
  1.14204924e-01  5.86248021e-02 -1.11327780e-01 -5.81774182e-02
 -8.59467401e-02 -8.87568329e-02 -7.85659847e-02  5.10298141e-02
 -9.68843831e-02  6.71582436e-02  4.13938531e-02  8.91483179e-02
 -1.23786452e-01 -2.97610416e-02  4.46252134e-02  5.81759486e-02
 -1.76586238e-01 -1.53018604e-02  1.54390064e-02  4.02398644e-02
  4.29025575e-02 -9.22062607e-02 -2.99578283e-02  2.11115518e-02
  1.34826503e-02 -9.92132979e-02 -3.41721527e-02  4.13765430e-02
 -1.41104890e-01 -7.73876725e-02 -9.72115593e-02  2.14377271e-02
  6.94090409e-02  1.49404842e-02  8.19133591e-02 -1.61907357e-01
 -4.29362657e-02 -8.09238358e-02  4.61278491e-02  5.93444162e-02
 -4.33299497e-02  1.02202985e-01 -2.09846961e-02 -4.99593730e-02
 -1.29381050e-01 -2.81452306e-02  1.13295288e-02  5.45862897e-03
 -3.13343705e-02  4.33865599e-02 -2.01847366e-02  4.00688664e-02
  1.73723213e-02  6.75257373e-03  1.33495749e-01  4.68714464e-02
 -2.41090157e-01  5.40500673e-02 -6.06024347e-02 -4.85938900e-02
  4.26997388e-02 -9.40615908e-02  6.17413974e-02  9.54825826e-02
  9.17930969e-02  6.15821339e-02 -8.56989272e-02 -1.84444627e-02
 -4.20425616e-02 -3.76304070e-02  1.87121933e-02  7.02431720e-02
 -4.79581906e-02  7.05911277e-02 -1.03288683e-01 -3.42367249e-02
 -1.18996613e-01  3.14566628e-02 -6.34884640e-02 -2.15387709e-01
  1.32810814e-02  1.13452294e-01  1.04389748e-01  6.71979968e-02
 -4.36684269e-02 -1.96167919e-04 -5.99281737e-02  1.31793390e-02
 -6.53129295e-02 -8.34611090e-02 -2.65876454e-02  4.25101142e-02
 -7.70502828e-02  6.79670610e-02 -3.23646949e-02  3.86981290e-02
 -5.39490220e-02  2.92184487e-02 -3.04640871e-02  2.20306888e-02
  4.29551104e-02 -3.34750777e-02 -1.03229333e-02  1.93650503e-02
  8.71637825e-03  1.03078350e-01  1.07419862e-02  1.68272508e-02
  5.60494678e-02  5.26203271e-02 -4.34830417e-02  1.95274650e-02
  7.15958307e-02 -7.47595800e-02 -3.54054934e-02  6.66471996e-02
  8.92114090e-02 -1.15165937e-02 -1.02412039e-01  7.53313890e-02
  9.92019524e-02  2.43534342e-02  1.40054058e-01  4.30342573e-02
 -5.95339189e-02  3.97489396e-02  6.73751708e-02  8.14437730e-02
 -1.91478678e-02 -8.94141562e-02  4.90277269e-02 -2.27002768e-02
  1.72718763e-02 -1.39956589e-02 -3.52642516e-02  2.70007985e-02
  7.81086303e-02 -3.51542992e-02 -3.55865654e-02  2.11633876e-02
  1.10366834e-01  1.62505102e-01 -3.22271520e-02  2.05625980e-02
  7.08360512e-02 -7.14171038e-03 -6.11032006e-02  5.05730836e-02
  9.07097137e-02 -2.93299328e-02  6.26671309e-03  1.26285809e-02
  2.56939167e-02 -4.28000902e-02  8.99104922e-02  5.57371443e-02
  1.87344526e-02  8.54262463e-02 -9.42757367e-03  9.05770263e-02
  2.23682532e-02 -1.46415077e-01  3.58280985e-02  1.09306994e-01
 -1.21751705e-02  5.31834671e-02 -8.79505041e-02 -3.37310286e-02]
The number of iterations:121
computation time when varying n:0.007173537484538687


Exercise 9.5  Singular values

Generate an × matrix, denoted by C, where each entry is 1 withprobability 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 nand the largest singular value?

import numpy as np  
from scipy.linalg import toeplitz
import time
n = 200
m = 500

p = 0.25
C = np.random.binomial(1, p, (n, n))

singular_lar = np.linalg.norm(C, 2)  
singular_sma = np.linalg.norm(C, -2)

print("singular_lar :")
print(singular_lar)
print("singular_sma :")
print(singular_sma)

输出如下:

singular_lar :
50.524087762206875
singular_sma :
0.03127772809208633


Exercise 9.6  Nearest neighbor

Write a function that takes a value z and an array A and nds 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 nd this value manually. In particular, use brackets and argmin.

import numpy as np  
from scipy.linalg import toeplitz
import time
n = 200
m = 500

A = np.random.standard_normal(n)
z = np.random.random_sample(1)
B = A - z
print("The value of z is:")
print(z[0])
c = np.argmin(np.abs(B))
print("The element in A that is closest to z:")
print(A[c])

输出如下:

The value of z is:
0.3790872229529858
The element in A that is closest to z:
0.3835423752654716

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值