numpy.split#

numpy.split(ary, indices_or_sections, axis=0)[源代码]#

将数组拆分为多个子数组,作为对 ary 的视图。

参数:
aryndarray

要拆分为子数组的数组。

indices_or_sectionsint 或 1-D 数组

如果 indices_or_sections 是一个整数 N,则数组将沿 axis 拆分为 N 个等长的数组。如果无法进行这种拆分,则会引发错误。

如果 indices_or_sections 是一个排序的整数 1-D 数组,则这些条目表示沿 axis 拆分数组的位置。例如,对于 axis=0[2, 3] 将产生:

  • ary[:2]

  • ary[2:3]

  • ary[3:]

如果某个索引超出数组沿 axis 的维度,则相应地返回一个空子数组。

axisint, 可选

沿其进行拆分的轴,默认为 0。

返回:
sub-arraysndarray 列表

一个子数组列表,作为对 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)]