numpy.split与numpy.array_split

本文参考官方文档进行总结得出:https://numpy.org/devdocs/reference/generated/numpy.array_split.html#numpy.array_split

numpy.split

numpy.split(aryindices_or_sectionsaxis=0)

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

参数:

ary : ndarray

        数组可分为子数组

indices_or_sections : int or 1-D array      

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

        如果indexs_or_sections是一维排序的整数数组,则该条目指示数组沿axis的拆分位置。 

axis:int, optional

        沿axis分割,默认值为0

返回:

sub-arrays:list of ndarrays

        作为ary视图的子数组列表

示例:

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

numpy.array_split

numpy.array_split(aryindices_or_sectionsaxis=0)

将一个数组拆分为多个子数组

请参阅split介绍。 这些函数之间的唯一区别是array_split允许indices_or_sections是一个不等分轴(axis)的整数。 对于长度为l的数组(应该拆分成n个部分),它返回大小为l/n+1(先算1/n ,再加1)的l%n个子数组,其余的大小为l/n。

示例:

>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
    [array([0.,  1.,  2.]), array([3.,  4.,  5.]), array([6.,  7.])]
>>> x = np.arange(7.0)
>>> np.array_split(x, 3)
    [array([0.,  1.,  2.]), array([3.,  4.]), array([5.,  6.])]
原创文章 46 获赞 49 访问量 2173

猜你喜欢

转载自blog.csdn.net/qq_41660119/article/details/105873990
今日推荐