numpy 기초! (astype(),shape,ndim,dtype)

김경민·2022년 12월 30일
0
post-thumbnail

astype() 타입 변환 하기

  • 타입을 변환하는 메서드
  • 리스트로 반환하기에 변수에 할당해 줘야한다.
import numpy as np
ary_1 = np.array([0,1,2,3,4,5,6,7,8,9])
ary_1_as_float = ary_1.astype('float32')
print(ary_1)
print(ary_1_as_float)
[0 1 2 3 4 5 6 7 8 9]
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
  • int8, int16, int32, float64 등의 옵션이 있다.
  • 데이터의 크기가 작으면 메모리를 최대한 줄여 효율을 높일 수 있다.

dtype 타입 확인

  • ndarray에 담긴 데이터의 테입을 볼 수 있다.
print(ary_1.dtype)
print(ary_1_as_float.dtype)
int64
float32

shape 모양 확인

print(ary_1.shape) # 모양 확인하기
(10,) # 왜 이렇게 나와요??

왜냐면 차원이 1인 배열이므로 축은 axis 0축 하나만 있다.

axis 0축부터 차례로 써주기 때문에 (10,)이라는 값이 나오는 것

(axis0, axis1, axis2,,,,)

[axis란??]

ndim 차원 확인하기

print(ary_1.ndim)
1

여러가지 예시

ary_2 = np.array( [[1, 2], [3, 4], [5, 6], [7, 8]] )
print(ary_2)
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
ary_3 = np.array( [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ] )
print(ary_3)
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
print(ary_2.shape)  # 모양 확인 하기
print(ary_2.ndim)   # 차원 확인 하기
(4, 2)  # 4행 2열
2       # 2차원
print(ary_3.shape)
print(ary_3.ndim)
(2, 2, 2)
3
ary_4 = np.array( [ [[1, 2, 2], [3, 4, 2]], [[5, 6, 2], [7, 8, 2]] ] )
print(ary_4.shape)
print(ary_4.ndim)
(2, 2, 3)
3

이해가 안 간다면

numpy axis 축 이해하기

profile
적은 대로 된다.

0개의 댓글