zoukankan      html  css  js  c++  java
  • numpy的基本API(二)——维数操作

    numpy的基本维数操作API

      iwehdio的博客园:https://www.cnblogs.com/iwehdio/

     

    1、np.copyto(dst, src)  copyto方法将数组src复制到dst中。如果两个数组的形状完全相同,则复制后两数组中的数据相同。如果src的维数n比dst的维数低,且与dst中的最后几个维度shape[:-n]相同,就将dst中每个形状与src相同的都复制为src。

    >>> a = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])  # (2,2,3)
    >>> b = np.array([[7,8,9],[10,11,12]]) # (2,3)
    >>> c = np.array([10])                      #(1,)
    >>> np.copyto(a,b)
    >>> a
    array([[[ 7,  8,  9],
            [10, 11, 12]],
           [[ 7,  8,  9],
            [10, 11, 12]]])
    >>> np.copyto(a,c)
    >>> a
    array([[[10, 10, 10],
            [10, 10, 10]],
           [[10, 10, 10],
            [10, 10, 10]]])

     

    2、np.reshape(a, shape)  reshape方法将数组a在不改变数据的情况下改变形状。a表示目标数组,shape表示所要改变为的形状,可以为元组如(2,3),也可为列表如[2,3]。shape中某一维度的值为-1表示此维度的数据长度为自适应。但是shape改变的新数组大小必须与原数组相同。返回值为numpy数组。同时,可以使用a.reshape(shape),此时shape除了可为元组和列表外,也可以之间为用逗号间隔的数字2,3。

    >>> a = np.arange(6)
    >>> a
    array([0, 1, 2, 3, 4, 5])
    >>> np.reshape(a,(2, 3))

    array([[0, 1, 2],
           [3, 4, 5]])
    >>> np.reshape(a,(3,-1))
    array([[0, 1],
           [2, 3],
           [4, 5]])
    >>> a.reshape(6,1)
    array([[0],
           [1],
           [2],
           [3],
           [4],
           [5]])

     

    3、np.ravel(a)  raevl方法将数组a展平为一维数组,shape为(n, ),a为输入数组。同样的,也可以使用a.ravel(),注意不要丢掉括号。

    >>> np.ravel(a)
    array([0, 1, 2, 3, 4, 5])
    >>> np.array([[4,5,6],[1,2,3]]).ravel()
    array([4, 5, 6, 1, 2, 3])

     

    4、a.flat[n]  flat方法将数组a按一维数组索引,如数据a的shape为(M, N),则索引n可取[0, M*N-1]。也可以取一个列表作为索引如a.flat[[q, w]],返回索引为q和w的数组。同时,还有a.flat=x可以把数组a中的所有值改为x,也可a.flat[[q,w]=x]对列表中的特定之进行改变。

    >>> a = np.arange(1, 7).reshape(2, 3)
    >>> a
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> a.flat[5]
    6
    >>> a.flat[[2,4]]
    array([3, 5])
    >>> a.flat = 3
    >>> a
    array([[3, 3, 3],
           [3, 3, 3]])
    >>> a.flat[[1,5]]=5
    >>> a
    array([[1, 5, 3],
           [4, 5, 5]])

     

    5、np.moveaxis(a, src, dst)  moveaxis方法将数组的轴进行移动。src表示所要移动的轴的索引,dst表示所要移动到的位置,都可以为列表对象进行对应移动。

    需要注意的是,移动轴的含义是,把原来src索引位置的轴改为dst索引位置,而不是进行索引位置的交换。如shape为(2,3,4)的数组a经过b=np.moveaxis(a, 0, -1)后,b的shape为(3,4,2),也就是说其他轴按原来的顺序向前填充了,等价于np.moveaxis(x, [0, 1, 2], [-1, -3, -2])。此外,在索引更改后的数组与原数组对应的位置进行切片,切片的shape也有可能会与原数组不同而为其转置的shape。

    >>> x = np.arange(0, 24).reshape(2, 3, 4)
    >>> x
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11]],
    
           [[12, 13, 14, 15],
            [16, 17, 18, 19],
            [20, 21, 22, 23]]])
    >>> y=np.moveaxis(x, 0, -1)
    >>> y
    array([[[ 0, 12],
            [ 1, 13],
            [ 2, 14],
            [ 3, 15]],
    
           [[ 4, 16],
            [ 5, 17],
            [ 6, 18],
            [ 7, 19]],
    
           [[ 8, 20],
            [ 9, 21],
            [10, 22],
            [11, 23]]])
    >>> y.shape
    (3, 4, 2)
    >>> np.moveaxis(x, [0, 1, 2], [-1, -3, -2]).shape
    (3, 4, 2)
    >>> y[0,:,:] array([[ 0, 12], [ 1, 13], [ 2, 14], [ 3, 15]]) >>> x[:,0,:] array([[ 0, 1, 2, 3], [12, 13, 14, 15]])

    可以看到,x和y都在shape为3的轴求该轴索引为0时的切片,得到的数组互为转置。但是对所进行moveaxis操作的shape为2的轴,按其轴索引进行切片,得到的数组与原数组相同。

    moveaxis方法当只对一个轴进行操作时,也可由rollaxis方法实现,但是更推荐前者。

     

    6、np.swapaxes(a, axis1, axis2)  swapaxes方法对数组a中的两个轴的位置进行互换,其他轴的位置不变。

    >>> x = np.arange(0, 24).reshape(2, 3, 4)
    >>> x.shape
    (2, 3, 4)
    >>> y = np.swapaxes(x,0,1)
    >>> y
    array([[[ 0,  1,  2,  3],
            [12, 13, 14, 15]],
    
           [[ 4,  5,  6,  7],
            [16, 17, 18, 19]],
    
           [[ 8,  9, 10, 11],
            [20, 21, 22, 23]]])
    >>> y.shape
    (3, 2, 4)

     

    7、np.transpose(a, axex=None)  transpose方法排列数组a的形状。当axex为None时,返回的是数组a的转置。当axex为数组a轴的索引值的排列,如shape为[1,2,3,4]的数组a的axex为元组(2,1,3,0)或列表[2,1,3,0]时,表示数组a的shape变为了(第2轴,第1轴,第3轴,第0轴)的长度。

    >>> a = np.ones((1, 2, 3, 4))
    >>> np.transpose(a).shape
    (4, 3, 2, 1)
    >>> np.transpose(a,(2,1,3,0)).shape
    (3, 2, 4, 1)
    >>> a.T.shape
    (4, 3, 2, 1)

    当axex为None时,np.transpose(a)与a.T是等价的。

     

    8、np.atleast_1d(array1,array2,...)  atleast_1d方法将输入视为至少一维的数组,将输入数组放置到一个列表里。与直接创建list不同的是,把输入的每个对象都转换为了数组对象。atleast_2d方法将输入视为至少二维的数组,atleast_3d方法将输入视为至少三维的数组。其实就是把输入数组中维度高于一维/二维/三维的数组原样输出,低于一维/二维/三维的数组升至一维/二维/三维输出,把结果放入一个列表中。

    >>> x = np.arange(0, 24).reshape(2, 3, 4)
    >>> np.atleast_1d(x,[2,3],1)
    [array([[[ 0,  1,  2,  3],
             [ 4,  5,  6,  7],
             [ 8,  9, 10, 11]],
     
            [[12, 13, 14, 15],
             [16, 17, 18, 19],
             [20, 21, 22, 23]]]),
            array([2, 3]), array([1])]
    >>> [x,[2,3],1]
    [array([[[ 0,  1,  2,  3],
             [ 4,  5,  6,  7],
             [ 8,  9, 10, 11]],
     
            [[12, 13, 14, 15],
             [16, 17, 18, 19],
             [20, 21, 22, 23]]]), 
            [2, 3], 1]
    >>> np.atleast_2d(x,[2,3],1)
    [array([[[ 0,  1,  2,  3],
             [ 4,  5,  6,  7],
             [ 8,  9, 10, 11]],

            [[12, 13, 14, 15],
             [16, 17, 18, 19],
             [20, 21, 22, 23]]]), array([[2, 3]]), array([[1]])]
    >>> np.atleast_3d(x,[2,3],1)
    [array([[[ 0,  1,  2,  3],
             [ 4,  5,  6,  7],
             [ 8,  9, 10, 11]],

            [[12, 13, 14, 15],
             [16, 17, 18, 19],
             [20, 21, 22, 23]]]),
         array([[[2],[3]]]), array([[[1]]])]

    atleast_1d中,[2, 3]被转换为shape为(2, );atleast_1d中,[2, 3]被转换为shape为(1, 2);atleast_1d中,[2, 3]被转换为shape为(1, 2, 1)。先把整个数组当作一个元素升一维,再把数组中的每个数字当作一个元素升一维。

     

    9、np.broadcast(a, b)  broadcast方法创建并返回一个数组a和b的广播对象y。y只能通过迭代器获取内容,并且有.shape、.index等方法,表示广播结果的形状和索引。广播是指,如果数组a和b的形状不相同则对不相同的轴的值进行排列组合。

    >>> a = np.array([[1],[2]])
    >>> b = np.array([[4, 5, 6,7]])
    >>> a.shape, b.shape
    ((2, 1), (1, 4))
    >>> y = np.broadcast(a, b)
    >>> y
    <numpy.broadcast at 0x213aa462050>
    >>> y.shape
    (2, 4)
    >>>  for (u,v) in y:
                 print(u, v, '	', b.index)
    1 4      1
    1 5      2
    1 6      3
    1 7      4
    2 4      5
    2 5      6
    2 6      7
    2 7      8

     

    10、np.broadcast_to(a, shape)  broadcast_to方法把数组a广播并返回为形状为shape的数组。shape的形式应为new_shape+a.shape,即如需要把a广播为(m, n)次,a.shape=(q, w),则shape应为(m,n,q,w)。

    >>> a = np.array([[1,2],[3,4]])
    >>> a.shape
    (2, 2)
    >>> b = np.broadcast_to(a, (3,3,2,2))
    array([[[[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]]],
           [[[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]]],
           [[[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]],
            [[1, 2],
             [3, 4]]]])
    >>> b.shape
    (6, 4, 2, 2)

     

     

    11、np.broadcast_array(a, b)  broadcast_array方法获取广播的数组,返回的是包含广播数组的列表。

    >>> a = np.array([[1,2,3]])
    >>> b = np.array([[4],[5]])
    >>> np.broadcast_arrays(a, b)
    [array([[1, 2, 3],
            [1, 2, 3]]),
     array([[4, 4, 4],
            [5, 5, 5]])]    

     

    12、np.expand_dims(a, axis)  expand_dims方法返回扩展输入数组a的形状的数组,在axis轴位置添加新轴。

    >>> x = np.array([[1,2],[3,4]])
    >>> x.shape
    (2, 2)
    >>> y = np.expand_dims(x, 0)
    >>> y
    array([[[1, 2],
            [3, 4]]])
    >>> y.shape
    (1, 2, 2)
    >>> z = np.expand_dims(x, 1)
    >>> z
    array([[[1, 2]],
           [[3, 4]]])
    >>> z.shape
    (2, 1, 2)
    >>> w = np.expand_dims(x, 2)
    >>> w
    array([[[1],
            [2]],
           [[3],
            [4]]])
    >>> w.shape
    (2, 2, 1)

     

    13、在reshape等API中,都有order{'C', 'F', 'A'}参数,默认为'C'。其含义主要影响数组的索引速度,'C'表示使用类似C语言的索引顺序读取/写入元素,最后一个轴索引更改最快,回到第一个轴索引更改最快。 'F'表示使用类似Fortran语言的索引顺序读取/写入元素,第一个索引更改最快,最后一个索引更改最慢。 'C'和'F'选项不考虑基础数组的内存布局,仅指索引的顺序。 'A'表示如果数组在内存中是连续的,则以类似于Fortran语言的索引顺序读取/写入元素,否则为类似于C语言的顺序。

     

    参考:numpy中文文档:https://www.numpy.org.cn/reference/

       numpy英文文档:https://numpy.org/doc/1.17/reference/index.html

     

    iwehdio的博客园:https://www.cnblogs.com/iwehdio/

  • 相关阅读:
    基于MINA实现server端心跳检测(KeepAliveFilter)
    TCP的TIME_WAIT快速回收与重用
    HDU4183 起点到终点再到起点 除起点每点仅经过一次 网络流
    oracle union 注入工具
    sqlmap注入技巧收集
    sqlmap的一些小技巧
    FCKeditor漏洞利用
    WAF绕过的技巧
    杨辉三角
    Oracle 常用入侵命令
  • 原文地址:https://www.cnblogs.com/iwehdio/p/11949790.html
Copyright © 2011-2022 走看看