当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy hsplit用法及代码示例


本文简要介绍 python 语言中 numpy.hsplit 的用法。

用法:

numpy.hsplit(ary, indices_or_sections)

将一个数组水平拆分为多个子数组(按列)。

请参阅 split 文档。 hsplit 相当于 split axis=1 ,无论数组维度如何,数组始终沿第二轴分割。

例子

>>> 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([], shape=(4, 0), dtype=float64)]

对于更高维的数组,拆分仍然沿第二个轴。

>>> 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.]]])]

相关用法


注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.hsplit。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。