Numpy中的np.split()函数详解

Numpy中np.split()函数详解


NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。np.split()函数的作用是将一个数组拆分为多个子数组,跟Tensorflow中的slice()函数有点类似,但是np.split()函数返回的是多个数组,tf.slice()函数返回的则是被切取的一个张量,区别还是挺大的。

1.官方注释

官方的注释如下:

 """
    Split an array into multiple sub-arrays.

    Parameters
    ----------
    ary : ndarray
        Array to be divided into sub-arrays.
    indices_or_sections : int or 1-D array
        If `indices_or_sections` is an integer, N, the array will be divided
        into N equal arrays along `axis`.  If such a split is not possible,
        an error is raised.

        If `indices_or_sections` is a 1-D array of sorted integers, the entries
        indicate where along `axis` the array is split.  For example,
        ``[2, 3]`` would, for ``axis=0``, result in

          - ary[:2]
          - ary[2:3]
          - ary[3:]

        If an index exceeds the dimension of the array along `axis`,
        an empty sub-array is returned correspondingly.
    axis : int, optional
        The axis along which to split, default is 0.

    Returns
    -------
    sub-arrays : list of ndarrays
        A list of sub-arrays.

    Raises
    ------
    ValueError
        If `indices_or_sections` is given as an integer, but
        a split does not result in equal division.

    See Also
    --------
    array_split : Split an array into multiple sub-arrays of equal or
                  near-equal size.  Does not raise an exception if
                  an equal division cannot be made.
    hsplit : Split array into multiple sub-arrays horizontally (column-wise).
    vsplit : Split array into multiple sub-arrays vertically (row wise).
    dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
    concatenate : Join a sequence of arrays along an existing axis.
    stack : Join a sequence of arrays along a new axis.
    hstack : Stack arrays in sequence horizontally (column wise).
    vstack : Stack arrays in sequence vertically (row wise).
    dstack : Stack arrays in sequence depth wise (along third dimension).

除了split函数外,还有array_split函数,hsplit函数(用于水平分割),vsplit函数(用于垂直分割)等等。spli函数只能用于均等分割,如果不能均等分割则会报错:array split does not result in an equal division。而array_split则全能一点,可以用于不均等分割。

2.参数解释

def split(ary, indices_or_sections, axis=0):
...
    return res
  • ary
    ary的类型为ndarray(n维数组),表示待分割的原始数组
  • indices_or_sections
    indices_or_sections的类型为int或者一维数组,表示一个索引,也就是切的位置所在。indices_or_sections的值如果是一个整数的话,就用这个数平均分割原数组。indices_or_sections的值如果是一个数组的话,就以数组中的数字为索引切开,这个不是太好理解,待会看例子应该就容易理解了。
  • axis
    axis的类型为int,表示的是沿哪个维度切。ary为2维数组时,axis默认为0表示横向切,为1时表示纵向切。
  • res
    返回值res的类型为ndarray(n维数组),表示返回的是一组字数组。

3.例子

  • 例1
x = np.arange(9.0)  # [0. 1. 2. 3. 4. 5. 6. 7. 8.]
np.split(x, 3) # [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]  

讲解split函数前先说说例子中的np.arange函数,这个函数返回的是一个有终点和起点,有固定步长的排列。x表示以0为起点总数为9的等差数列,np.split(x, 3)表示的是将x沿纵向平均分割为三个数列

  • 例2
A = np.arange(36).reshape((2, 2, 9)) 
#[[[ 0  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]]]
[A1, A2, A3] = np.split(A, [3, 6], axis=2)
# A1:[[[ 0  1  2]
#      [ 9 10 11]]
#     [[18 19 20]
#      [27 28 29]]]
#A2:[[[ 3  4  5]
#      [12 13 14]]
#     [[21 22 23]
#      [30 31 32]]]
#A3:[[[ 6  7  8]
#      [15 16 17]]
#     [[24 25 26]
#      [33 34 35]]]

np.arange(36)表示的是从0开始生成36个数字组成的等比数列,A即表示将np.arange(36)重新整型为(2, 2, 9)格式的数组。(2, 2, 9)从左到右分别对应axis=0,axis=1,axis=2。np.split(A, [3, 6], axis=2)表示的是将数组A沿shape中9那个维度方向分割为(:,3]、(3,6]、(6,:]3个数组

参考

[1] NumPy 教程
[2] numpy.split()函数
[3] NumPy中对数组进行切分及一些基本概念

码字不易,如果您觉得有帮助,麻烦帮我点个赞~~

  • 32
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值