zoukankan      html  css  js  c++  java
  • 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.:

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

    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 基本函数的使用方法,

    完整代码

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

    分段解释

    首先导入 numpy,

    接着利用dtype属性查看元素数据类型,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

     获取元素可以根据索引值进行,也可以根据类似列表切片的方法获取

     1 # Basic slice operation
     2 matrix = np.array([[1, 2, 3, 4],
     3              [5, 6, 7, 8],
     4              [9, 10, 11, 12],
     5              [13, 14, 15, 16]])
     6 # Get number certain element(7), [row, column]
     7 print(matrix[1,2])          # 7
     8 # Get all row and column 3
     9 print(matrix[:, 2])         # [3 7 11 15]
    10 # Get certain row and column, not contains the max number
    11 print(matrix[0:3, 0:2])     ''' [[ 1  2]
    12                                  [ 5  6]
    13                                  [ 9 10]] '''

    使用列表的方式获取元素,两个列表分别为两个维度,两两组合获取数据

    1 # Get the number by certain list
    2 # (0,0),(1,1),(2,2),(3,3)
    3 print(matrix[[0, 1, 2, 3], [0, 1, 2, 3]])   # [1 6 11 16]
    4 # Use range method can make it too
    5 print(matrix[range(4), range(4)])           # [1 6 11 16]

    对于矩阵的操作将会分别作用于每一个元素上

     1 # Operation to array will act to each element
     2 equal_to_seven = (matrix == 7)
     3 print(equal_to_seven)       ''' [[False False False False]
     4                                  [False False  True False]
     5                                  [False False False False]
     6                                  [False False False False]] '''
     7 
     8 # equal_to_seven can be use as an index, True/False list also can do so
     9 print(matrix[equal_to_seven])       # [7]
    10 get_certain_row = [False, True, True, False]
    11 print(matrix[get_certain_row])      ''' [[ 5  6  7  8]
    12                                          [ 9 10 11 12]] '''
    13 # Fetch row where the certain number in
    14 matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
    15 print(matrix[matrix[:, 2] == 7])    # [[5 6 7 8]]
    16 
    17 vector = np.array([5, 10, 15, 20])
    18 # Find the element equal to ten and five(impossible)
    19 equal_to_ten_and_five = (vector == 5) & (vector == 10)
    20 print(vector[equal_to_ten_and_five])                    # []
    21 # Find the element equal to ten or five
    22 equal_to_ten_or_five = (vector == 5) | (vector == 10)   
    23 print(vector[equal_to_ten_or_five])                     # [5 10]
    24 # Change equal one to other number
    25 vector[equal_to_ten_or_five]=50
    26 print(vector)                                           # [50 50 15 20]

    使用astype或shape=或reshape进行列表形状修改

    1 # Change the type of array
    2 vector = np.array(['1', '2', '3'])
    3 print(vector.dtype, vector)     # <U1 ['1' '2' '3']
    4 new_vector = vector.astype(int)
    5 print(new_vector.dtype, new_vector)     # int32 [1 2 3]

    获取最大最小值,以及求和操作

     1 # Get the min number
     2 # Use print(help(np.array))
     3 vector = np.array([5, 10, 15, 20])
     4 print(vector.min())     # 5
     5 
     6 # Calculate the sum in certain dimension
     7 # 1 for cal sum in row, 0 for cal sum in column
     8 matrix = np.array([[1, 2, 3, 4],
     9              [5, 6, 7, 8],
    10              [9, 10, 11, 12],
    11              [13, 14, 15, 16]])
    12 print(matrix.sum(axis=1))   # [10 26 42 58]
    13 print(matrix.sum(axis=0))   # [28 32 36 40]

    4 numpy复制操作

    在numpy(或者说Python)中,复制的矩阵的操作主要有三种方式,

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

    代码如下

     1 import numpy as np
     2 
     3 # The simple assignments make no copy, a and b are two names for one same ndarray object
     4 a = np.arange(12)
     5 b = a 
     6 print(b is a) # True
     7 b.shape = (3, 4)
     8 print(a.shape) # (3, 4)
     9 # a and b with same id
    10 print(id(a))
    11 print(id(b))
    12 
    13 # The view method creates a new array obj that share with same source data
    14 a = np.arange(12)
    15 c = a.view()
    16 print(c is a) # False
    17 # Change the shape of c will not change the shape of a
    18 c.shape = (2, 6)
    19 print(a.shape) # (12, )
    20 # But Change the data of c will change the data of a 
    21 c[1, 2] = 1111
    22 print(a)
    23 # a and b with different id
    24 print(id(a))
    25 print(id(b))
    26 
    27 # The copy method makes a complete copy of array and its data
    28 a = np.arange(12)
    29 d = a.copy()
    30 print(d is a) # False

    5 numpy计算

    两个同维度矩阵的加减乘除、大小比较、开方、次幂、取整等操作,都将分别作用在各个元素上,点乘计算可以通过numpy.dot函数进行计算

    完整代码

      1 import numpy as np
      2 
      3 a = np.array([20, 30, 40, 50])
      4 b = np.arange(4)
      5 # For (matrix - matrix) will operate for each corresponding elements
      6 # Different shape can not be subtracted unless only one element
      7 c = a - b
      8 print(c)    # [20 29 38 47]
      9 # For (matrix (-/'**'/'<') number) will operate for each elements 
     10 c -= 1
     11 print(c)    # [19 28 37 46]
     12 b = b**2 # (b*b)
     13 print(b) # [0, 1, 4, 9]
     14 print(a<35) # [True, True, False, False]
     15 
     16 # * will multiply each element in corresponding position
     17 # dot will act the dot multiply for matrix(by using matrix_1.dot(matrix_2) or np.dot(matrix_1, matrix_2))
     18 x = np.array([[1, 1],
     19               [0, 1]])
     20 y = np.array([[2, 0],
     21               [3, 4]])
     22 print(x*y)  '''[[2 0]
     23                 [0 4]] '''
     24 print('-------')
     25 print(x.dot(y))     '''[[5 4]
     26                         [3 4]] '''
     27 
     28 print('-------')
     29 print(np.dot(x, y)) '''[[5 4]
     30                         [3 4]]'''
     31 
     32 # exp function to cal e^x for x in matrix
     33 # sqrt function to cal sqrt(x) for x in matrix
     34 m = np.arange(3)
     35 print(m)    # [0 1 2]
     36 print(np.exp(m))    # [ 1.          2.71828183  7.3890561 ]
     37 print(np.sqrt(m))   # [ 0.          1.          1.41421356]
     38 
     39 # floor/ceil function to round(down/up) number of each matrix
     40 x = 10*np.random.random((3, 4))
     41 print(x)    '''[[ 6.69732597  1.18238851  4.10109987  6.40797969]
     42                 [ 6.50193132  2.58724942  5.25748965  2.58338795]
     43                 [ 0.2798712   7.89760089  6.03544519  1.5176369 ]] '''
     44 print('-------')
     45 y = np.floor(x)
     46 print(y)     '''[[ 6.  1.  4.  6.]
     47                  [ 6.  2.  5.  2.]
     48                  [ 0.  7.  6.  1.]] '''
     49 print('-------')
     50 z = np.ceil(x)
     51 print(z)    ''' [[ 7.  2.  5.  7.]
     52                  [ 7.  3.  6.  3.]
     53                  [ 1.  8.  7.  2.]] '''
     54 
     55 # ravel function can flatten a matrix into vector
     56 print(y.ravel())    # [ 6.  1.  4.  6.  6.  2.  5.  2.  0.  7.  6.  1.]
     57 # Shape value can change shape too
     58 y.shape = (6, 2)
     59 print(y)    ''' [[ 6.  1.]
     60                  [ 4.  6.]
     61                  [ 6.  2.]
     62                  [ 5.  2.]
     63                  [ 0.  7.]
     64                  [ 6.  1.]] '''
     65 print('-------')
     66 # T property function
     67 print(y.T)  ''' [[ 6.  4.  6.  5.  0.  6.]
     68                  [ 1.  6.  2.  2.  7.  1.]] '''
     69 print('-------')
     70 # If a dimension is given as -1, this dimension will be calculated automatically
     71 print(y.reshape(6, -1)) ''' [[ 6.  1.]
     72                              [ 4.  6.]
     73                              [ 6.  2.]
     74                              [ 5.  2.]
     75                              [ 0.  7.]
     76                              [ 6.  1.]] '''
     77 
     78 # hstack/vstack function can stack the matrix in h/v direction
     79 a = np.arange(6).reshape(2, 3)
     80 b = np.arange(6).reshape(2, 3)
     81 print(np.hstack((a, b))) '''[[0 1 2 0 1 2]
     82                              [3 4 5 3 4 5]] '''
     83 print(np.vstack((a, b))) '''[[0 1 2]
     84                              [3 4 5]
     85                              [0 1 2]
     86                              [3 4 5]] '''
     87 
     88 # hsplit/vsplit function can split the matrix in h/v direction
     89 x = np.arange(30).reshape(2, -1)
     90 print(x)    ''' [[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
     91                  [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]] '''
     92 # Split into 3 part (h direction, from one (2, 15) matrix to three (2, 5) matrixes)
     93 print(np.hsplit(x, 3))  '''[array([[ 0,  1,  2,  3,  4],
     94                                    [15, 16, 17, 18, 19]]),
     95                             array([[ 5,  6,  7,  8,  9],
     96                                    [20, 21, 22, 23, 24]]),
     97                             array([[10, 11, 12, 13, 14],
     98                                    [25, 26, 27, 28, 29]])] '''
     99 # Split in certain loction (split the matrix in location(after) column 3 and column 4)
    100 print(np.hsplit(x, (3, 4))) '''[array([[ 0,  1,  2],
    101                                        [15, 16, 17]]),
    102                                 array([[ 3],
    103                                        [18]]),
    104                                 array([[ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
    105                                        [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
    106 # Split into 3 part (v direction, from one (2, 15) matrix to two (1, 15) matrixes)
    107 print(np.vsplit(x, 2))  ''' [array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14]]),
    108                              array([[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
    109 
    110 # Fetch the max value position by row/column
    111 data = np.random.random((5, 4))
    112 print(data) ''' [[ 0.65419205  0.75953852  0.89856871  0.96162281]
    113                  [ 0.76341568  0.10488636  0.06186101  0.27698986]
    114                  [ 0.73737843  0.75305691  0.28705743  0.45542513]
    115                  [ 0.5534984   0.54420756  0.86250921  0.596653  ]
    116                  [ 0.24295898  0.28894731  0.58726507  0.39418991]] '''
    117 # argmax/argmin function can get the max/min value index by row/column
    118 index = data.argmax(axis=0)
    119 print(index)    # [1 0 0 0]
    120 # Get the max value by position
    121 # data.shape[1] to get the number of column
    122 print(data[index, range(data.shape[1])])    # [ 0.76341568  0.75953852  0.89856871  0.96162281]
    123 
    124 # Extend the matrix(like tile does)
    125 x = np.arange(0, 40, 10)
    126 print(x)    # [ 0 10 20 30]
    127 # tile(x, (a, b)) can change origin shape(c, d) --> (a*c, b*d)
    128 y = np.tile(x, (3, 5))
    129 print(y)    ''' [[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
    130                  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
    131                  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]] '''
    132 print(y.shape) # (3, 20)
    133 
    134 # Sort the array
    135 x = np.array([[4, 3, 5], [1, 2, 1]])
    136 print(x)    '''[[4 3 5]
    137                 [1 2 1]] '''
    138 print('--------')
    139 # Sort the number by row/column
    140 y = np.sort(x, axis=1)
    141 print(y)    '''[[3 4 5]
    142                 [1 1 2]] '''
    143 print('--------')
    144 x.sort(axis=1)
    145 print(x)    '''[[3 4 5]
    146                 [1 1 2]] '''
    147 print('--------')
    148 # argsort can return the sorted index list
    149 x = np.array([4, 3, 1, 2])
    150 y = np.argsort(x)
    151 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
    152 # Use the sorted index list to fetch value
    153 print('--------')
    154 print(x[y]) # [1, 2, 3, 4]
    View Code

    分段解释

    两个同维度矩阵的加减乘除、大小比较、开方、次幂、取整等操作,都将分别作用在各个元素上,点乘计算可以通过numpy.dot函数进行计算

     1 import numpy as np
     2 
     3 a = np.array([20, 30, 40, 50])
     4 b = np.arange(4)
     5 # For (matrix - matrix) will operate for each corresponding elements
     6 # Different shape can not be subtracted unless only one element
     7 c = a - b
     8 print(c)    # [20 29 38 47]
     9 # For (matrix (-/'**'/'<') number) will operate for each elements 
    10 c -= 1
    11 print(c)    # [19 28 37 46]
    12 b = b**2 # (b*b)
    13 print(b) # [0, 1, 4, 9]
    14 print(a<35) # [True, True, False, False]
    15 
    16 # * will multiply each element in corresponding position
    17 # dot will act the dot multiply for matrix(by using matrix_1.dot(matrix_2) or np.dot(matrix_1, matrix_2))
    18 x = np.array([[1, 1],
    19               [0, 1]])
    20 y = np.array([[2, 0],
    21               [3, 4]])
    22 print(x*y)  '''[[2 0]
    23                 [0 4]] '''
    24 print('-------')
    25 print(x.dot(y))     '''[[5 4]
    26                         [3 4]] '''
    27 
    28 print('-------')
    29 print(np.dot(x, y)) '''[[5 4]
    30                         [3 4]]'''
    31 
    32 # exp function to cal e^x for x in matrix
    33 # sqrt function to cal sqrt(x) for x in matrix
    34 m = np.arange(3)
    35 print(m)    # [0 1 2]
    36 print(np.exp(m))    # [ 1.          2.71828183  7.3890561 ]
    37 print(np.sqrt(m))   # [ 0.          1.          1.41421356]

    random模块可以生成随机元素的矩阵

     1 # floor/ceil function to round(down/up) number of each matrix
     2 x = 10*np.random.random((3, 4))
     3 print(x)    '''[[ 6.69732597  1.18238851  4.10109987  6.40797969]
     4                 [ 6.50193132  2.58724942  5.25748965  2.58338795]
     5                 [ 0.2798712   7.89760089  6.03544519  1.5176369 ]] '''
     6 print('-------')
     7 y = np.floor(x)
     8 print(y)     '''[[ 6.  1.  4.  6.]
     9                  [ 6.  2.  5.  2.]
    10                  [ 0.  7.  6.  1.]] '''
    11 print('-------')
    12 z = np.ceil(x)
    13 print(z)    ''' [[ 7.  2.  5.  7.]
    14                  [ 7.  3.  6.  3.]
    15                  [ 1.  8.  7.  2.]] '''

    ravel函数可以将矩阵展开成一维,T属性方法可以得到转置矩阵

     1 # ravel function can flatten a matrix into vector
     2 print(y.ravel())    # [ 6.  1.  4.  6.  6.  2.  5.  2.  0.  7.  6.  1.]
     3 # Shape value can change shape too
     4 y.shape = (6, 2)
     5 print(y)    ''' [[ 6.  1.]
     6                  [ 4.  6.]
     7                  [ 6.  2.]
     8                  [ 5.  2.]
     9                  [ 0.  7.]
    10                  [ 6.  1.]] '''
    11 print('-------')
    12 # T property function
    13 print(y.T)  ''' [[ 6.  4.  6.  5.  0.  6.]
    14                  [ 1.  6.  2.  2.  7.  1.]] '''
    15 print('-------')

    reshape函数若其中一个参数为-1则该维度由其他维度计算得到

    1 # If a dimension is given as -1, this dimension will be calculated automatically
    2 print(y.reshape(6, -1)) ''' [[ 6.  1.]
    3                              [ 4.  6.]
    4                              [ 6.  2.]
    5                              [ 5.  2.]
    6                              [ 0.  7.]
    7                              [ 6.  1.]] '''

    random模块可以生成随机元素的矩阵。

    1 # hstack/vstack function can stack the matrix in h/v direction
    2 a = np.arange(6).reshape(2, 3)
    3 b = np.arange(6).reshape(2, 3)
    4 print(np.hstack((a, b))) '''[[0 1 2 0 1 2]
    5                              [3 4 5 3 4 5]] '''
    6 print(np.vstack((a, b))) '''[[0 1 2]
    7                              [3 4 5]
    8                              [0 1 2]
    9                              [3 4 5]] '''

    堆叠以及切割操作

     1 # hsplit/vsplit function can split the matrix in h/v direction
     2 x = np.arange(30).reshape(2, -1)
     3 print(x)    ''' [[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
     4                  [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]] '''
     5 # Split into 3 part (h direction, from one (2, 15) matrix to three (2, 5) matrixes)
     6 print(np.hsplit(x, 3))  '''[array([[ 0,  1,  2,  3,  4],
     7                                    [15, 16, 17, 18, 19]]),
     8                             array([[ 5,  6,  7,  8,  9],
     9                                    [20, 21, 22, 23, 24]]),
    10                             array([[10, 11, 12, 13, 14],
    11                                    [25, 26, 27, 28, 29]])] '''
    12 # Split in certain loction (split the matrix in location(after) column 3 and column 4)
    13 print(np.hsplit(x, (3, 4))) '''[array([[ 0,  1,  2],
    14                                        [15, 16, 17]]),
    15                                 array([[ 3],
    16                                        [18]]),
    17                                 array([[ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
    18                                        [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''
    19 # Split into 3 part (v direction, from one (2, 15) matrix to two (1, 15) matrixes)
    20 print(np.vsplit(x, 2))  ''' [array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14]]),
    21                              array([[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])] '''

    获取最大值索引

     1 # Fetch the max value position by row/column
     2 data = np.random.random((5, 4))
     3 print(data) ''' [[ 0.65419205  0.75953852  0.89856871  0.96162281]
     4                  [ 0.76341568  0.10488636  0.06186101  0.27698986]
     5                  [ 0.73737843  0.75305691  0.28705743  0.45542513]
     6                  [ 0.5534984   0.54420756  0.86250921  0.596653  ]
     7                  [ 0.24295898  0.28894731  0.58726507  0.39418991]] '''
     8 # argmax/argmin function can get the max/min value index by row/column
     9 index = data.argmax(axis=0)
    10 print(index)    # [1 0 0 0]
    11 # Get the max value by position
    12 # data.shape[1] to get the number of column
    13 print(data[index, range(data.shape[1])])    # [ 0.76341568  0.75953852  0.89856871  0.96162281]

    将矩阵进行铺展操作

    1 # Extend the matrix(like tile does)
    2 x = np.arange(0, 40, 10)
    3 print(x)    # [ 0 10 20 30]
    4 # tile(x, (a, b)) can change origin shape(c, d) --> (a*c, b*d)
    5 y = np.tile(x, (3, 5))
    6 print(y)    ''' [[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
    7                  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
    8                  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]] '''
    9 print(y.shape) # (3, 20)

    矩阵的排序及排序索引

     1 # Sort the array
     2 x = np.array([[4, 3, 5], [1, 2, 1]])
     3 print(x)    '''[[4 3 5]
     4                 [1 2 1]] '''
     5 print('--------')
     6 # Sort the number by row/column
     7 y = np.sort(x, axis=1)
     8 print(y)    '''[[3 4 5]
     9                 [1 1 2]] '''
    10 print('--------')
    11 x.sort(axis=1)
    12 print(x)    '''[[3 4 5]
    13                 [1 1 2]] '''
    14 print('--------')
    15 # argsort can return the sorted index list
    16 x = np.array([4, 3, 1, 2])
    17 y = np.argsort(x)
    18 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
    19 # Use the sorted index list to fetch value
    20 print('--------')
    21 print(x[y]) # [1, 2, 3, 4]

    6 numpy常用函数

    完整代码

     1 import numpy as np
     2 
     3 # Build a matrix, shape is (2, 4, 2)
     4 matrix = np.array([[[1,2], [2,3], [3,4], [4,5]], 
     5                     [[1,2], [2,3], [3,4], [4,5]]])
     6 
     7 # arange function to quickly create an array(vector)
     8 # Note: the shape is (x, ), one dimension array
     9 vector = np.arange(15)
    10 print(vector)   # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    11 # reshape function to reshape an array
    12 matrix = vector.reshape(3, 5)
    13 print(matrix)   ''' [[ 0  1  2  3  4]
    14                      [ 5  6  7  8  9]
    15                      [10 11 12 13 14]] '''
    16 # shape is the row&column of matrix(array) - (3, 5)
    17 # ndim is the number of dimensions(axes) of matrix - 2
    18 # dtype is a numpy class, dtype.name is the type name of matrix - int32
    19 # size is the total number of elements of matrix - 15
    20 print(matrix.shape, matrix.ndim, matrix.dtype.name, matrix.size) # (3, 5) 2 int32 15
    21 # arrange(from, to, step)
    22 vector = np.arange(10, 30, 5)
    23 print(vector)   # [10 15 20 25]
    24 vector = np.arange(1, 3, 0.5)
    25 print(vector)   # [ 1.   1.5  2.   2.5]
    26 
    27 # Init a matrix with all elements 0, default type is float64
    28 matrix = np.zeros((3, 4), dtype=np.float64)
    29 print(matrix)   ''' [[ 0.  0.  0.  0.]
    30                      [ 0.  0.  0.  0.]
    31                      [ 0.  0.  0.  0.]] '''
    32 # Init a matrix with all elements 1
    33 matrix = np.ones((2, 3, 4), dtype=np.int32)
    34 print(matrix)   ''' [[[1 1 1 1]
    35                       [1 1 1 1]
    36                       [1 1 1 1]]
    37                       
    38                      [[1 1 1 1]
    39                       [1 1 1 1]
    40                       [1 1 1 1]]]'''
    41 
    42 # random function will return a matrix with random number(between 0 to 1)
    43 # random((row, column))
    44 rd = np.random.random((2, 3))
    45 print(rd)   ''' [[ 0.45595053  0.69816822  0.30391984]
    46                  [ 0.22757757  0.725762    0.84856338]] '''
    47 
    48 # Get the matrix with some same step numbers,
    49 # 得到相应数量的等差数列
    50 # linspace(from, to, number)
    51 # Notice whether the num should be plus one
    52 matrix = np.linspace(0, 2, 11)
    53 print(matrix)   # [ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2. ]
    54 
    55 # sin/cos function can calculate the sin/cos of each elements
    56 from numpy import pi
    57 matrix = np.linspace(0, 2*pi, 9)
    58 print(matrix)   # [ 0.          0.78539816  1.57079633  2.35619449  3.14159265  3.92699082  4.71238898  5.49778714  6.28318531]
    59 matrix = np.sin(matrix)
    60 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]
    View Code

    分段解释

    建立一个矩阵,可以通过array或arange函数生成,对于一位矩阵,其shape值为(x, )

     1 import numpy as np
     2 
     3 # Build a matrix, shape is (2, 4, 2)
     4 matrix = np.array([[[1,2], [2,3], [3,4], [4,5]], 
     5                     [[1,2], [2,3], [3,4], [4,5]]])
     6 
     7 # arange function to quickly create an array(vector)
     8 # Note: the shape is (x, ), one dimension array
     9 vector = np.arange(15)
    10 print(vector)   # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    11 # reshape function to reshape an array
    12 matrix = vector.reshape(3, 5)
    13 print(matrix)   ''' [[ 0  1  2  3  4]
    14                      [ 5  6  7  8  9]
    15                      [10 11 12 13 14]] '''
    16 # shape is the row&column of matrix(array) - (3, 5)
    17 # ndim is the number of dimensions(axes) of matrix - 2
    18 # dtype is a numpy class, dtype.name is the type name of matrix - int32
    19 # size is the total number of elements of matrix - 15
    20 print(matrix.shape, matrix.ndim, matrix.dtype.name, matrix.size) # (3, 5) 2 int32 15
    21 # arrange(from, to, step)
    22 vector = np.arange(10, 30, 5)
    23 print(vector)   # [10 15 20 25]
    24 vector = np.arange(1, 3, 0.5)
    25 print(vector)   # [ 1.   1.5  2.   2.5]

    生成一个全零/一矩阵

     1 # Init a matrix with all elements 0, default type is float64
     2 matrix = np.zeros((3, 4), dtype=np.float64)
     3 print(matrix)   ''' [[ 0.  0.  0.  0.]
     4                      [ 0.  0.  0.  0.]
     5                      [ 0.  0.  0.  0.]] '''
     6 # Init a matrix with all elements 1
     7 matrix = np.ones((2, 3, 4), dtype=np.int32)
     8 print(matrix)   ''' [[[1 1 1 1]
     9                       [1 1 1 1]
    10                       [1 1 1 1]]
    11                       
    12                      [[1 1 1 1]
    13                       [1 1 1 1]
    14                       [1 1 1 1]]]'''

    随机矩阵及等差矩阵等

     1 # random function will return a matrix with random number(between 0 to 1)
     2 # random((row, column))
     3 rd = np.random.random((2, 3))
     4 print(rd)   ''' [[ 0.45595053  0.69816822  0.30391984]
     5                  [ 0.22757757  0.725762    0.84856338]] '''
     6 
     7 # Get the matrix with some same step numbers,
     8 # 得到相应数量的等差数列
     9 # linspace(from, to, number)
    10 # Notice whether the num should be plus one
    11 matrix = np.linspace(0, 2, 11)
    12 print(matrix)   # [ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2. ]
    13 
    14 # sin/cos function can calculate the sin/cos of each elements
    15 from numpy import pi
    16 matrix = np.linspace(0, 2*pi, 9)
    17 print(matrix)   # [ 0.          0.78539816  1.57079633  2.35619449  3.14159265  3.92699082  4.71238898  5.49778714  6.28318531]
    18 matrix = np.sin(matrix)
    19 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

  • 相关阅读:
    python,jsonpath提取json数据
    [.Net] Web API 本地化与全球化
    缺省源
    组合恒等式
    20210925衡阳八中多校联测
    codeforces赛后总结——1556D. Take a Guess
    codeforces赛后总结——1556C. Compressed Bracket Sequence
    阿里云(Ubuntu20.04)搭建wordpress全流程——附图超详细版
    Linux常用命令行操作
    阿里云服务器增加监听端口
  • 原文地址:https://www.cnblogs.com/stacklike/p/8253898.html
Copyright © 2011-2022 走看看