[파이썬] numpy 배열 생성, shape 확인과 변경

InAnarchy·2023년 4월 16일
0

numpy

목록 보기
1/4
post-thumbnail

numpy

  • 선형대수 계산에 주로 사용되는 수치해석용 파이썬 패키지
  • 다차원의 배열 자료구조 클래스인 ndarray 클래스를 지원
  • 넘파이 배열은 리스트보다 적은 메모리로 많은 데이터를 빠르게 처리
  • ndim 속성은 배열의 차원, shape 속성은 배열의 크기를 반환
  • size는 배열의 전체 요소의 개수
import numpy as np

넘파이 배열

  • array 함수에 리스트를 넣으면 ndarray 클래스 객체 즉, 배열로 변환
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
  • dtype
    따로 명시하지 않을 경우 해당 리스트에서 필요한 최소한의 타입으로 결정

다른 매개변수는 공식 홈페이지에 자세히 나와있으니 생략.

1차원 배열

  • 1차원으로 묶은 배열을 수학에서는 벡터라고 표현한다.(모양에 따라 행벡터, 열벡터)
ar = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(ar) #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(type(ar)) #<class 'numpy.ndarray'>
print(ar.ndim) #1차원
print(ar.size) #10
upca = np.array([1, 2, 3.0])
print(upca) #[1. 2. 3.]
print(upca.dtype) #float64

이 때 배열에 float형이 있었으므로, dtype을 따로 명시하지 않더라도 타입이 자동으로 설정된다.

intlist = np.array([1,4,5,8], dtype = float)
print(intlist) #[1. 4. 5. 8.]
print(intlist.dtype) #float64

배열 정보는 int형이지만 dtype매개변수에서 float로 설정하였다.

2차원 배열

  • 자료형이 같은 1차원 배열들의 묶음
  • 2차원으로 묶인 배열을 수학에서 행렬(matrix)이라 부른다.
A=np.array([
    [2,-3,1,0], #첫번째 벡터는 첫번째 row 
    [2,0,-1,2], #두번째 벡터는 두번째 row
    [1,4,5,3] #세번째 벡터는 세번째 row
])
print(A.shape) #(3, 4), row는 3개, column은 4개
print(A.ndim) #2차원
print(A.size) #12
c = np.array([[0, 1, 2],
              [3, 4, 5]])  # 2 x 3 array
print(c) 
print(type(c)) #<class 'numpy.ndarray'>
print(c.ndim) #2차원
print(c.shape) #(2, 3)
print(len(c)) #2행
print(len(c[0])) #3열

이 때 넘파이 배열의 길이는 행의 개수이고
i번째 행의 길이는 열의 개수이다.

3차원 배열

  • 2차원 배열들의 묶음
  • A X B X C 배열의 경우 (B X C)라는 2차원 배열의 A개 묶음이다.
array3 = np.array([
    [[2,-3,1,0], 
    [2,0,-1,2], 
    [1,4,5,3]], #3 X 4

    [[2,-3,1,0], 
    [2,0,-1,2], 
    [1,4,5,3]] #3 X 4
])
print(array3.shape) #(2, 3, 4)
print(array3.ndim) #3차원
d = np.array([[[1, 2, 3, 4],
               [5, 6, 7, 8],
               [9, 10, 11, 12]],
               
              [[11, 12, 13, 14],
               [15, 16, 17, 18],
               [19, 20, 21, 22]]])   # 2 x 3 x 4 array
print(d)
print(d.ndim) #3차원
print(d.shape) #(2, 3, 4)
print(len(d)) #2, 깊이
print(len(d[0])) #3, 행
print(len(d[0][0])) #4, 렬

넘파이 배열 구조 다루기

인덱싱과 슬라이싱

1차원 배열의 인덱싱과 슬라이싱은 리스트와 같다.

이 때 중요한 것은 반환되는 배열은 view라는 것이다.

It must be noted that the returned array is a view, i.e., it is not a copy of the original, but points to the same values in memory as does the original array

NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list

a = np.array([0, 1, 2, 3, 4])
print(a[2]) #2
print(a[-1]) #4

다차원 배열의 경우 해당 글 참고

reshape

  • 배열의 행렬 구조를 변경
numpy.reshape(a, newshape, order='C')
a = np.array([[1,2,3], [4,5,6]])
np.reshape(a, 6) # `a` 배열을 1차원 배열로 만들어서 길이가 6인 배열로 변환


reshape() 안에 -1가 들어가있는 경우가 있는데
-1은 "남은 차원은 알아서 계산하라" 라는 뜻이다.

예를 들어 배열의 길이가 15이고, 3행으로 구조를 변경하고 싶을 때
reshape(3,-1)으로 설정하면 열은 자동으로 15/3열으로 설정된다.

a = np.arange(15)
print(a.reshape(3,-1))
print(a.reshape(3,5))
a = np.arange(15)
print(a.reshape(4,-1)) #ValueError: cannot reshape array of size 15 into shape (4,newaxis)

그렇기 때문에 행 X 렬은 전체 요소의 개수와 같아야한다.
위의 경우 15/4가 나누어떨어질 수 없기 때문에 에러.

reshape(-1)

  • reshape(-1)은 하나의 행벡터이다.(1차원 배열)
x = np.arange(12)
x = x.reshape(-1,) #(12,)
x.ndim #1
x #array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

reshape(-1,정수)

x = np.arange(12)
x = x.reshape(-1,1) # (12, 1)
x.ndim #2
x
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11]])

reshape(정수,-1)

x = np.arange(12)
x = x.reshape(1,-1) # (-1,12)
x.ndim #2
x.shape #(1,12)
x #array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]])

예시

x = np.array([[1,2,5,8], [1,2,5,8]])
print(x.shape) #(2, 4)
print(x.reshape(-1,)) #[1 2 5 8 1 2 5 8], 
a = np.arange(15)
np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
# array([[1, 2],
#        [3, 4],
#        [5, 6]])
x = np.array(range(8)).reshape(4,2)
print(x.reshape(2,2,-1))

# [[[0 1]
#   [2 3]]

#  [[4 5]
#   [6 7]]]
print(x.ndim)  #2
print(x.shape) #(4, 2)

flatten(),ravel()

  • 배열을 1차원으로 변환
a1 = np.array([[1,2],
               [3,4]])
a2 = a1.ravel() 
a2 #array([1, 2, 3, 4])
a2.ndim #1
a1 = np.array([[1,2],
               [3,4]])
a2 = a1.flatten()
a2 #array([1, 2, 3, 4])
a2.ndim #1

reshape(-1), flatten() ,ravel()의 차이는?

  • reshape(-1): 변경된 배열이 원본 배열에도 영향을 줌
a = np.array([[1, 2], [3, 4]])
b = a.reshape(-1)

b[0] = 99
print(a)
# 출력: [[99  2]
#       [ 3  4]]
  • ravel(): 변경된 배열이 원본 배열에도 영향을 줌. flatten보다 빠름
a = np.array([[1, 2], [3, 4]])
b = a.ravel()

b[0] = 99
print(a)
# 출력: [[99  2]
#       [ 3  4]]
  • flatten(): 변경된 배열이 원본 배열에 영향X
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = a.flatten()

b[0] = 99
print(a)
# 출력: [[1 2]
#       [3 4]]

REFERENCE
numpy 공식문서
친절한 설명 발견!
친절한 설명!
인도인 유튜브

profile
keep going.

0개의 댓글