numpy(八)——数组分割

博客主要介绍了numpy中数组分割的方法。包括用vsplit/hsplit方法分割,np.vsplit可垂直分割数组,hsplit则水平分割,分割结果是原数组视图;还介绍了综合方法split,指定axis=1等同于hsplit,axis=0等同于vsplit,分割结果同样是原数组视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy(八)——数组分割

import numpy as np
arr = np.arange(42).reshape(6,7)
arr
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, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39, 40, 41]])
  • 用vsplit/hsplit方法分割

np.vsplit

Signature: np.vsplit(ary, indices_or_sections)

Docstring:

Split an array into multiple sub-arrays vertically (row-wise).

Please refer to the split documentation. vsplit is equivalent
to split with axis=0 (default), the array is always split along the
first axis regardless of the array dimension.

可见,np.vsplit要求两个参数,除了需要分割的数组本身以外,还需要告诉函数数组应当分割的位置:

  • 当传入整数时,相当于告诉函数需要将数组等分为几部分
arr_v1 = np.vsplit(arr,2)
arr_v1    #返回一个列表
[array([[ 0,  1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12, 13],
        [14, 15, 16, 17, 18, 19, 20]]), array([[21, 22, 23, 24, 25, 26, 27],
        [28, 29, 30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39, 40, 41]])]

当传入的数字不能将数组的垂直方向上的"行数"均分时,则会产生报错

arr_v2 = np.vsplit(arr,4)
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

~\AppData\Local\conda\conda\envs\DataScience\lib\site-packages\numpy\lib\shape_base.py in split(ary, indices_or_sections, axis)
    534     try:
--> 535         len(indices_or_sections)
    536     except TypeError:


TypeError: object of type 'int' has no len()


During handling of the above exception, another exception occurred:


ValueError                                Traceback (most recent call last)

<ipython-input-12-8887e29882f5> in <module>
----> 1 arr_v2 = np.vsplit(arr,4)


~\AppData\Local\conda\conda\envs\DataScience\lib\site-packages\numpy\lib\shape_base.py in vsplit(ary, indices_or_sections)
    654     if _nx.ndim(ary) < 2:
    655         raise ValueError('vsplit only works on arrays of 2 or more dimensions')
--> 656     return split(ary, indices_or_sections, 0)
    657 
    658 def dsplit(ary, indices_or_sections):


~\AppData\Local\conda\conda\envs\DataScience\lib\site-packages\numpy\lib\shape_base.py in split(ary, indices_or_sections, axis)
    539         if N % sections:
    540             raise ValueError(
--> 541                 'array split does not result in an equal division')
    542     res = array_split(ary, indices_or_sections, axis)
    543     return res


ValueError: array split does not result in an equal division
  • 当传入一个列表时,则将列表中的元素作为数组的"行向"下标进行分割
arr_v2 = np.vsplit(arr,[2,4]) #将arr数组按行分成三部分:[0,1],[2,3],[4,5]
arr_v2
[array([[ 0,  1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12, 13]]), array([[14, 15, 16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25, 26, 27]]), array([[28, 29, 30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39, 40, 41]])]
  • hsplit则是在水平方向上进行分割,用法与vsplit完全一致,不再演示
arr_v2[0][0,0] = 999
print(arr_v2[0])
print("--"*10)
print(arr)
[[999   1   2   3   4   5   6]
 [  7   8   9  10  11  12  13]]
--------------------
[[999   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  36  37  38  39  40  41]]
  • 可以发现,分割后得到的结果是原数组的视图

  • 用split方法进行分割

split方法是一种综合的方法,当指定axis=1时则等同于hsplit方法,axis=0时则等同于vsplit方法

arr_v3 = np.split(arr,[2,4])
arr_v3
[array([[999,   1,   2,   3,   4,   5,   6],
        [  7,   8,   9,  10,  11,  12,  13]]),
 array([[14, 15, 16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25, 26, 27]]),
 array([[28, 29, 30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39, 40, 41]])]
arr_h1 = np.split(arr,[2,4],axis=1)
arr_h1
[array([[999,   1],
        [  7,   8],
        [ 14,  15],
        [ 21,  22],
        [ 28,  29],
        [ 35,  36]]), array([[ 2,  3],
        [ 9, 10],
        [16, 17],
        [23, 24],
        [30, 31],
        [37, 38]]), array([[ 4,  5,  6],
        [11, 12, 13],
        [18, 19, 20],
        [25, 26, 27],
        [32, 33, 34],
        [39, 40, 41]])]
arr_h1[0][0,1] = 999
print(arr_h1[0])
print("--"*10)
print(arr)
[[999 999]
 [  7   8]
 [ 14  15]
 [ 21  22]
 [ 28  29]
 [ 35  36]]
--------------------
[[999 999   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  36  37  38  39  40  41]]
  • 同样,用split方法分割数组后,得到的结果仍是原数组的视图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值