Worth spreading

numpy_repeat 본문

Python/Library

numpy_repeat

annual 2018. 6. 2. 11:42

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


a를 repeats만큼 반복한 결과를 출력.

axis의 값으로 repeat이 이루어질 차원(dimension)을 지정해준다


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
np.repeat(34)

>>> array([3, 3, 3, 3])

 
= 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, [12], axis=0)

>>> array([[1, 2],
       [3, 4],
       [3, 4]])

cs


다음과 같이 binary color 이미지를 3차원 color 이미지로 확장시킬 때도 활용할 수 있다.

1
2
3
4
5
6
img.shape
>>> (64, 64)
 
img = np.repeat(img[:,:,np.newaxis],3,axis=2)
img.shape
 >>> (64, 64, 3)
cs



Comments