Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结
NumPy
目录
NumPy系统是Python的一种开源的数值计算扩展包。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。据说NumPy将Python相当于变成一种免费的更强大的MatLab系统。参考官网解释,
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.
NumPy is licensed under the BSD license, enabling reuse with few restrictions.
一个用python实现的科学计算包。包括:
- 一个强大的N维数组对象Array;
- 比较成熟的(广播)函数库;
- 用于整合C/C++和Fortran代码的工具包;
- 实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。
NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用,以及核心的科学计算组织如:Lawrence Livermore,NASA用其处理一些本来使用C++,Fortran或Matlab等所做的任务。
广播法则(rule)
广播法则能使通用函数有意义地处理不具有相同形状的输入。
广播第一法则是,如果所有的输入数组维度不都相同,一个“1”将被重复地添加在维度较小的数组上直至所有的数组拥有一样的维度。
广播第二法则确定长度为1的数组沿着特殊的方向表现地好像它有沿着那个方向最大形状的大小。对数组来说,沿着那个维度的数组元素的值理应相同。
应用广播法则之后,所有数组的大小必须匹配。
环境安装:
pip install numpy
2.1 常量 / Constants
2.1.1 pi常量
常量名: pi
常量值: π
2.2 函数 / Function
2.2.1 array()函数
函数调用: ndarray = np.array(matrix_list)
函数功能:生成一个ndarray格式的多维矩阵
传入参数: matrix_list
matrix_list: list类型,需要转换成矩阵的列表
返回参数: ndarray
ndarray: ndarray类型,numpy生成的矩阵
2.2.2 arange()函数
函数调用: vector = np.arange(num)
函数功能:生成一个1行n列的ndarray矩阵(一维向量)
传入参数: num
num: int类型,生成向量的数据个数(从0计算)
返回参数: vector
vector: ndarray类型,numpy生成的矩阵(一维)
2.2.3 zeros()函数
函数调用: matrix = np.zeros(shape, dtype=)
函数功能:生成一个shape形状元素为0,数据类型为dtype的矩阵
传入参数: shape, dtype
shape: tuple类型,生成的0矩阵的形状
dtype: obj类型,如np.float64/np.int32等,确定元素的数据类型
返回参数: matrix
matrix: ndarray类型,numpy生成的零矩阵
2.2.4 ones()函数
函数调用: matrix = np.ones(shape, dtype=)
函数功能:生成一个shape形状元素为1,数据类型为dtype的矩阵
传入参数: shape, dtype
shape: tuple类型,生成的1矩阵的形状
dtype: obj类型,如np.float64/np.int32等,确定元素的数据类型
返回参数: matrix
matrix: ndarray类型,numpy生成的一矩阵
2.2.5 linspace()函数
函数调用: matrix = np.linspace(start, stop, num=50, endpoint=True)
函数功能:生成一个等差矩阵
传入参数: start, stop, num, endpoint
start: int类型,等差矩阵的起始数
stop: int类型,等差矩阵的终止数
num: int类型,生成的样本数量,默认50,必须非负
endpoint: bool类型,如果为True,最后一个数包括stop,False则不包括
返回参数: matrix
matrix: ndarray类型,numpy生成的等差矩阵
2.2.6 view()函数
函数调用: new_matrix = matrix.view()
函数功能:创建一个新的矩阵,与原始矩阵共享原始数据(不共享其余信息,id不同)
传入参数: 无
返回参数: new_ matrix
new_matrix: ndarray类型,view函数生成的新矩阵
2.2.7 copy()函数
函数调用: new_matrix = matrix.copy()
函数功能:创建一个新的矩阵,完全复制,且不共享任何资源,两个无关矩阵
传入参数: 无
返回参数: new_ matrix
new_matrix: ndarray类型,copy函数生成的新矩阵
2.2.8 sin/cos()函数
函数调用: matrix = np.sin/cos(matrix)
函数功能:对矩阵的每个元素进行sin/cos操作
传入参数: matrix
matrix: ndarray类型,需要处理的矩阵
返回参数: matrix
matrix: ndarray类型,sin/cos后生成的矩阵
2.2.9 exp()函数
函数调用: new_matrix = np.exp(matrix)
函数功能:对矩阵元素进行以e为底的乘方运算(e^x)
传入参数: matrix
matrix: ndarray类型,计算矩阵
返回参数: new_matrix
new_matrix: ndarray类型,乘方运算后的矩阵
2.2.10 sqrt()函数
函数调用: new_matrix = np.sqrt(matrix)
函数功能:对矩阵元素进行以开方运算
传入参数: matrix
matrix: ndarray类型,计算矩阵
返回参数: new_matrix
new_matrix: ndarray类型,开方运算后的矩阵
2.2.11 dot()函数
函数调用: matrix = np.dot(matrix_1, matrix_2) / matrix = matrix_1.dot(matrix_2)
函数功能:将两个矩阵进行点乘运算
传入参数: matrix_1, matrix_2
matrix_1: ndarray类型,点乘矩阵1
matrix_2: ndarray类型,点乘矩阵2
返回参数: matrix
matrix: ndarray类型,点乘后生成的矩阵
2.2.12 floor/ceil()函数
函数调用: new_matrix = np.floor/ceil(matrix)
函数功能:对矩阵中的每个元素进行取整操作(floor向下,ceil向上)
传入参数: matrix
matrix: ndarray类型,计算矩阵
返回参数: new_matrix
new_matrix: ndarray类型,取整处理后的矩阵
2.2.13 argmax()函数
函数调用: index = matrix.argmax(axis=None)
函数功能:获取原矩阵的最大值/每行/列最大值所在索引
传入参数: axis
asix: bool/int类型,None则返回最大值,0/1则返回每列/行中最大值的索引
返回参数: index
index: int/ndarray类型,最大值或每行/列最大值索引矩阵
2.2.14 ravel()函数
函数调用: new_matrix = matrix.ravel(order=‘C’)
函数功能:对原矩阵进行展开(flatten)操作,变成一个单行矩阵
传入参数: order
order: str类型,确定取值方向
返回参数: new_matrix
new_matrix: ndarray类型,取整处理后的矩阵
2.2.15 hstack / vstack()函数
函数调用: new_matrix = np.hstack/vstack(tup)
函数功能:对原矩阵在水平/竖直方向进行堆叠(堆叠位置维度需要相同)
传入参数: tup
tup: tuple类型,(a, b),包括两个需要进行堆叠的矩阵a和b
返回参数: new_matrix
new_matrix: ndarray类型,堆叠处理后的矩阵
2.2.16 hsplit / vsplit()函数
函数调用: new_matrix = np.hsplit/vsplit(matrix, indices_or_sections)
函数功能:对原矩阵在水平/竖直方向进行拆分
传入参数: matrix, indices_or_section
matrix: ndarray类型,需要分割的原始矩阵
indices_or_section: int/tuple类型,int则表示需要切割的份数,tuple则表示需要切割的位置,如(3,4)表示切割位置在3h/v和4h/v之间
返回参数: new_matrix
new_matrix: list类型,切割处理后的矩阵列表,包含切割后的所有array
2.2.17 tile()函数
函数调用: new_matrix = np.tile(matrix, reps)
函数功能:对原矩阵进行铺展,原矩阵(c, d), reps=(a, b),则铺展成(a*c, b*d)结构
传入参数: matrix, reps
matrix: ndarray类型,需要进行铺展的原始矩阵
reps: tuple类型,铺展形状
返回参数: new_matrix
new_matrix: ndarray类型,铺展处理后的矩阵
2.2.18 sort()函数
函数调用: new_matrix = np.sort(matrix, axis=-1, kind=’quicksort’, order=None)
函数功能:对原矩阵按行/列进行排序
传入参数: matrix, axis, kind, order
matrix: ndarray类型,需要进行排序的原始矩阵
axis: int/bool类型,None则将矩阵展开后排列, 0则按shape的第一维度排序,1,则按第二维度,依次类推,-1则为按最后一个维度排列
kind: str类型,确定sort的排序算法,包括{‘quicksort’, ‘mergesort’, ‘heapsort’}
order: str/list of str类型,确定排序的优先级,指定的优先排序,未指定的默认排序
返回参数: new_matrix
new_matrix: ndarray类型,排序处理后的矩阵
Eg.:
1 import numpy as np 2 3 x = np.array([[range(16, 12, -1), range(12, 8, -1), range(8, 4, -1)], [range(12, 8, -1), range(8, 4, -1), range(4, 0, -1)]]) 4 print(x) 5 print(x.shape) 6 print('----------------') 7 print(np.sort(x, axis=None)) 8 print('----------------') 9 print(np.sort(x, axis=0)) 10 print('----------------') 11 print(np.sort(x, axis=1)) 12 print('----------------') 13 print(np.sort(x, axis=-1))
输出结果
[[[16 15 14 13] [12 11 10 9] [ 8 7 6 5]] [[12 11 10 9] [ 8 7 6 5] [ 4 3 2 1]]] (2, 3, 4) ---------------- [ 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 14 15 16] ---------------- [[[12 11 10 9] [ 8 7 6 5] [ 4 3 2 1]] [[16 15 14 13] [12 11 10 9] [ 8 7 6 5]]] ---------------- [[[ 8 7 6 5] [12 11 10 9] [16 15 14 13]] [[ 4 3 2 1] [ 8 7 6 5] [12 11 10 9]]] ---------------- [[[13 14 15 16] [ 9 10 11 12] [ 5 6 7 8]] [[ 9 10 11 12] [ 5 6 7 8] [ 1 2 3 4]]]
2.2.19 argsort()函数
函数调用: index_matrix = np.argsort(matrix, axis=-1)
函数功能:返回矩阵按行/列进行排序后的索引值列表
传入参数: matrix, axis
matrix: ndarray类型,需要进行排序的原始矩阵
axis: int/bool类型,None则将矩阵展开后排列, 0则按shape的第一维度排序,1,则按第二维度,依次类推,-1则为按最后一个维度排列
返回参数: index_matrix
index_matrix: ndarray类型,排序处理后的索引矩阵
2.2.20 T属性方法
方法调用: T_matrix = matrix.T
方法功能:对原矩阵进行转置操作
传入参数: 无
返回参数: T_matrix
T_matrix: ndarray类型,转置处理后的矩阵
2.3 类 / Class
2.3.1 ndarray类
类实例化:ndarray = np.array(matrix_list) / np.arange(num)
类的功能:用于生成矩阵数组
传入参数: matrix_list / num
matrix_list: list类型,包含需要构建成ndarray矩阵的数据
num: int类型,生成ndarray的数据数量
返回参数: ndarray
ndarray: ndarray类型,生成的ndarray矩阵
2.3.1.1 dtype属性
属性调用: fmt = ndarray.dtype
属性功能: 返回矩阵内数据的格式类型
属性参数: fmt
fmt: obj类型,<class ‘numpy.dtype’>
2.3.1.2 shape属性
属性调用: shp = ndarray.shape / ndarray.shape = shp
属性功能: 返回矩阵各维度长度参数 / 更改矩阵各维度长度参数
属性参数: shp
shp: tuple类型,eg. 3维矩阵返回 (x, y, z)
2.3.1.3 ndim属性
属性调用: rank = ndarray.ndim
属性功能: 返回矩阵的秩
属性参数: rank
rank: int类型,矩阵的秩
2.3.1.4 size属性
属性调用: size = ndarray.size
属性功能: 返回矩阵的元素总数
属性参数: size
siez: int类型,矩阵的元素总数,等于shape中的元素乘积
2.3.1.5 itemsize属性
属性调用: iSize = ndarray.itemsize
属性功能: 返回矩阵的元素总数
属性参数: size
siez: int类型,数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsize属性值为8(=64/8),又如,一个元素类型为complex32的数组item属性为4(=32/8)
2.3.1.6 data属性
属性调用: mem = ndarray.data
属性功能: 返回包含实际数组元素的缓冲区
属性参数: mem
siez: obj类型,type: <class ‘memoryview’>,value: <memory at 0x00000X00X>
2.3.1.7 reshape方法
函数调用: new_matrix = matrix.reshape (shape)
函数功能:返回一个基于原始数据重排的矩阵
传入参数: shape, order
shape: tuple/int类型,用于确定新矩阵的维度参数,若一个元组某个维度为-1,则该维度由其余维度计算得出
order: str类型,‘C’表示将原始数据根据行顺序重排列,‘F’表示根据列顺序
返回参数: new_matrix
new_matrix: ndarray类型,重排后的矩阵
2.3.1.8 min/max()方法
函数调用: value = matrix.min/max()
函数功能:返回矩阵中的最小/最大值
传入参数: 无
返回参数: value
value: int/str类型,矩阵中的最小/最大值
2.3.1.9 sum()方法
函数调用: value = matrix.sum(axis=None)
函数功能:返回矩阵的求和值
传入参数: axis
axis: int/None类型,None时返回矩阵所有元素之和,axis=0返回第一个维度求和值,axis=1第二个维度
返回参数: value
value: int/str类型,矩阵中的求和值
2.4 模块 / Module
2.4.1 random模块
2.4.1.1 常量
Pass
2.4.1.2 函数
2.4.1.2.1 random()函数
函数调用: rdm = np.random.random(shape)
函数功能:返回一个shape形状的随机元素矩阵
传入参数: shape
shape: tuple类型,随机矩阵的维度形状
返回参数: rdm
rdm: ndarray类型,返回的随机值矩阵
2.4.1.2.2 ranint()函数
函数调用: rdi = np.random.randint(start, stop, num)
函数功能:返回一个随机整数列表
传入参数: start, stop, num
start: int类型,随机数起始值
stop: int类型,随机数终止值
num: int类型,随机数数量
返回参数: rdi
rdi: list类型,返回的随机值列表
下面的代码提供了一些 numpy 基本函数的使用方法,
完整代码
1 import numpy as np 2 3 # Change type, all the elements in array should be in same type. 4 matrix = np.array([1, 2, 3, 4]) 5 print(matrix, matrix.dtype) # [1 2 3 4] int32 6 matrix = np.array([1, 2, 3, 4.0]) 7 print(matrix, matrix.dtype) # [ 1. 2. 3. 4.] float64 8 matrix = np.array([1, 2, 3, '4']) 9 print(matrix, matrix.dtype) # ['1' '2' '3' '4'] <U11 10 11 # Basic slice operation 12 matrix = np.array([[1, 2, 3, 4], 13 [5, 6, 7, 8], 14 [9, 10, 11, 12], 15 [13, 14, 15, 16]]) 16 # Get number certain element(7), [row, column] 17 print(matrix[1,2]) # 7 18 # Get all row and column 3 19 print(matrix[:, 2]) # [3 7 11 15] 20 # Get certain row and column, not contains the max number 21 print(matrix[0:3, 0:2]) ''' [[ 1 2] 22 [ 5 6] 23 [ 9 10]] ''' 24 25 # Get the number by certain list 26 # (0,0),(1,1),(2,2),(3,3) 27 print(matrix[[0, 1, 2, 3], [0, 1, 2, 3]]) # [1 6 11 16] 28 # Use range method can make it too 29 print(matrix[range(4), range(4)]) # [1 6 11 16] 30 31 # Operation to array will act to each element 32 equal_to_seven = (matrix == 7) 33 print(equal_to_seven) ''' [[False False False False] 34 [False False True False] 35 [False False False False] 36 [False False False False]] ''' 37 38 # equal_to_seven can be use as an index, True/False list also can do so 39 print(matrix[equal_to_seven]) # [7] 40 get_certain_row = [False, True, True, False] 41 print(matrix[get_certain_row]) ''' [[ 5 6 7 8] 42 [ 9 10 11 12]] ''' 43 # Fetch row where the certain number in 44 matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) 45 print(matrix[matrix[:, 2] == 7]) # [[5 6 7 8]] 46 47 vector = np.array([5, 10, 15, 20]) 48 # Find the element equal to ten and five(impossible) 49 equal_to_ten_and_five = (vector == 5) & (vector == 10) 50 print(vector[equal_to_ten_and_five]) # [] 51 # Find the element equal to ten or five 52 equal_to_ten_or_five = (vector == 5) | (vector == 10) 53 print(vector[equal_to_ten_or_five]) # [5 10] 54 # Change equal one to other number 55 vector[equal_to_ten_or_five]=50 56 print(vector) # [50 50 15 20] 57 58 # Change the type of array 59 vector = np.array(['1', '2', '3']) 60 print(vector.dtype, vector) # <U1 ['1' '2' '3'] 61 new_vector = vector.astype(int) 62 print(new_vector.dtype, new_vector) # int32 [1 2 3] 63 64 # Get the min number 65 # Use print(help(np.array)) 66 vector = np.array([5, 10, 15, 20]) 67 print(vector.min()) # 5 68 69 # Calculate the sum in certain dimension 70 # 1 for cal sum in row, 0 for cal sum in column 71 matrix = np.array([[1, 2, 3, 4], 72 [5, 6, 7, 8], 73 [9, 10, 11, 12], 74 [13, 14, 15, 16]]) 75 print(matrix.sum(axis=1)) # [10 26 42 58] 76 print(matrix.sum(axis=0)) # [28 32 36 40]