Numpy的6道习题

首先,生成一个服从标准正太分布的200*500矩阵A,并生成一个500*500的teoplitz矩阵B,标准正太分布矩阵可通过输出均值和方差来确定。

import numpy as np  
import time  
seed = np.int64(time.time())  
print(seed)  
np.random.seed(seed) 
A = np.random.normal(size=[200, 50])  
print(np.mean(A))  
print(np.var(A)) 

def toeplitz(X, Y):  
  
    matrix = np.zeros([len(X), len(Y)])  
  
    for i in range(len(X)):  
        for j in range(len(Y) - i):  
            matrix[j][j+i] = Y[i]  
        for j in range(i, len(X)):  
            matrix[j][j-i] = X[i]  
  
    return matrix 
    
    
X = np.random.normal(size=[500])  
Y = np.random.normal(size=[500])  
B = toeplitz(X, Y)  
print(B)
1. Matrix operations

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

A_add_A = A + A  
A_mul_AT = np.dot(A, A.T)  
AT_mul_A = np.dot(A.T, A)  
A_mul_B = np.dot(A, B)  
  
  
def A_B_I(A, B, i):  
    C= B - i * (np.eye(m)) 
    return np.dot(A,C)
2. Solving a linear system
Generate a vector b with m entries and solve Bx = b
def solve(B):  
    b = np.ones((m, 1))
    x = np.linalg.solve(B, b)  
    return x  


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 .
def norm_and_singularity(A, B):  

    AF = np.linalg.norm(A, 'fro')  
    print(AF)  
    BF = np.linalg.norm(B, np.inf)  
    print(BF)  
    large= np.linalg.norm(B, 2)  
    small = np.linalg.norm(B, -2)  
    print("the largest singular:", large)  
    print("the smallest singular:", small)  


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?

  1. def eigen(A, B):  
  2.     
  3.     Z = np.random.standard_normal((200, 200))  
  4.     vector = np.ones(200)  
  5.     value = 0  
  6.     k = np.zeros(200)  
  7. temp = 0 
  8.      while(True):  
  9.        
  10.         k = np.dot(Z, vector)  
  11.       
  12.         value_t = value  
  13.         value = np.linalg.norm(k)  
  14.      
  15.         vector = k / value 
  16.         temp ++  
  17.         if(abs(value_t - value) < 0.005):  
  18.             break;  
  19.  
  20.       
  21.     print("the largest eigenvalue is:", value)  
  22.     print("the corresponding eigenvector is:", vector)  
  23.      print("The number of iterations is:", temp)  
  24.  

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?
def singular(A, B):  
    p = 0.5  
    C = np.random. binomial(1, p, (200, 200))  
    large = np.linalg.norm(C, 2)  
    small = np.linalg.norm(C, -2)  
    print("the largest singular is:", large)  
    print("the smallest singular:", small)  
    print("n * p:", n*p)  
    print("the largest singular is equal to 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, B):  
   
    z = 2 
    B, C = A[A>z], A[A<=z]  
 ceil, floor = 0, 0
 if(len(B)):  
        ceil = np.argmin(B)  
    else:  
        closest= C[np.argmax(C)] 
if(len(C)):  
        floor = np.argmax(C)  
    else:  
        closest= B[ceil]  
          
    if(abs(B[ceil]-z) < abs(C[floor]-z)):  
        closest= B[ceil]  
    else:  
        closest= C[floor] 
print("the closest value is:", closest)  







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值