데이터 공부기록

[Numpy] np.tile : 가지고 있는 배열을 똑같이 찍어낸다. 본문

카테고리 없음

[Numpy] np.tile : 가지고 있는 배열을 똑같이 찍어낸다.

standingR 2023. 11. 22. 20:49

[요약]

np.tile : 가지고 있는 배열을 똑같이 찍어낸다.

기준은 axis (axis = 0 : row /  axis = 1 : columns / axis =2 : depth) 


numpy.tile

numpy.tile(A, reps)

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

 

If reps has length d, the result will have dimension of max(d, A.ndim).

 

If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.

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

 

Note :  Although tile may be used for broadcasting, it is strongly recommended to use numpy’s broadcasting operations and functions.

 

 

 

numpy.tile(A, reps) 함수는 A 배열을 reps에 주어진 횟수만큼 반복하여 새로운 배열을 생성합니다.

만약 reps가 길이 d를 가지고 있다면, 결과 배열은 max(d, A.ndim) 차원을 가집니다.

만약 A.ndim이 d보다 작다면, A는 새로운 축을 앞에 추가하여 d 차원으로 확장됩니다.

즉, 1차원 배열인 경우 2차원 복제를 위해 (1, 3)으로 확장되거나, 3차원 복제를 위해 (1, 1, 3)으로 확장됩니다.

이 동작이 원하는 동작이 아니라면, 이 함수를 호출하기 전에 A를 수동으로 d 차원으로 확장시키세요.

만약 A.ndim이 d보다 크다면, reps는 앞에 1을 추가하여 A.ndim과 같은 차원이 됩니다. 예를 들어, (2, 3, 4, 5) 모양의 A가 있을 때, (2, 2)의 reps는 (1, 1, 2, 2)로 처리됩니다.

참고: tile은 브로드캐스팅에 사용될 수 있지만, 넘파이의 브로드캐스팅 연산과 함수를 사용하는 것이 강력히 권장됩니다.

 

 

Parameters:

A : array_like

 

The input array.

input 값은 array

 

reps : array_like

The number of repetitions of A along each axis.

 

간단하게,  가지고 있는 array만큼을 타일 처럼 옮긴다.

Returns: c : ndarray

The tiled output array.

 

 

 

 

See also
 

repeat

Repeat elements of an array.

broadcast_to

Broadcast an array to a new shape

 


in code :

 

import numpy as np

data = np.arange(6).reshape(2,3)
print(data)

print("title(axis=0): \n",
      np.tile(data, reps=[3,1]))
print("title(axis=1):\n",
      np.tile(data, reps=[1,3]))
print("tilte(axis=0 and axis=1):\n",
      np.tile(data, reps=[3,3]))

 

code 출력 결과 :

data =

[[0 1 2]
 [3 4 5]]

title(axis=0): 
 [[0 1 2]
 [3 4 5]
 [0 1 2]
 [3 4 5]
 [0 1 2]
 [3 4 5]]

title(axis=1):
 [[0 1 2 0 1 2 0 1 2]
 [3 4 5 3 4 5 3 4 5]]

tilte(axis=0 and axis=1):
 [[0 1 2 0 1 2 0 1 2]
 [3 4 5 3 4 5 3 4 5]
 [0 1 2 0 1 2 0 1 2]
 [3 4 5 3 4 5 3 4 5]
 [0 1 2 0 1 2 0 1 2]
 [3 4 5 3 4 5 3 4 5]]