Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结
NumPy
目录
1 关于numpy / About 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的数组沿着特殊的方向表现地好像它有沿着那个方向最大形状的大小。对数组来说,沿着那个维度的数组元素的值理应相同。
应用广播法则之后,所有数组的大小必须匹配。
2 numpy库 / numpy Library
环境安装:
- 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.:
- import numpy as np
- 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)]])
- print(x)
- print(x.shape)
- print('----------------')
- print(np.sort(x, axis=None))
- print('----------------')
- print(np.sort(x, axis=0))
- print('----------------')
- print(np.sort(x, axis=1))
- print('----------------')
- 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类型,返回的随机值列表
3 numpy基本操作
下面的代码提供了一些 numpy 基本函数的使用方法,
完整代码
- import numpy as np
- # Change type, all the elements in array should be in same type.
- matrix = np.array([1, 2, 3, 4])
- print(matrix, matrix.dtype) # [1 2 3 4] int32
- matrix = np.array([1, 2, 3, 4.0])
- print(matrix, matrix.dtype) # [ 1. 2. 3. 4.] float64
- matrix = np.array([1, 2, 3, ''])
- print(matrix, matrix.dtype) # ['1' '2' '3' '4'] <U11
- # Basic slice operation
- matrix = np.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12],
- [13, 14, 15, 16]])
- # Get number certain element(7), [row, column]
- print(matrix[1,2]) #
- # Get all row and column 3
- print(matrix[:, 2]) # [3 7 11 15]
- # Get certain row and column, not contains the max number
- print(matrix[0:3, 0:2]) ''' [[ 1 2]
- [ 5 6]
- [ 9 10]] '''
- # Get the number by certain list
- # (0,0),(1,1),(2,2),(3,3)
- print(matrix[[0, 1, 2, 3], [0, 1, 2, 3]]) # [1 6 11 16]
- # Use range method can make it too
- print(matrix[range(4), range(4)]) # [1 6 11 16]
- # Operation to array will act to each element
- equal_to_seven = (matrix == 7)
- print(equal_to_seven) ''' [[False False False False]
- [False False True False]
- [False False False False]
- [False False False False]] '''
- # equal_to_seven can be use as an index, True/False list also can do so
- print(matrix[equal_to_seven]) # [7]
- get_certain_row = [False, True, True, False]
- print(matrix[get_certain_row]) ''' [[ 5 6 7 8]
- [ 9 10 11 12]] '''
- # Fetch row where the certain number in
- matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
- print(matrix[matrix[:, 2] == 7]) # [[5 6 7 8]]
- vector = np.array([5, 10, 15, 20])
- # Find the element equal to ten and five(impossible)
- equal_to_ten_and_five = (vector == 5) & (vector == 10)
- print(vector[equal_to_ten_and_five]) # []
- # Find the element equal to ten or five
- equal_to_ten_or_five = (vector == 5) | (vector == 10)
- print(vector[equal_to_ten_or_five]) # [5 10]
- # Change equal one to other number
- vector[equal_to_ten_or_five]=50
- print(vector) # [50 50 15 20]
- # Change the type of array
- vector = np.array(['', '', ''])
- print(vector.dtype, vector) # <U1 ['1' '2' '3']
- new_vector = vector.astype(int)
- print(new_vector.dtype, new_vector) # int32 [1 2 3]
- # Get the min number
- # Use print(help(np.array))
- vector = np.array([5, 10, 15, 20])
- print(vector.min()) #
- # Calculate the sum in certain dimension
- # 1 for cal sum in row, 0 for cal sum in column
- matrix = np.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12],
- [13, 14, 15, 16]])
- print(matrix.sum(axis=1)) # [10 26 42 58]
- print(matrix.sum(axis=0)) # [28 32 36 40]
分段解释
首先导入 numpy,
接着利用dtype属性查看元素数据类型,numpy中元素的类型必须一致,因此改变一个元素的类型,则其余元素也会被改变。
- import numpy as np
- # Change type, all the elements in array should be in same type.
- matrix = np.array([1, 2, 3, 4])
- print(matrix, matrix.dtype) # [1 2 3 4] int32
- matrix = np.array([1, 2, 3, 4.0])
- print(matrix, matrix.dtype) # [ 1. 2. 3. 4.] float64
- matrix = np.array([1, 2, 3, ''])
- print(matrix, matrix.dtype) # ['1' '2' '3' '4'] <U11
获取元素可以根据索引值进行,也可以根据类似列表切片的方法获取
- # Basic slice operation
- matrix = np.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12],
- [13, 14, 15, 16]])
- # Get number certain element(7), [row, column]
- print(matrix[1,2]) #
- # Get all row and column 3
- print(matrix[:, 2]) # [3 7 11 15]
- # Get certain row and column, not contains the max number
- print(matrix[0:3, 0:2]) ''' [[ 1 2]
- [ 5 6]
- [ 9 10]] '''
使用列表的方式获取元素,两个列表分别为两个维度,两两组合获取数据
- # Get the number by certain list
- # (0,0),(1,1),(2,2),(3,3)
- print(matrix[[0, 1, 2, 3], [0, 1, 2, 3]]) # [1 6 11 16]
- # Use range method can make it too
- print(matrix[range(4), range(4)]) # [1 6 11 16]
对于矩阵的操作将会分别作用于每一个元素上
- # Operation to array will act to each element
- equal_to_seven = (matrix == 7)
- print(equal_to_seven) ''' [[False False False False]
- [False False True False]
- [False False False False]
- [False False False False]] '''
- # equal_to_seven can be use as an index, True/False list also can do so
- print(matrix[equal_to_seven]) # [7]
- get_certain_row = [False, True, True, False]
- print(matrix[get_certain_row]) ''' [[ 5 6 7 8]
- [ 9 10 11 12]] '''
- # Fetch row where the certain number in
- matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
- print(matrix[matrix[:, 2] == 7]) # [[5 6 7 8]]
- vector = np.array([5, 10, 15, 20])
- # Find the element equal to ten and five(impossible)
- equal_to_ten_and_five = (vector == 5) & (vector == 10)
- print(vector[equal_to_ten_and_five]) # []
- # Find the element equal to ten or five
- equal_to_ten_or_five = (vector == 5) | (vector == 10)
- print(vector[equal_to_ten_or_five]) # [5 10]
- # Change equal one to other number
- vector[equal_to_ten_or_five]=50
- print(vector) # [50 50 15 20]
使用astype或shape=或reshape进行列表形状修改
- # Change the type of array
- vector = np.array(['', '', ''])
- print(vector.dtype, vector) # <U1 ['1' '2' '3']
- new_vector = vector.astype(int)
- print(new_vector.dtype, new_vector) # int32 [1 2 3]
获取最大最小值,以及求和操作
- # Get the min number
- # Use print(help(np.array))
- vector = np.array([5, 10, 15, 20])
- print(vector.min()) #
- # Calculate the sum in certain dimension
- # 1 for cal sum in row, 0 for cal sum in column
- matrix = np.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12],
- [13, 14, 15, 16]])
- print(matrix.sum(axis=1)) # [10 26 42 58]
- print(matrix.sum(axis=0)) # [28 32 36 40]
4 numpy复制操作
在numpy(或者说Python)中,复制的矩阵的操作主要有三种方式,
- 直接通过赋值操作复制,这种复制得到的两个矩阵具有相同的id,其本质是两个指向同一内存空间的不同变量名;
- 通过numpy的view函数进行复制,这种复制可以得到两个id不同的矩阵,分别改变他们的形状等参数是互不影响的,但是两者共享同一原始数据,即修改其中一个的数据内容会对另外一个产生影响;
- 使用numpy的copy函数进行复制,这是一种完全复制,可以得到两个完全互不相关的矩阵。
代码如下
- import numpy as np
- # The simple assignments make no copy, a and b are two names for one same ndarray object
- a = np.arange(12)
- b = a
- print(b is a) # True
- b.shape = (3, 4)
- print(a.shape) # (3, 4)
- # a and b with same id
- print(id(a))
- print(id(b))
- # The view method creates a new array obj that share with same source data
- a = np.arange(12)
- c = a.view()
- print(c is a) # False
- # Change the shape of c will not change the shape of a
- c.shape = (2, 6)
- print(a.shape) # (12, )
- # But Change the data of c will change the data of a
- c[1, 2] = 1111
- print(a)
- # a and b with different id
- print(id(a))
- print(id(b))
- # The copy method makes a complete copy of array and its data
- a = np.arange(12)
- d = a.copy()
- print(d is a) # False
5 numpy计算
两个同维度矩阵的加减乘除、大小比较、开方、次幂、取整等操作,都将分别作用在各个元素上,点乘计算可以通过numpy.dot函数进行计算
完整代码
- import numpy as np
- a = np.array([20, 30, 40, 50])
- b = np.arange(4)
- # For (matrix - matrix) will operate for each corresponding elements
- # Different shape can not be subtracted unless only one element
- c = a - b
- print(c) # [20 29 38 47]
- # For (matrix (-/'**'/'<') number) will operate for each elements
- c -= 1
- print(c) # [19 28 37 46]
- b = b**2 # (b*b)
- print(b) # [0, 1, 4, 9]
- print(a<35) # [True, True, False, False]
- # * will multiply each element in corresponding position
- # dot will act the dot multiply for matrix(by using matrix_1.dot(matrix_2) or np.dot(matrix_1, matrix_2))
- x = np.array([[1, 1],
- [0, 1]])
- y = np.array([[2, 0],
- [3, 4]])
- print(x*y) '''[[2 0]
- [0 4]] '''
- print('-------')
- print(x.dot(y)) '''[[5 4]
- [3 4]] '''
- print('-------')
- print(np.dot(x, y)) '''[[5 4]
- [3 4]]'''
- # exp function to cal e^x for x in matrix
- # sqrt function to cal sqrt(x) for x in matrix
- m = np.arange(3)
- print(m) # [0 1 2]
- print(np.exp(m)) # [ 1. 2.71828183 7.3890561 ]
- print(np.sqrt(m)) # [ 0. 1. 1.41421356]
- # floor/ceil function to round(down/up) number of each matrix
- x = 10*np.random.random((3, 4))
- print(x) '''[[ 6.69732597 1.18238851 4.10109987 6.40797969]
- [ 6.50193132 2.58724942 5.25748965 2.58338795]
- [ 0.2798712 7.89760089 6.03544519 1.5176369 ]] '''
- print('-------')
- y = np.floor(x)
- print(y) '''[[ 6. 1. 4. 6.]
- [ 6. 2. 5. 2.]
- [ 0. 7. 6. 1.]] '''
- print('-------')
- z = np.ceil(x)
- print(z) ''' [[ 7. 2. 5. 7.]
- [ 7. 3. 6. 3.]
- [ 1. 8. 7. 2.]] '''
- # ravel function can flatten a matrix into vector
- print(y.ravel()) # [ 6. 1. 4. 6. 6. 2. 5. 2. 0. 7. 6. 1.]
- # Shape value can change shape too
- y.shape = (6, 2)
- print(y) ''' [[ 6. 1.]
- [ 4. 6.]
- [ 6. 2.]
- [ 5. 2.]
- [ 0. 7.]
- [ 6. 1.]] '''
- print('-------')
- # T property function
- print(y.T) ''' [[ 6. 4. 6. 5. 0. 6.]
- [ 1. 6. 2. 2. 7. 1.]] '''
- print('-------')
- # If a dimension is given as -1, this dimension will be calculated automatically
- print(y.reshape(6, -1)) ''' [[ 6. 1.]
- [ 4. 6.]
- [ 6. 2.]
- [ 5. 2.]
- [ 0. 7.]
- [ 6. 1.]] '''
- # hstack/vstack function can stack the matrix in h/v direction
- a = np.arange(6).reshape(2, 3)
- b = np.arange(6).reshape(2, 3)
- print(np.hstack((a, b))) '''[[0 1 2 0 1 2]
- [3 4 5 3 4 5]] '''
- print(np.vstack((a, b))) '''[[0 1 2]
- [3 4 5]
- [0 1 2]
- [3 4 5]] '''
- # hsplit/vsplit function can split the matrix in h/v direction
- x = np.arange(30).reshape(2, -1)
- print(x) ''' [[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
- [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]] '''
- # Split into 3 part (h direction, from one (2, 15) matrix to three (2, 5) matrixes)
- print(np.hsplit(x, 3)) '''[array([[ 0, 1, 2, 3, 4],
- [15, 16, 17, 18, 19]]),
- array([[ 5, 6, 7, 8, 9],
- [20, 21, 22, 23, 24]]),
- array([[10, 11, 12, 13, 14],
- [25, 26, 27, 28, 29]])] '''
- # Split in certain loction (split the matrix in location(after) column 3 and column 4)
- print(np.hsplit(x, (3, 4))) '''[array([[ 0, 1, 2],
- [15, 16, 17]]),
- array([[ 3],
- [18]]),
- array([[ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
- [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
- # Split into 3 part (v direction, from one (2, 15) matrix to two (1, 15) matrixes)
- print(np.vsplit(x, 2)) ''' [array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]),
- array([[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
- # Fetch the max value position by row/column
- data = np.random.random((5, 4))
- print(data) ''' [[ 0.65419205 0.75953852 0.89856871 0.96162281]
- [ 0.76341568 0.10488636 0.06186101 0.27698986]
- [ 0.73737843 0.75305691 0.28705743 0.45542513]
- [ 0.5534984 0.54420756 0.86250921 0.596653 ]
- [ 0.24295898 0.28894731 0.58726507 0.39418991]] '''
- # argmax/argmin function can get the max/min value index by row/column
- index = data.argmax(axis=0)
- print(index) # [1 0 0 0]
- # Get the max value by position
- # data.shape[1] to get the number of column
- print(data[index, range(data.shape[1])]) # [ 0.76341568 0.75953852 0.89856871 0.96162281]
- # Extend the matrix(like tile does)
- x = np.arange(0, 40, 10)
- print(x) # [ 0 10 20 30]
- # tile(x, (a, b)) can change origin shape(c, d) --> (a*c, b*d)
- y = np.tile(x, (3, 5))
- print(y) ''' [[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
- [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
- [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]] '''
- print(y.shape) # (3, 20)
- # Sort the array
- x = np.array([[4, 3, 5], [1, 2, 1]])
- print(x) '''[[4 3 5]
- [1 2 1]] '''
- print('--------')
- # Sort the number by row/column
- y = np.sort(x, axis=1)
- print(y) '''[[3 4 5]
- [1 1 2]] '''
- print('--------')
- x.sort(axis=1)
- print(x) '''[[3 4 5]
- [1 1 2]] '''
- print('--------')
- # argsort can return the sorted index list
- x = np.array([4, 3, 1, 2])
- y = np.argsort(x)
- print(y) # [2, 3, 1, 0] min is 1, index=2, then num 2, index is 3, num 3,index is 1, max is 4, index=0
- # Use the sorted index list to fetch value
- print('--------')
- print(x[y]) # [1, 2, 3, 4]
分段解释
两个同维度矩阵的加减乘除、大小比较、开方、次幂、取整等操作,都将分别作用在各个元素上,点乘计算可以通过numpy.dot函数进行计算
- import numpy as np
- a = np.array([20, 30, 40, 50])
- b = np.arange(4)
- # For (matrix - matrix) will operate for each corresponding elements
- # Different shape can not be subtracted unless only one element
- c = a - b
- print(c) # [20 29 38 47]
- # For (matrix (-/'**'/'<') number) will operate for each elements
- c -= 1
- print(c) # [19 28 37 46]
- b = b**2 # (b*b)
- print(b) # [0, 1, 4, 9]
- print(a<35) # [True, True, False, False]
- # * will multiply each element in corresponding position
- # dot will act the dot multiply for matrix(by using matrix_1.dot(matrix_2) or np.dot(matrix_1, matrix_2))
- x = np.array([[1, 1],
- [0, 1]])
- y = np.array([[2, 0],
- [3, 4]])
- print(x*y) '''[[2 0]
- [0 4]] '''
- print('-------')
- print(x.dot(y)) '''[[5 4]
- [3 4]] '''
- print('-------')
- print(np.dot(x, y)) '''[[5 4]
- [3 4]]'''
- # exp function to cal e^x for x in matrix
- # sqrt function to cal sqrt(x) for x in matrix
- m = np.arange(3)
- print(m) # [0 1 2]
- print(np.exp(m)) # [ 1. 2.71828183 7.3890561 ]
- print(np.sqrt(m)) # [ 0. 1. 1.41421356]
random模块可以生成随机元素的矩阵
- # floor/ceil function to round(down/up) number of each matrix
- x = 10*np.random.random((3, 4))
- print(x) '''[[ 6.69732597 1.18238851 4.10109987 6.40797969]
- [ 6.50193132 2.58724942 5.25748965 2.58338795]
- [ 0.2798712 7.89760089 6.03544519 1.5176369 ]] '''
- print('-------')
- y = np.floor(x)
- print(y) '''[[ 6. 1. 4. 6.]
- [ 6. 2. 5. 2.]
- [ 0. 7. 6. 1.]] '''
- print('-------')
- z = np.ceil(x)
- print(z) ''' [[ 7. 2. 5. 7.]
- [ 7. 3. 6. 3.]
- [ 1. 8. 7. 2.]] '''
ravel函数可以将矩阵展开成一维,T属性方法可以得到转置矩阵
- # ravel function can flatten a matrix into vector
- print(y.ravel()) # [ 6. 1. 4. 6. 6. 2. 5. 2. 0. 7. 6. 1.]
- # Shape value can change shape too
- y.shape = (6, 2)
- print(y) ''' [[ 6. 1.]
- [ 4. 6.]
- [ 6. 2.]
- [ 5. 2.]
- [ 0. 7.]
- [ 6. 1.]] '''
- print('-------')
- # T property function
- print(y.T) ''' [[ 6. 4. 6. 5. 0. 6.]
- [ 1. 6. 2. 2. 7. 1.]] '''
- print('-------')
reshape函数若其中一个参数为-1则该维度由其他维度计算得到
- # If a dimension is given as -1, this dimension will be calculated automatically
- print(y.reshape(6, -1)) ''' [[ 6. 1.]
- [ 4. 6.]
- [ 6. 2.]
- [ 5. 2.]
- [ 0. 7.]
- [ 6. 1.]] '''
random模块可以生成随机元素的矩阵。
- # hstack/vstack function can stack the matrix in h/v direction
- a = np.arange(6).reshape(2, 3)
- b = np.arange(6).reshape(2, 3)
- print(np.hstack((a, b))) '''[[0 1 2 0 1 2]
- [3 4 5 3 4 5]] '''
- print(np.vstack((a, b))) '''[[0 1 2]
- [3 4 5]
- [0 1 2]
- [3 4 5]] '''
堆叠以及切割操作
- # hsplit/vsplit function can split the matrix in h/v direction
- x = np.arange(30).reshape(2, -1)
- print(x) ''' [[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
- [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]] '''
- # Split into 3 part (h direction, from one (2, 15) matrix to three (2, 5) matrixes)
- print(np.hsplit(x, 3)) '''[array([[ 0, 1, 2, 3, 4],
- [15, 16, 17, 18, 19]]),
- array([[ 5, 6, 7, 8, 9],
- [20, 21, 22, 23, 24]]),
- array([[10, 11, 12, 13, 14],
- [25, 26, 27, 28, 29]])] '''
- # Split in certain loction (split the matrix in location(after) column 3 and column 4)
- print(np.hsplit(x, (3, 4))) '''[array([[ 0, 1, 2],
- [15, 16, 17]]),
- array([[ 3],
- [18]]),
- array([[ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
- [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
- # Split into 3 part (v direction, from one (2, 15) matrix to two (1, 15) matrixes)
- print(np.vsplit(x, 2)) ''' [array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]),
- array([[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
获取最大值索引
- # Fetch the max value position by row/column
- data = np.random.random((5, 4))
- print(data) ''' [[ 0.65419205 0.75953852 0.89856871 0.96162281]
- [ 0.76341568 0.10488636 0.06186101 0.27698986]
- [ 0.73737843 0.75305691 0.28705743 0.45542513]
- [ 0.5534984 0.54420756 0.86250921 0.596653 ]
- [ 0.24295898 0.28894731 0.58726507 0.39418991]] '''
- # argmax/argmin function can get the max/min value index by row/column
- index = data.argmax(axis=0)
- print(index) # [1 0 0 0]
- # Get the max value by position
- # data.shape[1] to get the number of column
- print(data[index, range(data.shape[1])]) # [ 0.76341568 0.75953852 0.89856871 0.96162281]
将矩阵进行铺展操作
- # Extend the matrix(like tile does)
- x = np.arange(0, 40, 10)
- print(x) # [ 0 10 20 30]
- # tile(x, (a, b)) can change origin shape(c, d) --> (a*c, b*d)
- y = np.tile(x, (3, 5))
- print(y) ''' [[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
- [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
- [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]] '''
- print(y.shape) # (3, 20)
矩阵的排序及排序索引
- # Sort the array
- x = np.array([[4, 3, 5], [1, 2, 1]])
- print(x) '''[[4 3 5]
- [1 2 1]] '''
- print('--------')
- # Sort the number by row/column
- y = np.sort(x, axis=1)
- print(y) '''[[3 4 5]
- [1 1 2]] '''
- print('--------')
- x.sort(axis=1)
- print(x) '''[[3 4 5]
- [1 1 2]] '''
- print('--------')
- # argsort can return the sorted index list
- x = np.array([4, 3, 1, 2])
- y = np.argsort(x)
- print(y) # [2, 3, 1, 0] min is 1, index=2, then num 2, index is 3, num 3,index is 1, max is 4, index=0
- # Use the sorted index list to fetch value
- print('--------')
- print(x[y]) # [1, 2, 3, 4]
6 numpy常用函数
完整代码
- import numpy as np
- # Build a matrix, shape is (2, 4, 2)
- matrix = np.array([[[1,2], [2,3], [3,4], [4,5]],
- [[1,2], [2,3], [3,4], [4,5]]])
- # arange function to quickly create an array(vector)
- # Note: the shape is (x, ), one dimension array
- vector = np.arange(15)
- print(vector) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
- # reshape function to reshape an array
- matrix = vector.reshape(3, 5)
- print(matrix) ''' [[ 0 1 2 3 4]
- [ 5 6 7 8 9]
- [10 11 12 13 14]] '''
- # shape is the row&column of matrix(array) - (3, 5)
- # ndim is the number of dimensions(axes) of matrix - 2
- # dtype is a numpy class, dtype.name is the type name of matrix - int32
- # size is the total number of elements of matrix - 15
- print(matrix.shape, matrix.ndim, matrix.dtype.name, matrix.size) # (3, 5) 2 int32 15
- # arrange(from, to, step)
- vector = np.arange(10, 30, 5)
- print(vector) # [10 15 20 25]
- vector = np.arange(1, 3, 0.5)
- print(vector) # [ 1. 1.5 2. 2.5]
- # Init a matrix with all elements 0, default type is float64
- matrix = np.zeros((3, 4), dtype=np.float64)
- print(matrix) ''' [[ 0. 0. 0. 0.]
- [ 0. 0. 0. 0.]
- [ 0. 0. 0. 0.]] '''
- # Init a matrix with all elements 1
- matrix = np.ones((2, 3, 4), dtype=np.int32)
- print(matrix) ''' [[[1 1 1 1]
- [1 1 1 1]
- [1 1 1 1]]
- [[1 1 1 1]
- [1 1 1 1]
- [1 1 1 1]]]'''
- # random function will return a matrix with random number(between 0 to 1)
- # random((row, column))
- rd = np.random.random((2, 3))
- print(rd) ''' [[ 0.45595053 0.69816822 0.30391984]
- [ 0.22757757 0.725762 0.84856338]] '''
- # Get the matrix with some same step numbers,
- # 得到相应数量的等差数列
- # linspace(from, to, number)
- # Notice whether the num should be plus one
- matrix = np.linspace(0, 2, 11)
- print(matrix) # [ 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. ]
- # sin/cos function can calculate the sin/cos of each elements
- from numpy import pi
- matrix = np.linspace(0, 2*pi, 9)
- print(matrix) # [ 0. 0.78539816 1.57079633 2.35619449 3.14159265 3.92699082 4.71238898 5.49778714 6.28318531]
- matrix = np.sin(matrix)
- print(matrix) # [ 0.00000000e+00 7.07106781e-01 1.00000000e+00 7.07106781e-01 1.22464680e-16 -7.07106781e-01 -1.00000000e+00 -7.07106781e-01 -2.44929360e-16]
分段解释
建立一个矩阵,可以通过array或arange函数生成,对于一位矩阵,其shape值为(x, )
- import numpy as np
- # Build a matrix, shape is (2, 4, 2)
- matrix = np.array([[[1,2], [2,3], [3,4], [4,5]],
- [[1,2], [2,3], [3,4], [4,5]]])
- # arange function to quickly create an array(vector)
- # Note: the shape is (x, ), one dimension array
- vector = np.arange(15)
- print(vector) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
- # reshape function to reshape an array
- matrix = vector.reshape(3, 5)
- print(matrix) ''' [[ 0 1 2 3 4]
- [ 5 6 7 8 9]
- [10 11 12 13 14]] '''
- # shape is the row&column of matrix(array) - (3, 5)
- # ndim is the number of dimensions(axes) of matrix - 2
- # dtype is a numpy class, dtype.name is the type name of matrix - int32
- # size is the total number of elements of matrix - 15
- print(matrix.shape, matrix.ndim, matrix.dtype.name, matrix.size) # (3, 5) 2 int32 15
- # arrange(from, to, step)
- vector = np.arange(10, 30, 5)
- print(vector) # [10 15 20 25]
- vector = np.arange(1, 3, 0.5)
- print(vector) # [ 1. 1.5 2. 2.5]
生成一个全零/一矩阵
- # Init a matrix with all elements 0, default type is float64
- matrix = np.zeros((3, 4), dtype=np.float64)
- print(matrix) ''' [[ 0. 0. 0. 0.]
- [ 0. 0. 0. 0.]
- [ 0. 0. 0. 0.]] '''
- # Init a matrix with all elements 1
- matrix = np.ones((2, 3, 4), dtype=np.int32)
- print(matrix) ''' [[[1 1 1 1]
- [1 1 1 1]
- [1 1 1 1]]
- [[1 1 1 1]
- [1 1 1 1]
- [1 1 1 1]]]'''
随机矩阵及等差矩阵等
- # random function will return a matrix with random number(between 0 to 1)
- # random((row, column))
- rd = np.random.random((2, 3))
- print(rd) ''' [[ 0.45595053 0.69816822 0.30391984]
- [ 0.22757757 0.725762 0.84856338]] '''
- # Get the matrix with some same step numbers,
- # 得到相应数量的等差数列
- # linspace(from, to, number)
- # Notice whether the num should be plus one
- matrix = np.linspace(0, 2, 11)
- print(matrix) # [ 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. ]
- # sin/cos function can calculate the sin/cos of each elements
- from numpy import pi
- matrix = np.linspace(0, 2*pi, 9)
- print(matrix) # [ 0. 0.78539816 1.57079633 2.35619449 3.14159265 3.92699082 4.71238898 5.49778714 6.28318531]
- matrix = np.sin(matrix)
- print(matrix) # [ 0.00000000e+00 7.07106781e-01 1.00000000e+00 7.07106781e-01 1.22464680e-16 -7.07106781e-01 -1.00000000e+00 -7.07106781e-01 -2.44929360e-16]
参考链接
http://blog.csdn.net/chen_shiqiang/article/details/51868115
Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结的更多相关文章
- python学习--大数据与科学计算第三方库简介
大数据与科学计算 库名称 简介 pycuda/opencl GPU高性能并发计算 Pandas python实现的类似R语言的数据统计.分析平台.基于NumPy和Matplotlib开发的,主要用于 ...
- python安装numpy科学计算模块
解决两个问题: (1)Import Error: No module named numpy (2)Python version 2.7 required, which was not found i ...
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)
原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...
- 【Python】Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
本文转载自:https://www.cnblogs.com/colipso/p/4284510.html 好文 mark http://www.52nlp.cn/python-%E7%BD%91%E9 ...
- python学习笔记(2):科学计算及数据可视化入门
一.NumPy 1.NumPy:Numberical Python 2.高性能科学计算和数据分析的基础包 3.ndarray,多维数组(矩阵),具有矢量运算的能力,快速.节省空间 (1)ndarray ...
- Python科学计算—numpy模块总结(1)
作为一个本科学数学专业,目前研究非线性物理领域的研究僧.用什么软件进行纯科学计算好,Fortran永远是第一位的:matlab虽然很强大,可以很容易的处理大量的大矩阵,但是求解我们的模型(有时可能是几 ...
- 给统计人讲Python(1)_科学计算库-Numpy
本地代码是.ipynb格式的转换到博客上很麻烦,这里展示部分代码,了解更多可以查看我的git-hub:https://github.com/Yangami/Python-for-Statisticia ...
- 科学计算 NumPy 与C语言对比 N-dimensional array ndarray 元素元素操作 计算正太分布分位数
w http://www.numpy.org/ NumPy is the fundamental package for scientific computing with Python. It co ...
- Numpy科学计算
NumPy介绍 NumPy(Numerical Python)是一个开源的Python科学计算库,用于快速处理任意维度的数组. NumPy支持常见的数组和矩阵操作.对于同样的数值计算任务,使用Nu ...
随机推荐
- 【Dual Support Vector Machine】林轩田机器学习技法
这节课内容介绍了SVM的核心. 首先,既然SVM都可以转化为二次规划问题了,为啥还有有Dual啥的呢?原因如下: 如果x进行non-linear transform后,二次规划算法需要面对的是d`+1 ...
- 发布“豪情”设计的新博客皮肤-darkgreentrip
豪情 (http://www.cnblogs.com/jikey/)是一名在上海的前端开发人员,长期驻扎在园子里.他为大家设计了一款新的博客皮肤——darkgreentrip. 以下是该博客皮肤的介绍 ...
- thinkPHP 表单自动验证功能
昨天晚上我们老大叫我弄表单自动验证功能,愁了半天借鉴了好多官网的知识,才出来,诶,总之分享一下我自己的成果吧! thinkphp 在Model基类为我们定义了自动验证的函数和正则表达式,我们只需要在对 ...
- Kotlin中功能操作与集合(KAD 11)
作者:Antonio Leiva 时间:Feb 2, 2017 原文链接:https://antonioleiva.com/functional-operations-collections-kotl ...
- Mysql 查询—按位运算
前言:虽说这是件小事儿,但本宝宝思前想后,还是为它留下一笔,嘿嘿.反正写博客不浪费纸和笔!好久没有开启我的逗比模式了,我亲爱的乖徒弟DBA,DBB,DBAA等,好久不见你们,遥祝幸福快乐+DB. 整个 ...
- kibana的查询语法
kibana的查询语法是 字段Fields:关键词
- Hexo安装配置详解
原文 http://blog.csdn.net/tonydandelion2014/article/details/61615898 http://www.joryhe.com/2016-06-06- ...
- Java 虚拟机类加载机制
看到这个题目,很多人会觉得我写我的java代码,至于类,JVM爱怎么加载就怎么加载,博主有很长一段时间也是这么认为的.随着编程经验的日积月累,越来越感觉到了解虚拟机相关要领的重要性.闲话不多说,老规矩 ...
- gdb server调试步骤
编译gdb/gdbserver 编译arm-linux-gdb 下载gdb-7.12,解压缩进入目录 ./configure --target=arm-linux --program-prefix=a ...
- 自动设置 rem es模块写法
export default function () { let html = document.documentElement; function onWindowResize() { if (ht ...