numpy.repeat#
- numpy.repeat(a, repeats, axis=None)[源代码]#
在每个数组元素后重复元素自身
- 参数:
- aarray_like
输入数组.
- repeats整数或整数数组
每个元素的重复次数. repeats 会被广播来适应给定轴的形状.
- axis整数,可选
沿哪个轴重复值. 默认情况下,使用扁平化的输入数组,并返回一个扁平化的输出数组.
- 返回:
- repeated_arrayndarray
输出数组,该数组与
a具有相同的形状,除非沿着给定的轴.
示例
>>> 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]])