numpy.split#
- numpy.split(ary, indices_or_sections, axis=0)[源代码]#
将一个数组分割成多个子数组,作为 ary 的视图.
- 参数:
- aryndarray
要被分成子数组的数组.
- indices_or_sections整数或1维数组
如果 indices_or_sections 是一个整数,N,数组将沿着 axis 被分割成 N 个等份数组.如果这样的分割不可能,则会引发一个错误.
如果 indices_or_sections 是一个排序后的 1-D 整数数组,这些条目表示沿 axis 分割数组的位置.例如,``[2, 3]`` 对于
axis=0,将导致ary[:2]
ary[2:3]
ary[3:]
如果索引超过了沿 axis 的数组维度,则相应地返回一个空子数组.
- axisint, 可选
要分割的轴,默认为 0.
- 返回:
- sub-arraysndarrays 列表
作为对 ary 的视图的子数组列表.
- 引发:
- ValueError
如果 indices_or_sections 被给定为一个整数,但分割结果不导致相等的划分.
参见
array_split将一个数组分割成多个大小相等或接近相等的子数组.如果无法进行相等分割,则不会引发异常.
hsplit将数组水平(按列)拆分为多个子数组.
vsplit将数组垂直(按行)拆分为多个子数组.
dsplit沿第3轴(深度)将数组拆分为多个子数组.
concatenate沿现有轴连接一系列数组.
stack沿新轴连接一系列数组.
hstack按顺序水平堆叠数组(按列).
vstack按顺序垂直(按行)堆叠数组.
dstack按深度顺序堆叠数组(沿第三维度).
示例
>>> import numpy as np >>> x = np.arange(9.0) >>> np.split(x, 3) [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]
>>> x = np.arange(8.0) >>> np.split(x, [3, 5, 6, 10]) [array([0., 1., 2.]), array([3., 4.]), array([5.]), array([6., 7.]), array([], dtype=float64)]