• NUMPY


    综述

    numpy:支持大量的维度数组与矩阵运算
    ndarray:对象(存放同类型元素的多维数组)

    • Numpy 常用的基本数据类型

    np.bool_, np.bool8
    np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64
    np.uint, np.uint8, np.uint16, np.uint32, np.uint64
    np.float_, np.float16, np.float32, np.float64
    np.complex_, np.complex64, np.complex128

    • 字符串代码(每一个内建类型都有唯一定义它的字符串代码)

    b布尔型 i(有符号)整型 u(无符号)整型
    f浮点型 c复数浮点型
    m时间间隔(timedelta) M日期时间(datetime)
    O(python)对象
    S,a(byte)字符串
    U:Unicode
    V:(void)原始数据

    • 数据类型对象 dtype
    dt = np.dtype(np.int32)
    dt_1 = np.dtype('i8') # int8, int16, int32, int64 可以使用字符串 'i1','i2','i4','i8'
    dt_2 = np.dtype('>f4') # 大端法(高位组放在最前面)
    dt_3 = np.dtype([('name','S20'), ('age','), ("mark", '>f4')])  # 结构化数据类型
    
    students = np.array([("lizhong", 25, 95), ("xiaohong", 19, 89.5), ('zhangjun', 30, 60)], dtype=dt_3)
    print(students)
    print(students['age'])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Ndarray attribute
    a = np.arange(24).reshape(2,4,3)
    print(a)
    print(a.ndim, a.shape, a.size, a.dtype, a.itemsize)
    print(a.flags)  # 内存信息
    print(a.real, a.imag)
    print(a.data)  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1. Create Ndarray

    • np.array()
    a = np.array([[1,2,3],[4,5,6]], dtype=float, ndmin=3).reshape(3, 2)  # a.dtype=float64 
    
    • 1
    • Create fixed ndarray
    a = np.empty([3,2], dtype='i1', order='C')  # 随机填充
    b = np.empty([2,2,3], dtype=[('x', "S1"), ("y",'>i4'), ('z', np.float32)], order='C')
    c = np.zeros([4,5], dtype=float, order='F')  # 以0填充
    d = np.ones([1,2,3], dtype=np.int16, order='F')  # 以1填充
    e = np.eye(3)  # 对角线为1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • Create Ndarray from exist list, tuple, buffer, iter
    x = [(1,2,3),(4,5,6)]
    y = np.asarray(x, dtype=np.int16, order="C")
    
    buf = b'Hello World'
    from_buf = np.frombuffer(buf, dtype='S1', count=-1, offset=0)
    
    it = iter(range(6))
    from_it = np.fromiter(it, dtype=float, count=-1).reshape(2,3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Create Ndarray from range
    ar = np.arange(0, 10, 2, dtype=float)  # 数值范围
    li = np.linspace(1, 10, 10, endpoint=True, retstep=True, dtype=int)  # 等差数列
    lg = np.logspace(1.0, 2.0, num=10, endpoint=True, base=10.0, dtype=None)  # 等比数列
    
    • 1
    • 2
    • 3

    2 slice and index

    • slice
    a = np.arange(10)
    sl = slice(2,7,2)  # [start, stop, step]
    print(a[sl])  # print(a[2:7:2])
    
    ## (:)冒号 
    b = np.array([[1,2,3],[3,4,5],[4,5,6]])
    print(b[1:]) # b[1], b[2]
    print("b[1]=", b[1])
    print("b[2]=", b[2])
    
    ## 省略号(...)使得选择元组的长度与数组的维度相同
    print(b[...,1])  # 第2列元素
    print(b[1,...])  # 第2行元素
    print(b[...,1:])  # 第2列及剩下的所有元素(第3列)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 数组索引
    # 数组索引(0,0) (1,1) (2,0)
    x = np.array([[1,2],[3,4],[5,6]])
    y = x[[0,1,2],[0,1,0]]
    print(y)
    
    # 获取4x3数组中四个角的元素,行索引是[0.0]和[3,3].而列索引是[0.2]和[0,2]
    x = np.arange(12).reshape(4,3)
    rows = np.array([[0,0],[3,3]])
    cols = np.array([[0,2],[0,2]])
    y = x[rows, cols] # 2*2        y = x[[0,0,3,3],[0,2,0,2]] 1*4
    print(y)
    
    # 借助省略号(...)与索引数组组合
    a = np.array([[1,2,3],[4,5,6],[7,8,9]])
    b = a[1:3, 1:3]  # 第2,3行  第2,3列
    c = a[1:3, [1,2]] # 第2,3行 第2,3列
    d = a[...,1:] # 第2,3列,行与数组维度相同
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 布尔索引
    x = np.arange(12).reshape(4,3)
    print(x[x>5])  # 获取大于5的元素
    a = np.array([1, 2+6j, 5, 3.5+5j])
    print(a[np.iscomplex(a)])
    b = np.array([np.nan, 1, 2, np.nan, 3,4,5])
    print(b[~np.isnan(b)])  # 过滤掉非复数元素
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 花式索引(利用整数数组进行索引)
    x = np.arange(32).reshape((8,4))
    print(x[[4,2,1,7]]) # 顺序索引数组(第4,2,1,7行)
    print(x[[-4,-2,-1,-7]]) # 传入倒序索引数组(第4,6,7,1行)
    
    • 1
    • 2
    • 3

    3. Broadcast

    对不同形状的数组进行数值计算的方式。

    a = np.arange(12).reshape(4,3)
    b = np.array([1,1,1])
    c = np.tile(b, [4,1])  # 重复b的各个维度
    print(a+b)
    print(a+c)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4. Iterable

    a = np.arange(6).reshape(2,3)
    for x in np.nditer(a):
        print(x, end=",")
    for x in np.nditer(a.T):  # a与a.T的遍历顺序相同,由于在内存中的存储顺序相同
        print(x, end=",")
    for x in np.nditer(a.T.copy(order='C')):  # 与a的遍历顺序不同,由于在内存中的存储顺序不一样
        print(x, end=",")
    for x in np.nditer(a.T.copy(order="F")):
        print(x, end=",")
        
    print() # 通过显示设置,强制nditer对象使用某种顺序
    for x in np.nditer(a, order='C'):  
        print(x, end=",")
    for x in np.nditer(a, order="F"):
        print(x, end=",")
    
    print()# nditer 对象遍历权限 (read-only、read-write、write-only)
    for x in np.nditer(a, op_flags=['readwrite']):
        x[...] = 2*x
    print(a)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 外部循环
      b = np.arange(0, 60, 5).reshape(3,4)
      print(b)
      for x in np.nditer(b, flags=[‘external_loop’], order=“F”): # 迭代器将每列组合为一维数组
      print(x, end=“,”)

    • 广播迭代
      c = np.array([1,2,3,4],dtype=int)
      for x,y in np.nditer([b, c]):
      print(“%d:%d” % (x, y), end=", ")

    3. Ndarray Operations

    3.1 修改数组形状

    ## reshape
    a = np.arange(8).reshape(4, 2)
    for row in a:
        print(row)
    
    ## flat:  A 1-D iterator over the array.
    for element in a.flat:
        print(element)
    print(a.T.flat[3]) # 6
    
    ## flatten:  Return a copy of the array collapsed into one dimension.
    print(a.flatten())
    print(a.flatten(order='F'))
    
    ## ravel:  Return a contiguous flattened array.  view of the array, modifications affect the original data
    print(a.ravel())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.2 翻转数组

    • np.transpose(a, axes=None)

    Reverse or permute the axes of an array; returns the modified array.

    a = np.arange(12).reshape(3,4)
    print(np.transpose(a)) 
    print(a.T)
    
    • 1
    • 2
    • 3
    • np.rollaxis(a, axis, start=0)

    Roll the specified axis backwards, until it lies in a given position.

    a = np.arange(8).reshape(2,2,2)
    print(a)
    print(np.where(a==6))
    print(a[1,1,0])
    
    b = np.rollaxis(a, 2, 0)  # 将轴2 滚到轴0(宽度到深度)
    print(b)
    print(np.where(b==6))
    print(b[0,1,1])
    
    c = np.rollaxis(a, 2,1) # 将轴2 滚动到轴1(宽度到高度)
    print(c)
    print(np.where(c==6))
    print(c[1,0,1])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • np.swapaxes(a, axis1, axis2)

    Interchange two axes of an array.

    x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])
    y = np.swapaxes(x, 2, 0) # 交换轴0(深度方向)到轴2(宽度方向)
    print(y)
    
    • 1
    • 2
    • 3

    3.3 修改数组维度

    • broadcast()

    Produce an object that mimics broadcasting.

    x = np.array([[1],[2],[3]])
    y = np.array([4,5,6])
    b = np.broadcast(x,y) # 对y广播x
    
    r,c = b.iters
    print(next(r), next(c))
    print(next(r), next(c))
    print(b.shape)  # 3x3
    
    b = np.broadcast(x, y)  # 若以上一步的广播结果+next后,结果不同
    c = np.empty(b.shape)
    c.flat = [u+v for (u,v) in b]
    print(c)
    print(x+y)  # built_in broadcasting
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • np.broadcast_to(array, shape, subok=False)

    Broadcast an array to a new shape.

    a = np.arange(4)
    b = np.broadcast_to(a,(4,4))
    print(a.shape, b.shape)  # (4,) (4, 4)
    
    • 1
    • 2
    • 3
    • np.expand_dims(a, axis)

    Insert a new axis that will appear at the axis position in the expanded array shape.

    x = np.array(([1,2],[3,4]))
    y = np.expand_dims(x, axis=0)
    print(x.shape, y.shape)  # (2, 2) (1, 2, 2)
    y = np.expand_dims(x, axis=1)
    print(x.shape, y.shape)  # 2, 2) (2, 1, 2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • a.squeeze(axis=None)

    Remove single-dimensional entries from the shape of a.

    a = np.arange(6).reshape(1,2,3)
    b = np.squeeze(a, axis=0)
    print(a.shape, b.shape)  # (1, 2, 3) (2, 3)
    
    • 1
    • 2
    • 3

    3.4 连接数组

    • np.concatenate((a1, a2, ...), axis=0, out=None)

    Join a sequence of arrays along an existing axis.

    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    c = np.concatenate((a,b),axis=0)
    d = np.concatenate((a,b),axis=1)
    
    • 1
    • 2
    • 3
    • 4
    • np.stack(arrays, axis=0, out=None)

    Join a sequence of arrays along a new axis.

    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    c = np.stack((a,b), 0)  # 沿轴0 堆叠两数组
    d = np.stack((a,b), 1)
    
    • 1
    • 2
    • 3
    • 4
    • np.hstack(tup)

    Stack arrays in sequence horizontally (column wise).

    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    c = np.hstack((a,b))
    
    • 1
    • 2
    • 3
    • np.vstack(tup):

    Stack arrays in sequence vertically (row wise).

    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    c = np.vstack((a,b))
    
    • 1
    • 2
    • 3

    3.5 分割数组

    • np.split(ary, indices_or_sections, axis=0)

    Split an array into multiple sub-arrays as views into ary.

    a = np.arange(9)
    b = np.split(a,3) # 平均切分
    c = np.split(a, [4, 7])  # 按指定位置分割
    
    a = np.arange(16).reshape(4,4)
    b = np.split(a, 2, 0) # 沿水平方向分割
    c = np.split(a, 2, 1)  # 沿垂直方向分割
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • np.hsplit(ary, indices_or_sections)

    Split an array into multiple sub-arrays horizontally (column-wise).

    a = np.floor(10*np.random.random((2,6)))
    np.hsplit(a, 3)
    
    • 1
    • 2
    • np.vsplit(ary, indices_or_sections)

    Split an array into multiple sub-arrays vertically (row-wise).

    b = np.arange(16).reshape(4,4)
    np.vsplit(b, 2)
    
    • 1
    • 2

    3.6 add or remove elements

    • np.resize(a, new_shape)

    Return a new array with the specified shape.

     a=np.array([[0,1],[2,3]])
     print(np.resize(a, (2,3))
     print(np.resize(a, (1,4))
     print(np.resize(a, (2,4))
    
    • 1
    • 2
    • 3
    • 4
    • np.append(arr, values, axis=None)

    Append values to the end of an array.

    a = np.array([[1,2,3],[4,5,6]])
    b = np.append(a, [7,8,9])  # 向数组添加元素
    c = np.append(a, [[7,8,9]], axis=0)  # 列 添加元素
    d = np.append(a, [[5,5,5], [7,8,9]], axis=1)  # 行 添加元素
    
    • 1
    • 2
    • 3
    • 4
    • np.insert(arr, obj, values, axis=None)

    Insert values along the given axis before the given indices.

    a = np.array([[1,2,3],[4,5,6]])
    b = np.insert(a, 3, [11,12])
    c = np.insert(a,1,[11], axis=0)  # 沿轴0 广播
    d = np.insert(a,1,11, axis=1)  # 沿轴1 广播
    a,b,c,d
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • np.delete(arr, obj, axis=None)

    Return a new array with sub-arrays along an axis deleted.

    a = np.arange(12).reshape(3,4)
    b = np.delete(a, 5)
    c = np.delete(a, 1, axis=1)  # 删除第二列
    d = np.delete(a, np.s_[::2])
    a,b,c,d
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • np.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)

    Returns the sorted unique elements of an array.

    a = np.array([5,2,6,2,7,5,6,8,2,9])
    u,indics = np.unique(a, return_index=True)  # 去重后的数组,及在原数组中的索引
    u,indics = np.unique(a, return_inverse=True)  # 去重后的数组,及原数组中元素在该数组中的索引
    u[indics]  # 重构原数组
    u,indics = np.unique(a, return_counts=True) # 去重后的数组,及在原数组中的数量
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4. 位运算

    • bitwise_and,bitwise_or,invert,binary_repr
    print(bin(13),bin(17))  # 0b1101 0b10001
    print(np.bitwise_and(13,17))  # 1
    print(np.bitwise_or(12,17))  # 29
    print(np.invert(np.array([13], dtype=np.uint8)))  # [242]
    print(np.binary_repr(13, width=8))  # 00001101
    print(np.binary_repr(242, width=8))  # 11110010
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • left_shift,right_shift
    print(np.left_shift(10,2))  # 40
    print(np.right_shift(40,2))  # 10
    
    • 1
    • 2

    5. 字符串函数

    定义在 numpy.char

    print(np.char.add(["hello", 'Google'],[" world", ' Search']))  # Return element-wise string concatenation for two arrays of str or unicode.
    print(np.char.multiply("RUNOOB ",3))  # Return (a * i), that is string multiple concatenation,element-wise.
    
    print(np.char.center("RUNOOB", 20, fillchar='*'))  # Return a copy of `a` with its elements centered in a string of length `width`.
    
    print(np.char.capitalize('runoob'))  # Return a copy of `a` with only the first character of each element capitalized.
    print(np.char.title("i like runoob"))  #  Title case words start with uppercase characters, all remaining cased characters are lowercase.
    
    print(np.char.lower(["RUNoob","GOOgle"]))
    print(np.char.upper(['runoob','facebook']))
    
    print(np.char.split('i like runoob?'))
    print(np.char.split('www.runoob.com', sep='.'))
    print(np.char.splitlines('i\nlike\r\nrunoob?'))  # 以换行符作为分隔符来分割字符串
    print(np.char.splitlines('i\rlike\n\rrunoob?'))  # "\n","\r","\r\n" 都可用作换行符
    
    print(np.char.strip(["arunooba","admin","java"],"a"))  # 用于移除开头或结尾处的特定字符
    print(np.char.join([':','-'],['runoob','google']))  # 通过指定分隔符来连接数组中的元素或字符串
    
    print(np.char.replace('i like runoob','oo','cc'))  # 使用新字符串替换字符串中的所有子字符串
    
    a = np.char.encode('runoob','cp500')
    print(a)
    print(np.char.decode(a, 'cp500'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    6. 数学函数

    • 三角函数
    a = np.array([0, 30, 45, 60, 90])  # 角度
    print(np.around(np.sin(a*np.pi/180), 1))  # 通过乘 np.pi/180 角度转化为弧度
    print(np.around(np.cos(a*np.pi/180), 1))
    print(np.tan(a*np.pi/180))
    
    • 1
    • 2
    • 3
    • 4
    • 反三角函数
    a = np.array([0,30,45,60, 90])  # 角度
    sin = np.sin(a*np.pi/180) 
    inv = np.arcsin(sin)  # 返回值以弧度为单位
    print(np.degrees(inv))  # 将弧度转换为角度
    
    cos = np.cos(a*np.pi/180)
    inv = np.arccos(cos)
    print(np.degrees(inv))
    
    tan = np.tan(a*np.pi/180)
    inv = np.arctan(tan)
    print(np.degrees(inv))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 舍入函数
    a = np.array([1.0, 15.55, 123, 0.467,-2.532, -1.34])
    np.around(a, decimals=1)  # 保留小数点后一位四舍五入
    np.around(a, decimals=-1) # 小数点前一位四舍五入
    np.floor(a)  # 向下取整
    np.ceil(a)  # 向上取整
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 算术函数
    a = np.arange(9, dtype=np.float_).reshape(3,3)
    b = np.array([10,10,10])
    print(np.add(a,b))       # 广播相加
    print(np.subtract(a,b))  # 广播相减
    print(np.multiply(a, b)) # 广播相乘
    print(np.divide(a,b))    # 广播相除
    a,b
    
    a = np.array([0.25, 1.33, 1, 100])
    print(np.reciprocal(a))  # 返回逐个元素的倒数
    
    a = np.array([2,3,4])
    b = np.array([1,2,3])
    print(np.power(a, 2))
    print(np.power(a, b))
    
    a = np.array([10, 20, 30])
    b = np.array([3,5,7])
    print(np.mod(a, b))
    print(np.remainder(a,b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 统计函数
    # amax(a, axis=None, out=None, keepdims=, initial=, where=) 
    ## Return the maximum of an array or maximum along an axis.
    a = np.array([[3,7,5],[8,4,3],[2,4,9]])
    print(np.amax(a))  # 数组中最大值 9
    print(np.amax(a, 1))  # 各行最大值 [7 8 9]
    print(np.amax(a, 0))  # 各列最大值 [8 7 9]
    
    # amin(a, axis=None, out=None, keepdims=, initial=, where=)
    ## Return the minimum of an array or minimum along an axis.
    a = np.array([[3,7,5],[8,4,3],[2,4,9]])
    print(np.amin(a))  # 数组中最小值 2
    print(np.amin(a, 1)) # 各行最小值 [3 3 2]
    print(np.amin(a, 0))  # 各列最小值 [2 4 3]
    
    # ptp(a, axis=None, out=None, keepdims=)
    ##  Range of values (maximum - minimum) along an axis.
    a = np.array([[3,7,5],[8,4,3],[2,4,9]])
    print(np.ptp(a))          # 数组中(最大值-最小值)即9-2=7
    print(np.ptp(a, axis=1))  # 各行(最大值-最小值) [4 5 7]
    print(np.ptp(a, axis=0))  # 各列(最大值-最小值) [6 3 6]
    
    # percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)   分位数值
    a = np.array([[10,7,4],[3,2,1]])
    print(np.percentile(a, 50))
    print(np.percentile(a, 50, axis=1))  # 各行
    print(np.percentile(a, 50, axis=0))  # 各列
    print(np.percentile(a, 50, axis=1, keepdims=True))  # 保持维度不变
    
    # median(a, axis=None, out=None, overwrite_input=False, keepdims=False)  中位数
    a = np.array([[30,65,70],[80,95,10],[50,90,60]])
    print(np.median(a))
    print(np.median(a, axis=0)) # 轴0(列)的中位数
    print(np.median(a, axis=1)) # 轴1(行)的中位数
    
    # mean(a, axis=None, dtype=None, out=None, keepdims=)  算术平均值
    a = np.array([[1,2,3],[3,4,5],[4,5,6]])
    print(np.mean(a))
    print(np.mean(a, axis=0))  # 轴0(列)算术平均值
    print(np.mean(a, axis=1))  # 轴1(行)算术平均值
    
    # average(a, axis=None, weights=None, returned=False)  加权平均值
    a = np.array([1,2,3,4]).reshape(2,2)
    np.average(a)
    np.average(a, weights=[[4,3],[2,1]])
    np.average(a, weights=[1,2], axis=0) # 轴0(列)加权平均值
    np.average(a, weights=[2,1], axis=1) # 轴1(行)加权平均值
    
    # std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=)  标准差
    a = np.array([1,2,3,4]).reshape(2,2)
    print(np.std(a))
    print(np.std(a, axis=0)) # 轴0(列)标准差
    print(np.std(a, axis=1)) # 轴1(行)标准差
    
    # var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=)  方差
    a = np.array([1,2,3,4]).reshape(2,2)
    print(np.var(a))
    print(np.var(a, axis=0))  # 轴0(列)方差
    print(np.var(a, axis=1))  # 轴1(行)方差
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 排序函数
    # sort(a, axis=-1, kind=None, order=None)
    a = np.array([[3,7],[9,1]])
    print(np.sort(a))
    print(np.sort(a, axis=0))  # 按轴0(列)排序
    print(np.sort(a, axis=1))  # 按轴1(行)排序
    
    dt = np.dtype([('name','S10'),("age", int)])  # 结构化数据类型
    a = np.array([('raju',31),("anil",25),('ravi',17),("amar",27)], dtype=dt)
    print(np.sort(a, order='name'))
    
    
    # argsort(a, axis=-1, kind=None, order=None)
    ## Returns the indices that would sort an array.
    x = np.array([3,1,2])
    y = np.argsort(x)
    y, x[y]  # 以排序后的顺序重构原数组
    
    
    # lexsort(keys, axis=-1)
    ## Perform an indirect stable sort using a sequence of keys.
    a = [1,5,1,4,3,4,4] # First column
    b = [9,4,0,4,0,2,1] # Second column
    ind = np.lexsort((b,a)) # Sort by a, then by b, Return indices 
    print(ind)  # [2 0 4 6 5 3 1]
    [(a[i],b[i]) for i in ind]  # [(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]
    
    
    # sort_complex(a) 
    ##  Sort a complex array using the real part first, then the imaginary part.
    print(np.sort_complex([1+2j, 2-1j, 3-2j, 3+2j, -1+3j]))  # [-1.+3.j  1.+2.j  2.-1.j  3.-2.j  3.+2.j]
    
    • 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
    • 30
    • 条件筛选函数
    # partition(a, kth, axis=-1, kind='introselect', order=None)
    ##  Return a partitioned copy of an array.
    a = np.array([3, 4, 2, 1])
    print(np.partition(a, 3))  # [2, 1, 3, 4]
    print(np.partition(a, (1, 3)))  # [1, 2, 3, 4]
    
    # argpartition(a, kth, axis=-1, kind='introselect', order=None)
    ## Array of indices that partition `a` along the specified axis.
    x = np.array([3, 4, 2, 1])
    print(x[np.argpartition(x, 3)])
    print(x[np.argpartition(x, (1, 3))])
    
    # argmax(a, axis=None, out=None)
    ## Returns the indices of the maximum values along an axis.
    a = np.array([[30, 40, 70],[80, 20, 10], [50,90,60]])
    np.argmax(a)
    np.argmax(a, axis=0) # 沿轴0(列)的最大值索引   第1列,第2行
    np.argmax(a, axis=1) # 沿轴1(行)的最大值索引   第2行,第1列
    
    # argmin(a, axis=None, out=None)
    ## Returns the indices of the minimum values along an axis.
    np.argmin(a)
    np.argmin(a, axis=0)
    np.argmin(a, axis=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    # nonzero() 
    ## Return the indices of the elements that are non-zero.
    a = np.array([[30,40,0],[0,20,10],[50,0,60]])
    print(np.nonzero(a))
    a[np.nonzero(a)]
    
    # where(condition, [x, y])
    ## Return elements chosen from `x` or `y` depending on `condition`.
    x = np.arange(9.).reshape(3,3)
    y = np.where(x>3)
    x[y]
    np.where(x==6)
    
    # extract(condition, arr)
    ## Return the elements of an array that satisfy some condition.
    x = np.arange(9.).reshape(3,3)
    condition = np.mod(x, 2)==0  # 定义条件,选择偶数
    np.extract(condition, x)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 字节交换

    E.g:data = 0x 00 12 34 56
    大端模式:数据的高字节保存在内存的低地址中,数据的低字节保存在内存的高地址中。[00 12 34 56] (阅读习惯一直)
    小端模式:数据的高字节保存在内存的高地址中,数据的高字节保存在内存的低地址中。[56 43 12 00]

    • np.ndarray.byteswap()

    将 Ndarray 中每个元素中的字节进行大小端转换。

    a = np.array([1, -1, 256, 8755], dtype=np.int16)
    print(a)
    print(list(map(hex, a)))  # 以十六进制表示内存中的数据
    a.byteswap(True)
    print(a)
    print(list(map(hex, a)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    7. 副本和视图

    • 赋值操作不产生副本
    a = np.arange(6).reshape(2,3)
    b = a
    print(id(a))  # id 相同
    print(id(b))
    b[0,0] = 100  # 修改值影响
    print(a)
    print(b)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 视图和浅拷贝
    a = np.arange(6).reshape(3,2)
    b = a.view()
    print(id(a))
    print(id(b))  # id不同
    b[0, 0] = 100  # 修改值影响
    print(a)
    print(b)
    b.shape = 1,6
    print(a)  # 修改维度不影响
    print(b)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 切片创建视图
    a = np.arange(12)
    b = a[3:]
    c = a[3:]  # 切片创建视图,,修改数据会影响到原始数组
    b[1] = 123
    c[2] = 234
    print(id(b),id(c),id(a[3:]))  # id不同(区别于赋值引用)
    b is c  # False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 副本和深拷贝
    a = np.array([[10, 10], [2,3], [4,5]])
    b = a.copy()  # 修改副本数据,不会影响原始数据
    print(b is a)  # False
    print(id(a), id(b))  # 不同
    b[0, 0] = 100
    a, b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    7. 矩阵库 Matrix

    • ndarray 与 matrix 区别

    ndarray 可以是任意维数据,mat只能是2维数据
    矩阵乘法与逐项乘法不同

    # ndarray的dot和a@b是矩阵乘法, a*b和multiply()是逐项相乘
    a = np.array([[1, 2], [3, 4]])
    b = np.array([[5, 6], [7, 8]])
    print('dot =',np.dot(a, b)) # [[1*5+2*7=19  1*6+2*8=22], [3*5+4*7=43  3*6+4*8=50]] 矩阵乘法
    print('@ =',a@b)
    print('* =',a*b)
    print('mul =', np.multiply(a,b), '\n')
    
    
    # matrix的dot和c*d,c@d是矩阵乘法, multiply()是逐项相乘
    c = np.mat([[1,2], [3,4]])
    d = np.mat([[5,6], [7,8]])
    print('dot =', np.dot(c, d))
    print('* =', c*d)
    print('@ =', c@d)
    print('mul =', np.multiply(c, d))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • Matrix 定义
    import numpy.matlib as mb
    a = mb.empty((2,2))  # mb.zeros(), mb.ones(), mb.eye(), mb.identity(), mb.rand(), 
    print(a, type(a))
    
    b = np.mat((2,3))
    c = np.array([(2,3)])
    print(b, type(b))
    print(c, type(c))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 二者转换
    import numpy.matlib as mb
    
    a = np.mat('1,2;3,4')
    print(a, type(a))
    b = np.asarray(a)  # matrix->Ndarray
    print(b, type(b))
    c = np.asmatrix(b) # Ndarr
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8. 线性代数

    内积(Inner product)和点积(dot product)的区别:

    • 内积:一般是用在内积空间中的
    • 点积:一般是用在欧几里得空间中的
    • 欧几里得空间是内积空间的一种特殊情况。
    • dot(a, b, out=None)

    Dot product of two arrays. 两数组点积

    # 1-D
    a = np.array([1, 2, 3])
    b = np.array([3, 2, 1])
    print(np.dot(a, b))  # 向量内积 inner product of vectors
    
    # 2-D
    a = np.array([[0, 1], [2, 3]])
    b = np.array([[4, 5], [6, 7]])
    print(np.dot(a, b))  # E.q: np.matmul(a,b) or a@b    矩阵乘积法(与np.inner不同)
    
    # 0-D
    a = np.array([[0, 1], [1, 2]])
    print(np.dot(a, 2))  # E.q: np.multiply(a, 2) or a*2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • vdot(a, b)

    Return the dot product of two vectors.

    a = np.array([[0, 1], [2, 3]])
    b = np.array([[4, 5], [6, 7]])
    print(np.vdot(a, b))  # 0*4 + 1*5 + 2*6 + 3*7 = 38   对应项相乘
    
    • 1
    • 2
    • 3
    • inner(a,b)

    Inner product of two arrays.

    a = np.array([[0, 1], [2, 3]])
    b = np.array([[4, 5], [6, 7]])
    print(np.inner(a, b))  # [[0*4+1*5  0*6+1*7], [2*4+3*5  2*6+3*7]   [[5 7],[23 33]]   对应项相乘
    
    • 1
    • 2
    • 3
    • matmul()
    a = np.array([[0, 1], [2, 3]])
    b = np.array([[4, 5], [6, 7]])
    print(np.dot(a, b))
    print(np.matmul(a, b))  # 与 np.dot() 都是矩阵乘积法
    
    • 1
    • 2
    • 3
    • 4
    • linalg.det(a)

    Compute the determinant of an array . 计算矩阵的行列式值

    a = np.array([[1,2],[3,4]])
    print(np.linalg.det(a))  # -2
    
    • 1
    • 2
    • lnalg.solve()

    Solve a linear matrix equation, or system of linear scalar equations. 求解线性方程组

    a = np.array([[3,1], [1,2]])
    b = np.array([9,8])
    x = np.linalg.solve(a, b)
    print(x)  # array([2.,  3.])
    ## 3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8`
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • linalg.inv()

    Compute the (multiplicative) inverse of a matrix.

    x = np.array([[1,2], [3,4]])
    y = np.linalg.inv(x)
    print(np.dot(x, y))
    
    • 1
    • 2
    • 3

    9. IO

    • save/load
    a = np.array([1,2,3,4,5])
    np.save('outfile.npy',a)
    b = np.load('outfile.npy')
    
    • 1
    • 2
    • 3
    • savez()

    Save several arrays into a single file in uncompressed .npz format.

    a = np.array([[1,2,3],[4,5,6]])
    b = np.arange(0,1.0,0.1)
    c = np.sin(b)
    np.savez("runoob.npz", a, b, sin_array=c)  # c使用关键字参数
    r = np.load("Runoob.npz")
    r.files
    # r['arr_0']
    # r['arr_1']
    # r["sin_array"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)

    Save an array to a text file.

    a = np.array([1,2,3,4,5])
    np.savetxt('out.txt', a)
    b = np.loadtxt('out.txt')
    
    a = np.arange(0, 10, 0.5).reshape(4, -1)
    np.savetxt('out_1.txt', a, fmt="%d", delimiter=",") # 保存为整数,以逗号分隔
    b = np.loadtxt('out_1.txt', delimiter=",")  # load 时也要指定逗号分隔
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    histogram() 数据的评率分布的图形表示

    a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
    hist, bins = np.histogram(a, bins=[0, 20, 40, 60, 80, 100])

    tensor 与 ndarray 转换 (底层内存共享)

    a = torch.arange(12, dtype=torch.float32).reshape(3,4) # tensor
    b = a.detach().numpy() # tensor 转换为 ndarray
    c = torch.from_numpy(b) # ndarray 转换为 tensor
    a[0, 1] = 100
    print(a, a.dtype)
    print(b, b.dtype)
    print(c, c.dtype)

    a = np.ones((2,2), dtype=np.float32) # ndarray
    b = torch.Tensor(a) # numpy 转换为 Tensor
    c = b.numpy() # tensor 转换为 numpy
    d = torch.tensor(a) # numpy 转换为 tensor 不共享内存
    a[1, 0]=100
    print(a, a.dtype)
    print(b, b.dtype)
    print(c, c.dtype)
    print(d, d.dtype)

    a = np.ones([2,3]) # float64
    b = torch.Tensor(a) # ndarray 转换 torch torch.float32 (数据类型不同,此处进行拷贝,不共享内存)
    c = torch.from_numpy(a) # 注意c的类型 torch.float64 共享内存
    d = torch.tensor(a) # 无论什么类型,tensor()都会进行数据拷贝,不会共享内存
    a[0,1] = 100
    print(a, a.dtype)
    print(b, b.dtype)
    print(c, c.dtype)
    print(d, d.dtype)

    from IPython.core.interactiveshell import InteractiveShell
    InteractiveShell.ast_node_interactivity = “all”

    a.dtype
    b.dtype
    c.dtype
    d.dtype

  • 相关阅读:
    IDEA安装与配置
    【Python 零基础入门】常用内置函数 再探
    【高性能】Web性能压力测试JMeter、测试秒杀Red
    大家一起来学习如何使用spring.factories
    日常Git使用自我记录
    企业中 Docker 的 Swarm 使用及作用详解
    Android之 SVG绘制
    国庆作业4
    基于Python个人博客系统设计与实现 开题报告
    基于SpringBoot的“幼儿园管理系统”的设计与实现(源码+数据库+文档+PPT)
  • 原文地址:https://blog.csdn.net/weixin_55000908/article/details/126067423