Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结

NumPy


目录

  1. 关于 numpy
  2. numpy 库
  3. numpy 基本操作
  4. numpy 复制操作
  5. numpy 计算
  6. 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实现的科学计算包。包括:

  1. 一个强大的N维数组对象Array;
  2. 比较成熟的(广播)函数库;
  3. 用于整合C/C++和Fortran代码的工具包;
  4. 实用的线性代数、傅里叶变换和随机数生成函数。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)中,复制的矩阵的操作主要有三种方式,

  1. 直接通过赋值操作复制,这种复制得到的两个矩阵具有相同的id,其本质是两个指向同一内存空间的不同变量名;
  2. 通过numpy的view函数进行复制,这种复制可以得到两个id不同的矩阵,分别改变他们的形状等参数是互不影响的,但是两者共享同一原始数据,即修改其中一个的数据内容会对另外一个产生影响;
  3. 使用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://www.numpy.org/

http://blog.csdn.net/chen_shiqiang/article/details/51868115

上一篇:Android 学习笔记二 自定义按钮形状 颜色 点击渐变


下一篇:RF《Quick Start Guide》操作总结