1. Matrix operations:
Calculate A + A, AA>,A>A and AB. Write a function that computes A(B−λI) for any λ.
代码如下:
import numpy as np
A = np.random.randint(0,10,size=(2,5))
B = np.random.randint(0,10,size=(5,5))
print("A = ")
print(A)
print("A.T = ")
print(A.T)
print("A+A = ")
print(A+A)
print("A*A.T = ")
print(np.dot(A,A.T))
print("A*B = ")
print(np.dot(A,B))
def compute(lumda,A,B):
I=np.eye(5,dtype=int)
ans=np.dot(A,(B-lumda*I))
print(ans)
return ans
print("A ( B - lumda * I ) =")
compute(2,A,B)
运行结果:
2. Solving a linear system :
Generate a vector b with m entries and solve Bx = b.
代码如下:
import numpy as np
A = np.random.randint(0,10,size=(2,5))
B = np.random.randint(0,10,size=(5,5))
b = np.random.randint(0,10,size=(5,1))
x = np.linalg.solve(B,b)
print("B = ")
print(B)
print("b = ")
print(b)
print("x = ")
print(x)
print("验算: Bx = ")
print(np.dot(B,x))
结果如下:
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
A = np.random.randint(0,10,size=(2,5))
B = np.random.randint(0,10,size=(5,5))
print("A = \n",A)
print("B = \n",B)
print('the fro nurm of A is ',np.linalg.norm(A,'fro'))
print('the inf nurm of B is ',np.linalg.norm(B,np.inf))
u, s, vh =np.linalg.svd(B,full_matrices=True)
max_singular=max(s)
min_singular=min(s)
print('max singular value = ',max_singular)
print('min singular value = ',min_singular)
结果如下:
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
import time
for n in (10,20,50,100):
Z = np.random.normal(5,1,n*n).reshape(n,n)
def powerit(Z,n):
start = time.clock()
u = np.random.normal(5,1,n)
last = 100
lam = 0
cnt = 0
while abs(last-lam) > 0.000001:
cnt = cnt+1
v = Z@u
big = -1
last = lam
for tem in v:
if abs(tem)>big:
lam = tem
big = abs(tem)
u = v/lam
end = time.clock()
return lam, cnt, end-start
print(powerit(Z,n))
运行结果:
设置精度为1e-6,n分别为10,20,50,100时,迭代的次数变化的不大。
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?
代码如下:
import numpy as np
for n in (10,20,50,100):
for p in (0.1,0.3,0.5,0.7,0.8):
print("n = %d, p = %.2f" % (n,p))
C = np.random.binomial(1,p,(n,n))
u, s, vh = np.linalg.svd(C,full_matrices=True)
max_singular = max(s)
print('the max singular value is ',max_singular)
结果如下:
n和p与最大特征值都存在正相关的关系。
在n比较小的时候,最大特征值对p=0.5附近p的变化比较敏感,对p接近1或0时候的变化不敏感。
在n比较大的时候,最大特征值随p的变化接近正比例变化。
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.
代码如下:
def Nearest(A,z):
B = A - z
idx = np.argmin(np.abs(B))
return A[idx]
分析如下:
B矩阵每个元素的绝对值表示A中相对应的每个元素与z的距离,然后通过argmin函数获得B矩阵中最小元素的下标,再将其在矩阵A中对应的值返回。