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]]]