【第十一周】numpy作业

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A is n*m matrix and B is m*m matrix, for n=200, m=500.

Exercise 9.1:Matrix operation

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

from scipy.linalg import toeplitz
import numpy as np


###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))

def ex_one(A,B,c):
             print("Matrix A:\n",A)
             print("Starting to compute.......")
             print("Matrix A+A:\n",A+A)
             print("Matrix A.At:\n",A@A.T)
             print("Matrix At.A:\n",A.T@A)
             print("Matrix A.B:\n",A@B)
             
             print("Matrix A.(B-c*I):\n",A@(B-c*np.eye(50)))
A = np.random.randn(20,50)
B = toeplitz(list(range(1,51)))
ex_one(A,B,2)

运行截图(部分):



Exercise 9.2:Solving a linear system

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

from scipy.linalg import toeplitz
import numpy as np

###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))


def ex_two(B,b):
    print("Bx=b solving for x....")
    print(np.linalg.solve(B,b))
    
A = np.random.randn(20,50)
B = toeplitz(list(range(1,51)))
b = np.array(list(range(1,51)))
ex_two(B,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.

from scipy.linalg import toeplitz
import numpy as np

###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))


def ex_three(A,B):
    print("The Frobenius norm of A:",np.linalg.norm(A))
    print("The infinity norm of B:",np.linalg.norm(B,np.inf))
    u,s,vh=np.linalg.svd(B)
    print("Max singular value:",max(s))
    print("Min singular value:",min(s))
    
A = np.random.randn(20,50)
B = toeplitz(list(range(1,51)))
b = np.array(list(range(1,51)))
ex_three(A,B)

运行截图:



Exercise 9.4: Power iteration

Generate a matrix Z× 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. 


from scipy.linalg import toeplitz
import numpy as np
import random
import time

###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))


def ex_four(A):
    begin = time.clock()
    esp = 0.000001
    count = 0
    
    b_k = np.random.rand(A.shape[1])
    a1=8
    a2=1
    
    while( abs(a2-a1)>esp ):
       b_k1 = np.dot(A,b_k)
       a1 = np.linalg.norm(b_k)
       
       b_k = b_k1/a1
       a2 = np.linalg.norm(b_k)
       
       count +=1

    end = time.clock()
    print(A)
    print("Iteration times:",count)
    print("time:",end-begin)
    
A = np.random.randn(20,20)
ex_four(A)

运行截图:



Exercise 9.5: Singular values
Generate an × matrix, denoted by C, where each entry is 1 with probability and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of  
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? 

from scipy.linalg import toeplitz
import numpy as np
import random

###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))


def ex_five(n,p):
    x = []
    for i in range(0,n):
        x.append([])
        for j in range(0,n):
           r=random.random()
           if(p<r):
             x[i].append(1)
           else:
             x[i].append(0)
    C = np.array(x)
    print("C is:\n",C)
    u,s,vh=np.linalg.svd(C)
    print("Max singular value:",max(s))


n=input("n=")
p=input("p=")
ex_five(int(n),float(p))

运行截图:


由不同的n、p输入,发现当n变大,p变小时max sigular value变大。

Exercise 9.6: Nearest neighbor
Write a function that takes a value and an array and finds the element in 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. 

from scipy.linalg import toeplitz
import numpy as np

###简化200*500 为20*50
###A = np.random.randn(200,500)
###B = toeplitz(list(range(1,501))


def ex_six(A,z):
    sub = [abs(A[i]-z) for i in range(0,A.size)]
    s=np.argmin(sub)
    return A[s]
    
A = np.random.randn(20,50)
B = toeplitz(list(range(1,51)))
b = np.array(list(range(1,51)))
C = np.array([1,2,3,4,7,8,9])
print("closest value:",ex_six(C,5))

运行截图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: numpy、pandas和matplotlib是Python中常用的数据处理和可视化工具库。它们可以帮助我们更方便地进行数据分析和展示。 numpy是Python中的一个科学计算库,提供了高效的数组操作和数学函数。它可以用来处理各种类型的数据,如数字、字符串、图像等。 pandas是Python中的一个数据分析库,提供了高效的数据结构和数据分析工具。它可以用来处理各种类型的数据,如表格、时间序列等。 matplotlib是Python中的一个数据可视化库,提供了各种绘图工具和图形界面。它可以用来绘制各种类型的图表,如折线图、散点图、柱状图等。 在使用这些库时,我们需要先导入它们的模块,然后调用相应的函数来完成我们需要的操作。例如,我们可以使用numpy中的array函数来创建一个数组,使用pandas中的read_csv函数来读取一个CSV文件,使用matplotlib中的plot函数来绘制一个折线图等。 总之,numpy、pandas和matplotlib是Python中非常重要的数据处理和可视化工具库,掌握它们的使用方法对于进行数据分析和展示非常有帮助。 ### 回答2: numpy是Python数据科学中最基本的库之一,它提供高效的数组和矩阵运算,使处理数字计算变得更加容易。numpy的数组在数学计算和数据科学方面被广泛使用,这些应用包括线性代数、数组操作、统计学和图像处理等。 pandas是Python中用于数据处理和数据分析的重要库之一,它被用来读取、跟踪和管理大量数据。pandas提供高效的DataFrame和Series数据结构,使得数据分析更加简洁、易于阅读和理解。pandas在处理数据时非常方便,可以轻松地执行数据过滤、排序、聚合和连接。 matplotlib是Python中用于数据可视化和科学绘图的库,它提供Python中最灵活和最可定制的绘图选项之一。使用matplotlib,用户可以生成各种不同的图形展示,包括线图、柱状图、散点图和饼图等,同时matplotlib还允许用户进行自定义调整、注释和标记。 以上三个库的组合提供了Python中非常强大的数据科学生态系统,适合用于数据处理、分析和可视化。这些库的使用也非常广泛,包括数据科学领域、金融领域、物理学领域和天文学领域等。无论是初学者还是专业人士,都可以从中获益和发挥作用。 ### 回答3: numpy、pandas、和matplotlib是Python中非常常用的三个模块,它们能够方便地进行数据处理和数据可视化。 首先,我们来谈一下numpynumpy是Python中处理科学数据的基础模块。通过使用numpy,我们能够方便地进行矩阵运算、数值分析、信号处理、图像处理等科学计算,从而更好地处理数据。在numpy中最重要的是其多维数组对象ndarray,通过使用ndarray,我们可以对数组中的数据进行高效的数学运算,例如加减乘除等。另外,numpy还提供了一些方便的函数,例如随机数生成函数、拷贝和转换函数等,使得使用numpy进行科学计算变得更加得心应手。 接下来,我们来说一下pandas。Pandas是一个Python工具包,主要用于数据处理和数据分析。Pandas中的两个主要数据结构是Series和DataFrame,它们可以方便地处理各种类型的数据,从而使得数据清洗和数据分析变得更加高效和简单。使用Pandas,我们可以轻松读取和导入数据,处理和过滤数据,清理和填充数据,以及进行数据分析和可视化。Pandas提供了很多工具,例如数据重构、分组、聚合、切片、索引、透视以及时间序列等,使得数据处理和分析变得更加高效和灵活。 最后,我们来看一下matplotlib。Matplotlib是一个Python中常用的数据可视化工具。它提供一系列高质量的数据可视化图形,例如折线、散点、柱形、饼图、等高线、瀑布图等等。通过使用matplotlib,我们可以快速地将获得的数据进行可视化,从而更容易地发现其中的规律和特点。Matplotlib提供了丰富的参数设置和样式选项,使得生成的图形可以更加美观和专业。同时,matplotlib还提供了集成的图像显示和保存函数,可以轻松地将图形保存在不同的格式中,例如PNG、PDF、SVG、EPS等格式。 总之,numpy、pandas、和matplotlib都是处理和分析数据的重要工具,它们相互补充,共同构成了Python中的数据分析生态系统。通过掌握这三个工具,我们可以轻松地处理和分析各种类型的数据,从而更好地理解数据和发现数据规律。为了掌握这三个工具,我们需要认真学习它们的基本语法和常用函数,并勤加练习。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值