Numpy 课后练习

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


Exercise 9.1: Matrix operations

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

import numpy as np
import scipy.linalg as lp

n = 200
m = 500

A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))

print('A + A')
print(A + A)
print()
print('AAT')
print(np.dot(A,A.T))
print('ATA')
print(np.dot(A.T,A))
print()
print("AB")
print(np.dot(A,B))
print()

def n1(A,B,k):
    t = np.eye(m)
    print(np.dot(A , B - k * t))

n1(A,B,5)

Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b .
import numpy as np
import scipy.linalg as lp

n = 200
m = 500

A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))

b = np.random.normal(size=m)
print(np.linalg.solve(B,b))
Exercise 9.3: Norms
Compute the Frobenius norm of A : k A k F and the infinity norm of B : k B k 1 . Also find the largest and

smallest singular values of B.

import numpy as np
import scipy.linalg as lp

n = 200
m = 500

A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))

print(lp.norm(A, 'fro'))
print(lp.norm(B, np.inf))
temp = lp.svdvals(B)
print('Max:')
print(max(temp))
print('Min:')
print(min(temp))
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
import scipy.linalg as lp
import time

n = 200
m = 500
iteration = 0 
max_svd = 0
me = np.random.randn(n)
Z = np.random.normal(size=(n, n))  
 
begin = time.clock()
while 1:    
    n = max_svd  
    v = Z.dot(me)  
    max_svd = np.max(v)  
    me = np.true_divide(v, max_svd)
    iteration += 1
    if(abs(max_svd - n) < 0.001):
        break
end = time.clock()

print('Used Time: ', end = '')
print(end - begin)
print(max_svd)
print(me)
print(iteration)
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?

import numpy as np
import scipy.linalg as lp
import math

n = 200
m = 500

p = 0.5
C = np.random.binomial(1, p, (n, n))
U, svd, Vh = lp.svd(C)
print("Max SVD:\n\t", end = '')
print(max(svd))
print("n * p:\n\t", end = '')
print(n * p)
print("They are almost equal.")

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 .

import numpy as np
import scipy.linalg as lp

n = 200
m = 500

A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))

def n6(A, z):
    return A[np.argmin([abs(A[i] - z) for i in range(len(A))])]

print(n6([np.random.normal(0, 10) for i in range(1000)], 1))



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值