numpy.tile(A, reps)Construct an array by repeating A the number of times given by reps.
tile是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来。
【例】将原矩阵横向、纵向地复制。
import numpy as npx = np.array([[1, 2], [3, 4]])print(x)# [[1 2]# [3 4]]y = np.tile(x, (1, 3))print(y)# [[1 2 1 2 1 2]# [3 4 3 4 3 4]]y = np.tile(x, (3, 1))print(y)# [[1 2]# [3 4]# [1 2]# [3 4]# [1 2]# [3 4]]y = np.tile(x, (3, 3))print(y)# [[1 2 1 2 1 2]# [3 4 3 4 3 4]# [1 2 1 2 1 2]# [3 4 3 4 3 4]# [1 2 1 2 1 2]# [3 4 3 4 3 4]]
numpy.repeat(a, repeats, axis=None)Repeat elements of an array.axis=0,沿着y轴复制,实际上增加了行数。axis=1,沿着x轴复制,实际上增加了列数。repeats,可以为一个数,也可以为一个矩阵。axis=None时就会flatten当前矩阵,实际上就是变成了一个行向量。
【例】重复数组的元素。
import numpy as npx = np.repeat(3, 4)print(x) # [3 3 3 3]x = np.array([[1, 2], [3, 4]])y = np.repeat(x, 2)print(y)# [1 1 2 2 3 3 4 4]y = np.repeat(x, 2, axis=0)print(y)# [[1 2]# [1 2]# [3 4]# [3 4]]y = np.repeat(x, 2, axis=1)print(y)# [[1 1 2 2]# [3 3 4 4]]y = np.repeat(x, [2, 3], axis=0)print(y)# [[1 2]# [1 2]# [3 4]# [3 4]# [3 4]]y = np.repeat(x, [2, 3], axis=1)print(y)# [[1 1 2 2 2]# [3 3 4 4 4]]
