@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 狗带吧!!
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠