天天看點

python粘性拓展_Python Numpy 數組擴充 repeat和tile

numpy.repeat

numpy.repeat(a, repeats, axis=None)

Repeat elements of an array.

可以看出repeat函數是操作數組中的每一個元素,進行元素的複制。

例如:

>>> a = np.arange(3)

>>> a

array([0, 1, 2])

>>> np.repeat(a, 2)

array([0, 0, 1, 1, 2, 2])

>>> a = [[0,1], [2,3], [4,5]]

>>> y = np.repeat(a, 2)

>>> y

array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5])

numpy.tile

numpy.tile(A, reps)

Construct an array by repeating A the number of times given by reps.

可以看出tile函數是将數組A作為操作對象

例如:

>>> a = np.array([[1,2],[3,4]])

>>> a

array([[1, 2],

[3, 4]])

>>> np.tile(a, 2)

array([[1, 2, 1, 2],

[3, 4, 3, 4]])

>>> a = [[0,1], [2,3], [4,5]]

>>> x = np.tile(a, (2,1))

>>> x

array([[0, 1],

[2, 3],

[4, 5],

[0, 1],

[2, 3],

[4, 5]])