Numpy. section2 : ndarray 만들기. Lec5. 파이썬 리스트로 ndarray만들기

timekeeep·2023년 2월 11일
0

Numpy

목록 보기
4/28

[1] Making scalar ndarrays

  • 리스트, 튜플, 딕셔너리로부터 기본적인 ndarray 생성가능
# making scalar ndarray

import numpy as np

int_py = 3
float_py = 3.14

int_np = np.array(int_py)
float_np = np.array(float_py)

#integer case

print(type(int_py), type(int_np)) #<class 'int'> <class 'numpy.ndarray'>
print(int_py, int_np, sep = ' - ') # 3 - 3

#float case

print(type(float_py), type(float_np)) # <class 'float'> <class 'numpy.ndarray'>
print(float_py, float_np, sep = ' - ') # 3.14 - 3.14

[2] Making vector ndarrays

  • vec_np는 분명한 numpy array이기 때문에 numpy에서 제공해주는 다양한 api들을 사용할 준비가 되었다
  • add, sub, mul등이 element-wise로 처리가 됨
#Making vector ndarrays

import numpy as np

vec_py = [1,2,3]
vec_np = np.array(vec_py)

print(type(vec_py), type(vec_np)) # <class 'list'> <class 'numpy.ndarray'>
print(vec_py, vec_np, sep = ' - ') # [1, 2, 3] - [1 2 3]

[3] Making Matrix ndarrays

  • Numpy는 matrix가 row별로 보기 쉽게 출력됨
#Making Matrix ndarrays

import numpy as np

mat_py = [[1,2,3],[4,5,6]]

mat_np = np.array(mat_py)

print(type(mat_py), type(mat_np)) # <class 'list'> <class 'numpy.ndarray'>
print(mat_py, mat_np, sep = '\n\n')
#[[1, 2, 3], [4, 5, 6]]

#[[1 2 3]
# [4 5 6]]

[4] Making 3rd Order Tensor ndarrays

  • 이미지 데이터 사용시 이용
  • (3, 500, 600) 컬러 채널이 앞에 등장가능
  • (500, 600, 3) -> (3, 500, 600) 을 api를 이용해서 컬러 채널을 땡겨야 함
#Making 3rd Order Tensor ndarrays

import numpy as np

tensor_py = [[[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]]] #(2,2,3) 짜리 matrix

tensor_np = np.array(tensor_py)

print(type(tensor_py), type(tensor_np)) # <class 'list'> <class 'numpy.ndarray'>
print(tensor_py, tensor_np, sep = '\n\n')

'''
[[[1, 2, 3], [4, 5, 6]], [[11, 12, 13], [14, 15, 16]]]

[[[ 1  2  3]
  [ 4  5  6]]

 [[11 12 13]
  [14 15 16]]]'''

[4] Shape Notation

  • (3) 은 파이썬에서 자동적으로 스칼라 형태로 바꾸어주기 때문에 (3,)으로 튜플상태를 유지시킴
  • shape은 object의 대표적인 data에 해당하는 영역임
  • len()함수로 몇차원 텐서인지 알 수 있음
  • 핵심은 우리가 원하는 값들을 배치할 수 있다는 것임
# Shapes of ndarrays

import numpy as np

scalar_np = np.array(3.14)
vec_np = np.array([1,2,3])
mat_np = np.array([[1,2],[3,4]])
tensor_np = np.array([[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]])

print(scalar_np.shape) # ()
print(vec_np.shape) # (3,)
print(mat_np.shape) # (2, 2)
print(tensor_np.shape) # (2, 2, 3)

print(len(())) # 0
print(len((3,))) # 1
print(len((2,2))) # 2
print(len((2,2,3))) # 3
profile
Those who are wise will shine like the brightness of the heavens, and those who lead many to righteousness, like the stars for ever and ever

0개의 댓글