Part 1 安装NumPy包,以VScode为例:
打开Visual Studio Code,选取任一python项目文件,运行该文件,在终端(Terminal)中输入cd + 空格 + Python安装目录下Scripts文件的路径,例如我的Python安装在D:\Python\app\Scripts,因此我需要输入:cd D:\Python\app\Scripts
输入后按下回车得到 D:\Python\app\Scripts > 输入 .\pip install numpy 再按下回车即可
1 PS D:\PythonWork> cd D:\Python\app\Scripts #标红加粗部分即为输入部分 因为我已经安装过因此显示的为以下这些,若未安装过显示下载进度条以及包的大小
2 PS D:\Python\app\Scripts> .\pip install numpy
3 Requirement already satisfied: numpy in d:\python\app\lib\site-packages (1.23.4)
4 WARNING: You are using pip version 22.0.4; however, version 22.3 is available.
5 You should consider upgrading via the 'D:\Python\app\python.exe -m pip install --upgrade pip' command.
6 PS D:\Python\app\Scripts>
Part 2 numpy数组
创建数组
Python中没有数组和矩阵的内置类型,但可以将列表近似为数组和矩阵,如一个两行三列的矩阵我们可以表示为:
A = [[1,2,3],
[4,5,6]]
而当导入numpy包后,为了进行数学运算,我们需要将数据转化为numpy数组先
在numpy包中有整数型,浮点型和复数型数组,创建方法为,在numpy包中,numpy数组中的元素默认为64位浮点型:
arrayName = numpy.array(list, dtypt = type) #数组\矩阵名 = numpy.array(列表, dtype = 数组类型) 其中dtype可省略,若使用则为自定义类型
实例01:创建数组
1 import numpy
2 A = [[1,2,3],
3 [4,5,6]]
4 a = numpy.array(A) #创建数组a
5 b = numpy.array([1.2, 2.0, 3.1]) #创建数组b
6 c = numpy.array(A,dtype = complex) #创建复数数组c
7 print(a,'\n\n',b,'\n\n',c) #输出数组a,b,c
8 print(type(a),'\n',type(b),'\n',type(c)) #输出数组a,b,c的类型
输出后得到:
1 [[1 2 3]
2 [4 5 6]]
3
4 [1.2 2. 3.1]
5
6 [[1.+0.j 2.+0.j 3.+0.j]
7 [4.+0.j 5.+0.j 6.+0.j]]
8
9 <class 'numpy.ndarray'>
10 <class 'numpy.ndarray'>
11 <class 'numpy.ndarray'>
特殊数组矩阵 0、1数组,单位矩阵:
在导入numpy包后,我们可以使用zeros和ones函数创建成员全部为0或者为1的数组
ArrayName = numpy.zeros((row,column)) #row为行数,column为列数
ArrayName = numpy.ones((row,column))
ArrayName = numpy.eye((row,column))
实例02:创建0,1数组,单位矩阵
1 import numpy
2 A = [[1,2,3],
3 [4,5,6]]
4 a = numpy.array(A) #创建整形数组a
5 b = numpy.array([1.2, 2.0, 3.1]) #创建浮点型数组b
6 c = numpy.array(A,dtype = complex) #创建复数数组c
7 print(a,'\n\n',b,'\n\n',c,'\n') #输出数组a,b,c
8 print(type(a),'\n',type(b),'\n',type(c)) #输出数组a的类型
9 zeors_array = numpy.zeros((2,3))
10 print(zeors_array,'\n\n')
11 ones_array = numpy.ones((2,5),dtype = numpy.int32)
12 print(ones_array) #生成3x3的单位矩阵
13 C = numpy.eye(3,3)
14 print('\n\n\n',C)
15
16 '''
17 输出结果:
18 [[1 2 3]
19 [4 5 6]]
20
21 [1.2 2. 3.1]
22
23 [[1.+0.j 2.+0.j 3.+0.j]
24 [4.+0.j 5.+0.j 6.+0.j]]
25
26 <class 'numpy.ndarray'>
27 <class 'numpy.ndarray'>
28 <class 'numpy.ndarray'>
29 [[0. 0. 0.]
30 [0. 0. 0.]]
31
32
33 [[1 1 1 1 1]
34 [1 1 1 1 1]]
35
36
37
38 [[1. 0. 0.]
39 [0. 1. 0.]
40 [0. 0. 1.]]
41
42
43 '''
使用arange()函数和reshape()函数创建数组:
arange(n)函数可以生成一行n列的递增列表,其列表元素会依次递增1直到最后一个n-1,类似于range函数,只不过生成的是一个数组而不是依次生成单个数字
reshape(n,m)函数配合arange()可以生成指定形状(n行m列)的数组
实例03:使用arange()函数和reshape()函数创建数组
import numpy
A = numpy.arange(4) #生成一行四列元素为0-3的列表
print('A=',A)
B = numpy.arange(4).reshape(2,2) #生成二行二列元素为0-3的列表
print('\n\nB=',B)
'''输出结果:
A= [0 1 2 3]
B= [[0 1]
[2 3]]
'''
数组、矩阵运算
矩阵的加法,导入numpy包后,我们可以直接使用 ‘ + ’ 进行矩阵相加,但是必须是相同行数相同列数的两个矩阵
而乘法分为两种,一种是元素数组乘法,即每个元素对应相乘,同样要求数组行数列数相同,另一种为左乘,即矩阵左乘,使用函数dot()
A.dot(B) 即矩阵A左乘矩阵B
实例04:进行数组和矩阵的加法和乘法运算
1 import numpy
2 #生成矩阵
3 A = numpy.array([[2,4],[5,-6]])
4 B = numpy.array([[9,-3],[3,6]])
5 C = numpy.array([[3,6,7],[5,-3,0]])
6 D = numpy.array([[1,1],[2,1],[3,-3]])
7 #加法
8 Sum = A + B
9 #数组乘法
10 mult1 = A * B
11 #矩阵乘法
12 mult2 = C.dot(D)
13 print('A+B:\n',Sum,'\n\nA数乘B:\n',mult1,'\n\nC左乘D:\n',mult2)
运行后得到以下输出:
A+B:
[[11 1]
[ 8 0]]
A数乘B:
[[ 18 -12]
[ 15 -36]]
C左乘D:
[[ 36 -12]
[ -1 2]]
矩阵转置
在numpy包中,可以使用transpose函数计算矩阵的转置
ArrayNmae.transpose()
实例05:
生成一个矩阵,并对其进行转置操作
1 import numpy
2 #生成矩阵
3 A = numpy.array([[1,1],[2,1],[3,-3]])
4 #对矩阵进行转置
5 B = A.transpose()
6 print('原矩阵:\n',A,'\n\n转置后:\n',B)
运行后得到以下结果
原矩阵:
[[ 1 1]
[ 2 1]
[ 3 -3]]
转置后:
[[ 1 2 3]
[ 1 1 -3]]