numpy——hsplit()、vsplit()函数的详细使用

@toc


看了官网的文档,有看了好多网上的相关教程,总是感觉介绍的云里雾里的,没有说明白,自己在这里记录一下吧!!!


一、hsplit()函数的使用

hsplit() 对数组按列进行分割

1、官网教程

def hsplit(ary, indices_or_sections):
    """
    Split an array into multiple sub-arrays horizontally (column-wise).

    Please refer to the `split` documentation.  `hsplit` is equivalent
    to `split` with ``axis=1``, the array is always split along the second
    axis regardless of the array dimension.

    See Also
    --------
    split : Split an array into multiple sub-arrays of equal size.

    Examples
    --------
    >>> x = np.arange(16.0).reshape(4, 4)
    >>> x
    array([[  0.,   1.,   2.,   3.],
           [  4.,   5.,   6.,   7.],
           [  8.,   9.,  10.,  11.],
           [ 12.,  13.,  14.,  15.]])
    >>> np.hsplit(x, 2)   
    [array([[  0.,   1.],
           [  4.,   5.],
           [  8.,   9.],
           [ 12.,  13.]]),
     array([[  2.,   3.],
           [  6.,   7.],
           [ 10.,  11.],
           [ 14.,  15.]])]
    >>> np.hsplit(x, np.array([3, 6]))  
                                    
    [array([[  0.,   1.,   2.],
           [  4.,   5.,   6.],
           [  8.,   9.,  10.],
           [ 12.,  13.,  14.]]),
     array([[  3.],
           [  7.],
           [ 11.],
           [ 15.]]),
     array([], dtype=float64)]

    With a higher dimensional array the split is still along the second axis.

    >>> x = np.arange(8.0).reshape(2, 2, 2)
    >>> x
    array([[[ 0.,  1.],
            [ 2.,  3.]],
           [[ 4.,  5.],
            [ 6.,  7.]]])
    >>> np.hsplit(x, 2)
    [array([[[ 0.,  1.]],
           [[ 4.,  5.]]]),
     array([[[ 2.,  3.]],
           [[ 6.,  7.]]])]

    """
    if _nx.ndim(ary) == 0:
        raise ValueError('hsplit only works on arrays of 1 or more dimensions')
    if ary.ndim > 1:
        return split(ary, indices_or_sections, 1)
    else:
        return split(ary, indices_or_sections, 0)

哈哈,是不是有些灰色难懂

2、个人版本解释

>>> x = np.arange(16.0).reshape(2, 8)
>>> x
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
       [ 8.,  9., 10., 11., 12., 13., 14., 15.]])
>>> a = np.hsplit(x, [2,4,6])   # 返回的是按列分割之后的列表    [2,4,6]分别代表分按列分割的索引位置
>>> type(a)
<class 'list'>
>>> len(a)
4
>>> a
[array([[0., 1.],
       [8., 9.]]), array([[ 2.,  3.],
       [10., 11.]]), array([[ 4.,  5.],
       [12., 13.]]), array([[ 6.,  7.],
       [14., 15.]])]
>>> a[0]      # 列表中的每一个元素是一个数组
array([[0., 1.],
       [8., 9.]])
>>> a[1]
array([[ 2.,  3.],
       [10., 11.]])
>>> a[2]
array([[ 4.,  5.],
       [12., 13.]])
>>> a[3]
array([[ 6.,  7.],
       [14., 15.]])

从上面的例子可以总结出以下几点:

  • 1、a = np.hsplit(x, [2,4,6]) 返回值的数据类型是一个列表
  • 2、列表中的每一个元素是划分开的数组
  • 3、[2,4,6] 分别代表按列划分的索引位置,你可以想象是用刀在这个位置切了一刀

二、vsplit()函数的使用

vsplit() 对数组按行进行分割 用法同上,我就不举例啦,LZ要下班啦,996.ICU 狗带吧!!

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值