import numpy as np
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
다른 매개변수는 공식 홈페이지에 자세히 나와있으니 생략.
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로 설정하였다.
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번째 행의 길이는 열의 개수이다.
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
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가 나누어떨어질 수 없기 때문에 에러.
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])
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]])
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)
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
a = np.array([[1, 2], [3, 4]])
b = a.reshape(-1)
b[0] = 99
print(a)
# 출력: [[99 2]
# [ 3 4]]
a = np.array([[1, 2], [3, 4]])
b = a.ravel()
b[0] = 99
print(a)
# 출력: [[99 2]
# [ 3 4]]
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 공식문서
친절한 설명 발견!
친절한 설명!
인도인 유튜브