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, optional

分割的轴,默认为0。

返回:
sub-arrayslist of ndarrays

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