• NumPy速查笔记(持续更新中)


    1 总览

    NumPy是基于Python的科学计算包,主要用来进行科学计算。

    2 ndarray

    ndarray全名叫做n dimension array,习惯称为多维数组。ndarray既可以表示标量,还可以表示向量、矩阵,甚至是张量。

    ndarray有如下属性:

    • dtype 数据类型
    • shape 多维数组的尺寸
    • ndim 维度,等同轴的个数
    • size 元素的个数
    • itemsize
    • nbytes
    • base
    • flags
    • strides
    • data
    • T 转置
    • real
    • imag
    • flat
    • ctypes

    ndarray的方法其实已经可以对当前ndarray进行处理和运算的,但也可以把当前ndarray当做参数传递给NumPy顶层的函数np.<function_name>

    举例:ndarray.dot(x)np.dot(ndarray, x)是一样的

    3 常用API

    3.1 创建ndarray

    创建ndarray大致可分为五种方法。

    (1)将Python类似数组的对象转化成Numpy数组

    • array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
    • asarray(a, dtype=None, order=None)
    • asanyarray(a, dtype=None, order=None)
    • ascontiguousarray(a, dtype=None)
    • asmatrix(data, dtype=None)
    • copy(a, order='K')

    (2)numpy内置的数组创建

    注意:

    (1) prototype表示array-like,就是类似数组的对象

    (2) 部分对角矩阵涉及对角线的索引。0表示原始主对角线,1表示往右上平移一个单位,-1表示往左下平移一个单位

    • empty(shape, dtype=float, order='C') 随机矩阵
    • empty_like(prototype, dtype=None, order='K', subok=True, shape=None)
    • eye(N, M=None, k=0, dtype=<class 'float'>, order='C') 伪单位矩阵
    • identity(n, dtype=None) 真实的单位矩阵
    • ones(shape, dtype=None, order='C')
    • ones_like(a, dtype=None, order='K', subok=True, shape=None)
    • zeros(shape, dtype=float, order='C')
    • zeros_like(a, dtype=None, order='K', subok=True, shape=None)
    • full(shape, fill_value, dtype=None, order='C') 单一值填充矩阵
    • full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)
    • arange([start, ]stop, [step, ]dtype=None) 相当于python的range
    • linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) 在区间内生成若干个等距离的数据点
    • logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0) $base^{start}$$base^{stop}$
    • geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0)
    • meshgrid(*xi, **kwargs)
    • mgrid()
    • ogrid()
    • indices(dimensions, dtype=<class 'int'>, sparse=False)
    • diag(v, k=0) 如果v是一维的,构建一个对角阵,v为第k个主对角线上的元素;如果v是二维的,抽取出主对角线的元素并返回
    • diagflat(v, k=0) 将输入铺平成一维数组,然后以此为对角线元素构建矩阵
    • tri(N, M=None, k=0, dtype=<class float>)== 创建N*M的下三角矩阵,第k个对角线及其下方都为1
    • tril(m, k=0) 把多维数组m变成下三角矩阵,第k对角线及其以上用0填充
    • triu(m, k=0) 把多维数组m变成上三角矩阵,第k对角线及其以下用0填充
    • vander(x, N=None, increasing=False) 创建范德蒙德矩阵
    • mat(data, dtype=None) 把data解析成matrix,若输入已经是matrix,将不会再次复制而是直接使用
    • bmat(obj, ldict=None, gdict=None) 从字符串、嵌套序列或者数组中创建矩阵
    >>> np.empty([3, 3])
    array([[1.69118108e-306, 2.04722549e-306, 6.23054972e-307],
           [7.56605239e-307, 1.33511562e-306, 8.01097889e-307],
           [1.24610723e-306, 8.34444713e-308, 2.29179042e-312]])
    >>> np.empty_like([1, 2, 3])
    array([0, 0, 0])
    >>> np.identity(4)
    array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]])
    >>> np.eye(3, 4, k=1)
    array([[0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]])
    >>> np.arange(12)
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    >>> np.linspace(1, 5, 20)
    array([1.        , 1.21052632, 1.42105263, 1.63157895, 1.84210526,
           2.05263158, 2.26315789, 2.47368421, 2.68421053, 2.89473684,
           3.10526316, 3.31578947, 3.52631579, 3.73684211, 3.94736842,
           4.15789474, 4.36842105, 4.57894737, 4.78947368, 5.        ])
    >>> np.logspace(1, 5, 3)
    array([1.e+01, 1.e+03, 1.e+05])
    >>> np.tri(3, 3, -1)
    array([[0., 0., 0.],
           [1., 0., 0.],
           [1., 1., 0.]])
    >>> np.triu([[1, 2, 3], [2, 3, 4], [4, 5, 6], [5, 6, 7]])
    array([[1, 2, 3],
           [0, 3, 4],
           [0, 0, 6],
           [0, 0, 0]])
    

    难点: meshgrid()

    meshgrid的作用,根据两个向量,在空间中画网格,返回两个矩阵,分别是网格上每个点的横坐标和纵坐标。

    >>> np.meshgrid([1, 2, 3], [2, 3, 4])
    [array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]]), array([[2, 2, 2],
           [3, 3, 3],
           [4, 4, 4]])]
    

    (3)从磁盘中读取标准格式或者自定义格式的多维数组

    • fromfile(file, dtype=float, count=-1, sep='', offset=0)
    • fromfunction(function, shape, **kwargs)
    • fromiter(iterable, dtype, count=-1)
    • loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)

    (4)通过使用字符串或缓冲区从原始字节创建数组

    • fromstring(string, dtype=float, count=-1, sep='')
    • frombuffer(buffer, dtype=float, count=-1, offset=0)

    (5)使用特殊的库函数

    通过随机函数来创建,对数据进行初始化的时候很常用。

    • random.rand(d0, d1, ..., dn) 依次传入每个维度的大小,生成[0, 1)的均匀分布(uniform distribution)
    • random.randn(d0, d1, ..., dn) 依次传入每个维度的大小,生成[0, 1)的标准正态分布(standard normal distribution)
    • random.randint(low, high=None, size=None, dtype='l') 整数的随机分布,大小为[low, high)
    • random.choice(a, size=None, replace=True, p=None) 在a中随便选size个元素(可以重复)
    • random.bytes(length) 生成随机的字节

    random.rand_integer()已经弃用

    3.2 索引和切片

    numpy有三种索引方式:基本索引、高级索引和字段索引。

    (1)基本索引

    start:stop:step,这个和python一样

    ...索引

    newaxis扩展维度,严格来说,这个不是用来索引数据的,而是用来扩展维度的,类似的操作是np.expand_dim()

    >>> np.array([1, 2, 3, 4])[:, np.newaxis]
    array([[1],
           [2],
           [3],
           [4]])
    

    :可以实现按顺序进行多个索引,这个和python一样

    :相比,用数组可以自定义顺序进行多个索引

    >>> np.array([0, 1, 2, 3, 4])[[1, 2, 3]]
    array([1, 2, 3])
    

    (2)高级索引

    >>> np.arange(12).reshape(4, 3)
    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 9, 10, 11]])
    

    多轴索引

    逗号隔开的索引,依次作用在多个轴上

    >>> np.arange(12).reshape(4, 3)[2:, 1:]
    array([[ 7,  8],
           [10, 11]])
    

    复合索引

    每个轴上可以是不同形式的基本索引

    >>> np.arange(12).reshape(4, 3)[[1, 2, 3], 1:]
    array([[ 4,  5],
           [ 7,  8],
           [10, 11]])
    

    布尔索引

    用一个包含布尔值的布尔数组来达到索引的目的,但要注意,长度是有限制的,必须和第一个维度的长度相等。

    >>> np.array([0, 1, 2, 3, 4])[[True, False, False, False, True]]
    array([0, 4])
    
    >>> a = np.array([0, 1, 2, 3, 4])
    >>> a > 2
    array([False, False, False,  True,  True])
    >>> a[a>2]
    array([3, 4])
    

    (4)字段索引

    暂时不是很清楚。

    >>> x = np.zeros((2,2), dtype=[('a', np.int32), ('b', np.float64, (3,3))])
    >>> x
    array([[(0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]),
            (0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])],
           [(0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]),
            (0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])]],
          dtype=[('a', '<i4'), ('b', '<f8', (3, 3))])
    >>> x['a']
    array([[0, 0],
           [0, 0]])
    >>> x['b']
    array([[[[0., 0., 0.],
             [0., 0., 0.],
             [0., 0., 0.]],
    
            [[0., 0., 0.],
             [0., 0., 0.],
             [0., 0., 0.]]],
    
    
           [[[0., 0., 0.],
             [0., 0., 0.],
             [0., 0., 0.]],
    
            [[0., 0., 0.],
             [0., 0., 0.],
             [0., 0., 0.]]]])
    >>> x['b'].shape
    (2, 2, 3, 3)
    

    3.3 改变多维数组的形状

    (1)通过指定新维度来直接改变形状

    • copyto(dst, src, casting='same_kind', where=True) 把一个数组复制到另一个数组上去,可能会有传播
    • shape(a) 等价于a.shape
    • reshape(a, newshape, order='C') 更改形状
    • ravel(a, order='C') 返回一个连续的拉伸的数组,等价于a.reshape(-1)
    • ndarray.flat 这是ndarray的一个属性,同样可以实现拉伸,返回的不是数组,而是一个numpy.flatiter对象,可以用[]进行索引
    • ndarray.flatten(order='C') 返回一个被拉伸之后的数组
    >>> a
    array([[1, 1, 1],
           [1, 1, 1]])
    >>> b = np.array([3, 3, 3])
    >>> np.copyto(a, b)
    >>> a
    array([[3, 3, 3],
           [3, 3, 3]])
    
    >>> np.ravel([[1, 2, 3], [4, 5, 6]])
    array([1, 2, 3, 4, 5, 6])
    
    >>> a = np.array([[1, 2, 3], [4, 5, 6]])
    >>> a
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> a.reshape(-1)
    array([1, 2, 3, 4, 5, 6])
    >>> np.reshape(a, -1)
    array([1, 2, 3, 4, 5, 6])
    >>> np.reshape(a, [3, 2])
    array([[1, 2],
           [3, 4],
           [5, 6]])
    
    >>> a
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> a.flat
    <numpy.flatiter object at 0x0000022EC2B7F120>
    >>> a.flat[0]
    1
    
    >>> a
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> a.flatten()
    array([1, 2, 3, 4, 5, 6])
    

    (2)通过转置来改变形状

    • ndarray.T 这个已经提过,但是仅限于矩阵使用
    • transpose(a, axes=None) 高维张量的转置
    • moveaxis(a, source, destination) 把source维移到destination维上,其余都不变,size为[3, 4, 5]把0轴移到2轴就得到[4, 5, 3]
    • rollaxis(a, axis, start=0) 把axis轴移到start轴上,其余不变,[3, 4, 5]如果把2轴移到0轴上,就是[5, 3, 4] ??
    • swapaxies(a, axis1, axis2) 在内部交换多维数组a的两个轴
    >>> a.shape
    (1, 2, 3)
    >>> np.transpose(a, [0, 2, 1]).shape
    (1, 3, 2)
    

    (3)通过直接改变维度(如压缩)来改变形状

    • squeeze(a, axis=None) 压缩掉一个维度,那个维度的长度必须为1
    • expand_dim(a, axis) 扩充一个维度,和np.newaxis类似
    >>> a.shape
    (2, 1, 3)
    >>> np.squeeze(a, axis=1).shape
    (2, 3)
    >>> np.expand_dims(a, axis=0).shape
    (1, 2, 1, 3)
    

    (2)拼接与合并

    • np.r_ 沿着第一个轴连接
    • np.c_ 沿着第二个轴把多维数组进行拼接,注意这不是括号,因为不是函数或者方法
    >>> np.r_[[1, 2, 3], 5, 6, np.array([7, 8])]
    array([1, 2, 3, 5, 6, 7, 8])
    >>> np.r_[[[1, 2], [2, 3]], [[3, 4], [5, 6]]]
    array([[1, 2],
           [2, 3],
           [3, 4],
           [5, 6]])
    >>> np.c_[[1, 2, 3], [2, 3, 4]]
    array([[1, 2],
           [2, 3],
           [3, 4]])
    

    3.4 线性代数计算

    一般都去用torch或者tensorflow了,所以这里一般没用到。

    3.5 permutation

    • random.shuffle(x) 原地随机打散元素
    • random.permutation(x) 返回一个打散的多维数组,如果是多维的,只作用于第一个轴

    3.6 np.frompyfunc()

    如何通过一个函数,来对ndarray的所有数据进行处理。比如:

    mapping_dict = {"china": 1, "usa": 2}
    arr =  np.array(["china", "usa"]) 
    func = np.frompyfunc(lambda x: mapping_dict[x], 1, 1)
    func(arr)
    
    '''
    output:
    array([1, 2], dtype=object)
    '''
    

    3.7 和其他工具包的转换

    ndarray和python互转

    ndarray.tolist()
    
    np.array(list)
    

  • 相关阅读:
    log4j日志输出级别(转)
    spring-framework——hao123
    gradle配置国内镜像
    项目启动控制台严重: Error listenerStart问题定位
    Pycharm按键失灵
    Mixed Content混合内容错误 Iframe Http页面无法访问
    RSA加密公钥系数获取结果多00
    xml报文标签替换正则表达式
    AES采用CBC模式128bit加密工具类
    js进行MD5加密(含中文),与后台JAVA加密之后结果不同(解决)
  • 原文地址:https://www.cnblogs.com/YoungF/p/12060875.html
Copyright © 2020-2023  润新知