일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ADsP
- Python
- 솔딱새과
- 딥러닝공부
- python3
- 맑은소리 스피치학원
- keras
- 딱다구리과
- 흰날개해오라기
- 기러기목
- 참새과
- 백로과
- 계수정렬
- AI역량평가
- 생일문제
- 한국의 새
- 딥러닝 공부
- 한국의새
- AI전략게임
- 비둘기목
- 참새목
- 오리과
- Birthday paradox
- 가마우지과
- 비둘기과
- structured_array
- django
- IBK기업은행 인턴
- SimpleCraft
- 직박구리과
- Today
- Total
진박사의 일상
[Python3] numpy에서 dictionary처럼 쓸 수 있는 Structured Array 본문
python의 dictionary 자료 구조는 데이터를 관리하기 무척 편하게 해준다.
그러나 numpy에서는 dictionary 구조가 없다. 대신 이와 비슷한 Structured Array라는 것이 존재한다.
Structured arrays — NumPy v1.10 Manual (scipy.org)
Structured arrays — NumPy v1.10 Manual
Defining Structured Arrays One defines a structured array through the dtype object. There are several alternative ways to define the fields of a record. Some of these variants provide backward compatibility with Numeric, numarray, or another module, and sh
docs.scipy.org
import numpy as np
d_type = ([('id','i4'), ('distance','f8')])
distances = np.zeros(10, dtype=d_type)
'''
array([(0, 0.), (0, 0.), (0, 0.), (0, 0.), (0, 0.), (0, 0.), (0, 0.),
(0, 0.), (0, 0.), (0, 0.)],
dtype=[('id', '<i4'), ('distance', '<f8')])
'''
간단히 말해서 dtype으로 [(속성명1, 속성타입1),(속성명2, 속성타입2)...)]을 지정해주면 array에 (속성1,속성2,...속성n)의 데이터 타입을 저장할 수 있다.
여기에
distances['id']
#array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
이렇게 dictionary처럼 속성명으로 속성값을 numpy array로 불러올수도 있고
distances['id'] = np.array([range(10)])
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
이렇게 대입해줄 수도 있다.
또한 원래 데이터가 일반 numpy 배열이라도 저런식으로 구조화되어 있는 데이터라면
student = np.array([['John',10],['Jane',12]])
#array([['John', '10'],
# ['Jane', '12']], dtype='<U11')
s_student = np.core.records.fromarrays(student.transpose(), names='name, age', formats='S10, i4')
#rec.array([(b'John', 10), (b'Jane', 12)],
# dtype=[('name', 'S10'), ('age', '<i4')])
이렇게 fromarrays를 사용하여 sturctured array로 바꿔줄 수 있다.
여기서 사용되는 data type는
(바이트배열순서-생략가능) + data type + data size
의 구조를 가지는데 여기서 바이트배열순서는 생략 가능이고 little endien이니 big endien이니 복잡하니 패스. 궁금하면 따로 찾아보시길
data type은
? = boolean
i = signed integer
f = float
U = unicode
S = String
O = object
정도만 알아두면 충분할듯
ex i4(int 4byte) f8(float 8byte) U10(10byte unicode 문자열)
'프로그래밍' 카테고리의 다른 글
[작품소개] SimpleCraft - Java (0) | 2021.06.30 |
---|---|
메모 - sensor data는 sequential 이라도 lstm보다 1d-cnn? (0) | 2021.06.18 |
[python3] maximum recursion 문제 해결하기 (0) | 2021.06.05 |
[C#/Winform] 변수값 변경 EventHandler (0) | 2021.05.16 |
[django] in a frame because it set 'x-frame-options' to 'deny'. (0) | 2021.05.10 |