• Python numpy 入门系列 15 数组操作(分割数组)


    分割数组

    函数数组及操作
    split 将一个数组分割为多个子数组
    hsplit 将一个数组水平分割为多个子数组(按列)
    vsplit 将一个数组垂直分割为多个子数组(按行)

    numpy.split

    numpy.split 函数沿特定的轴将数组分割为子数组,格式如下:

    numpy.split(ary, indices_or_sections, axis)

    参数说明:

    • ary:被分割的数组
    • indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
    • axis:设置沿着哪个方向进行切分,默认为 0,横向切分,即水平方向。为 1 时,纵向切分,即竖直方向。

    实例

    import numpy as np
     
    a = np.arange(9)
     
    print ('第一个数组:')
    print (a)
    print ('\n')
     
    print ('将数组分为三个大小相等的子数组:')
    b = np.split(a,3)
    print (b)
    print ('\n')
     
    print ('将数组在一维数组中表明的位置分割:')
    b = np.split(a,[4,7])
    print (b)

     输出结果为:

    第一个数组:
    [0 1 2 3 4 5 6 7 8]
    
    
    将数组分为三个大小相等的子数组:
    [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
    
    
    将数组在一维数组中表明的位置分割:
    [array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]

    axis 为 0 时在水平方向分割,axis 为 1 时在垂直方向分割:

    实例

    import numpy as np
    
    a = np.arange(16).reshape(4, 4)
    print('第一个数组:')
    print(a)
    print('\n')
    print('默认分割(0轴):')
    b = np.split(a,2)
    print(b)
    print('\n')
    
    print('沿水平方向分割:')
    c = np.split(a,2,1)
    print(c)
    print('\n')
    
    print('沿水平方向分割:')
    d= np.hsplit(a,2)
    print(d)

     输出结果为:

    第一个数组:
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    
    
    默认分割(0轴):
    [array([[0, 1, 2, 3],
           [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
           [12, 13, 14, 15]])]
    
    
    沿水平方向分割:
    [array([[ 0,  1],
           [ 4,  5],
           [ 8,  9],
           [12, 13]]), array([[ 2,  3],
           [ 6,  7],
           [10, 11],
           [14, 15]])]
    
    
    沿水平方向分割:
    [array([[ 0,  1],
           [ 4,  5],
           [ 8,  9],
           [12, 13]]), array([[ 2,  3],
           [ 6,  7],
           [10, 11],
           [14, 15]])]

    numpy.hsplit

    numpy.hsplit 函数用于水平分割数组,通过指定要返回的相同形状的数组数量来拆分原数组。

    实例

    import numpy as np
     
    harr = np.floor(10 * np.random.random((2, 6)))
    print ('原array:')
    print(harr)
     
    print ('拆分后:')
    print(np.hsplit(harr, 3))

     输出结果为:

    array
    [[4. 7. 6. 3. 2. 6.]
     [6. 3. 6. 7. 9. 7.]]
    拆分后:
    [array([[4., 7.],
           [6., 3.]]), array([[6., 3.],
           [6., 7.]]), array([[2., 6.],
           [9., 7.]])]

    numpy.vsplit

    numpy.vsplit 沿着垂直轴分割,其分割方式与hsplit用法相同。

    实例

    import numpy as np
     
    a = np.arange(16).reshape(4,4)
     
    print ('第一个数组:')
    print (a)
    print ('\n')
     
    print ('竖直分割:')
    b = np.vsplit(a,2)
    print (b)

     输出结果为:

    第一个数组:
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    
    
    竖直分割:
    [array([[0, 1, 2, 3],
           [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
           [12, 13, 14, 15]])]

    REF

    https://www.cnblogs.com/mzct123/p/8659193.html  (numpy flatten ravel)
    https://blog.csdn.net/weixin_43960668/article/details/114979389 (numpy flatten ravel)

    https://www.runoob.com/numpy/numpy-array-manipulation.html

  • 相关阅读:
    二:dot语言语法及使用
    一:安装graphviz
    一个程序的前世今生(四)——延迟绑定和GOT与PLT
    一个程序的前世今生(三)——动态链接库和静态链接库
    一个程序的前世今生(二)——可执行文件如何加载进内存
    更新mysql驱动5.1-47 Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEY
    The superclass javax servlet http HttpServlet was not found on the Java Build Path
    vue-router地址栏URL全局参数拼接
    Canvas签字画图板
    Vue 表单拖拽排序
  • 原文地址:https://www.cnblogs.com/emanlee/p/16036769.html
Copyright © 2020-2023  润新知