numpy repeat 用法及代码示例

用法:
numpy.repeat(a, repeats, axis=None)
重复数组的元素。

描述
参数 a: : array_like

输入数组。
repeats: : int 或 array of ints

每个元素的重复次数。重复播放以适合给定轴的形状。
axis: : int, 可选参数

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

返回值: repeated_array: : ndarray

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

例子:

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

发表回复

登录... 后才能评论