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]])
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方法是一种综合的方法,当指定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方法分割数组后,得到的结果仍是原数组的视图