1차원 벡터
s,ndim => 1 (1차원)
s.shape => (요소의 개수(열 개수), )
s.dtype => ndarray 에 저장된 요소의 타입을 반환
정리
1. np.array(리스트) 이용해서 1차원 배열(벡터) 생성이 가능하다.
2. 반드시 동일한 타입을 지정해야 된다. 타입이 다르면 일치 시킨다. ( upcasting )
3. 정수는 기본이 4byte인 int32이고 실수는 기본이 8byte인 float64이다.
4. 차원 정보는 ndim, 모양은 shape, 저장된 데이터타입은 dtype 으로 알수 있다.
'''
import numpy as np # numpy의 별칭은 np
# 1. 1 차원 배열인 벡터 생성 , np.array(리스트)
x = [1, 2, 3]
s = np.array(x)
print(x, s) # [1, 2, 3] [1 2 3]
print(type(x), type(s)) # <class 'list'> <class 'numpy.ndarray'>
# print( x.ndim, x.shape) # x는 python의 list타입이다. ndim과 shape 속성을 지원 안함
print(s.ndim, s.shape, s.dtype) # 1 (3,) int32 ( 4byte )
'''
int8 : -128 ~ 127
int16 : -32768 ~ 32767
int32 : -2,147,483,648~ 2,147,483,647
int64 :
'''
# 2. numpy의 배열은 반드시 동일한 타입으로 저장한다. 만약 타입이 다르면 동일한 타입으로 일치시킨다.
x = [1, 2, 3, "A"]
s = np.array(x)
print(x, s) # [1, 2, 3, 'A'] ['1' '2' '3' 'A']
print(s.ndim, s.shape, s.dtype) # 1 (4,) <U11 (유니코드: 전세계의 모든 문자 표현 가능 )
x = [1, 2, 3, 100.]
s = np.array(x)
print(x, s) # [1, 2, 3, 100.0] [ 1. 2. 3. 100.]
print(s.ndim, s.shape, s.dtype) # 1 (4,) float64 ( 8byte )
x = ["A", "B", "C", "D"] # <U1
x = ["AA", "BB", "C", "D"] # <U2
x = ["AAA", "BBB", "C", "D"] # <U3
x = ["AAAA", "BBB", "C", "D"] # <U4
s = np.array(x)
print(x, s) # ['A', 'B', 'C', 'D'] ['A' 'B' 'C' 'D']
print(s.ndim, s.shape, s.dtype) # 1 (4,) <U4 ==> <U갯수
'python' 카테고리의 다른 글
| [numpy] 1차원 배열 | np.zeros(), np.ones(), np.empty(), np.full() (1) | 2022.09.28 |
|---|---|
| [numpy] 1차원 배열 | 랜덤 함수 (0) | 2022.09.28 |
| [numpy] 0차원 배열 스칼라 (0) | 2022.09.28 |
| [python] 파일 처리 (0) | 2022.09.28 |
| [python] 날짜 데이터 (0) | 2022.09.27 |