1、numpy.repeat(a, repeats, axis=None)
在指定的维度上重复数组中的元素
参数:
a:输入序列
repeats:每个元素重复的次数
axis:要沿其重复值的轴。默认情况下,使用展开的输入数组,并返回展开的输出数组。
例1:输入序列3,repeat 4倍,axis使用默认值
np.repeat(3, 4)
array([3, 3, 3, 3])
例2:输入序列[[1,2],[3,4]],维度为2x2,因为axis使用默认值,因此将输入序列铺平,变为[1,2,3,4],维度为(4,),然后repeats为2,
x = np.array([[1,2],[3,4]])
np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])
例3:输入尺寸为2x2,在第二个维度上repeat
x = np.array([[1,2],[3,4]])
np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
例3:输入尺寸为2x2x2,在第二个维度上repeat
x = np.array([[[1,4],[2,5]],[[3,6],[4,7]]])
np.repeat(x, 3, axis=1)
array([[[1, 4],
[1, 4],
[1, 4],
[2, 5],
[2, 5],
[2, 5]],
[[3, 6],
[3, 6],
[3, 6],
[4, 7],
[4, 7],
[4, 7]]])
例4:输入尺寸为2x2,在第一个维度上repeat
x = np.array([[1,2],[3,4]])
np.repeat(x, [1, 2], axis=0)
array([[1, 2],
[3, 4],
[3, 4]])
2、numpy.tile(A, reps)
-
Construct an array by repeating A the number of times given by reps
将输入序列A重复一定的倍数 -
参数
若A 的维度小于reps的,A扩展新的维度,例如(3,) reshape 为(1, 3) ,或者 (1, 3) reshape为 (1, 1, 3);
If A.ndim > d, reps is promoted to A.ndim by pre-pending 1’s to it. Thus for an A of shape (2, 3, 4, 5), a reps of (2, 2) is treated as (1, 1, 2, 2).