본문 바로가기

Work

Python - ndarray 배열 1차원화

아래 코드와 결과를 보면, ndarray.ravel(), numpy.concatenate(), ndarray.flatten()는 모두 유효하게 동작한다.

names = ['eric', 'eric', 'eric', 'ericson', 'eric', 'erine', 'ericson', 'erika', 'erika']
arr = np.reshape(names, (3,3))
a = arr.reshape((1,9))
b = arr.ravel()
c = np.concatenate(arr)
d = arr.flatten()
print("names=", names)
print("a=", a)
print("b=", b)
print("c=", c)
print("d=", d)

결과:

names= ['eric', 'eric', 'eric', 'ericson', 'eric', 'erine', 'ericson', 'erika', 'erika']
a= [['eric' 'eric' 'eric' 'ericson' 'eric' 'erine' 'ericson' 'erika' 'erika']]
b= ['eric' 'eric' 'eric' 'ericson' 'eric' 'erine' 'ericson' 'erika' 'erika']
c= ['eric' 'eric' 'eric' 'ericson' 'eric' 'erine' 'ericson' 'erika' 'erika']
d= ['eric' 'eric' 'eric' 'ericson' 'eric' 'erine' 'ericson' 'erika' 'erika']

 

참고로 수행해 본 reshape은 우리가 생각하는 1차원 배열을 내놓지 않는다. numpy에선 우리가 흔히 1차원이라 보는 배열을 0-dimension으로 해석하는데, array 연산 입장에선 사람의 인식과 다른 듯. 그렇다고 (0, 9)와 같은 dimension을 인식하는 것도 아니다(이건 또 사람처럼 생각하네?).

 

 

 

 

 

'Work' 카테고리의 다른 글

빅 워크를 위한 자산 조사  (0) 2020.07.15
사내 공모  (0) 2020.07.07
Python - about builtin types  (0) 2020.07.05
Definitions of Defect, Error, Failure, Fault and Problem  (0) 2020.06.30
BMS Software Engineer Test  (0) 2020.06.11