np.repeat(a, repeats, axis=None)
作用:
复制数组的元素。
Parameters
a : array_like
Input array.
repeats : int or array of ints
每个元素复制多少次.
axis : 默认会先flattened输入数组,返回flat的输出数组。
Returns
repeated_array : ndarray
Output array which has the same shape as a
, except along
the given axis.
See Also
tile : Tile an array.
例子:
>>> np.repeat(3, 4) #默认扁平化
array([3, 3, 3, 3])
>>> x = np.array([[1,2],[3,4]])
>>> np.repeat(x, 2)#没有axis参数输出被扁平化
array([1, 1, 2, 2, 3, 3, 4, 4])
>>>np.repeat(x, 2,axis=1)#1轴上进行复制,每个复制两次
array([[1, 1, 2, 2],
[3, 3, 4, 4]])
>>> np.repeat(x, 2,axis=0)
array([[1, 2],
[1, 2],
[3, 4],
[3, 4]])
np.repeat(x, [1, 2], axis=0)#沿着0轴第一个元素复制一次,第二个元素复制两次
array([[1, 2],
[3, 4],
[3, 4]])