numpy.repeat#

numpy.repeat(a, repeats, axis=None)[源代码]#

将数组的每个元素重复到它们自身之后

参数:
a类数组对象

输入数组。

repeats整数或整数数组

每个元素的重复次数。 repeats 会被广播以适应给定轴的形状。

axisint, optional

值的重复所在的轴。默认情况下,使用展平的输入数组,并返回一个展平的输出数组。

返回:
repeated_arrayndarray

输出数组,其形状与 a 相同,除了在给定的轴上。

另请参阅

tile

平铺一个数组。

unique

查找数组的唯一元素。

示例

>>> import numpy as np
>>> np.repeat(3, 4)
array([3, 3, 3, 3])
>>> x = np.array([[1,2],[3,4]])
>>> np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])
>>> np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]])
>>> np.repeat(x, [1, 2], axis=0)
array([[1, 2],
       [3, 4],
       [3, 4]])